I'll try to answer briefly and then will leave the computer...
On Fri, Jun 05, 2009 at 11:21:49PM +0200, Herwig Hochleitner wrote:
> Writing 'connection_val' to connection foo
> Value of transaction foo: 'init'
> Writing 'transaction_val' to transaction foo
> Value of connection foo: 'connection_val'
> import sqlobject
> # Turn off caching so that multiple connections (ie transactions) sync properly
> conn = sqlobject.connectionForURI("sqlite:/:memory:", debug=True, logger="database.query", loglevel="debug", cache=False)
> class Dummy(sqlobject.SQLObject):
> foo = sqlobject.StringCol()
> Dummy.createTable(connection=conn)
> Dummy(foo="init", connection=conn)
> tr = conn.transaction()
> conn_dummy=Dummy.get(1,conn)
> trans_dummy=Dummy.get(1,tr)
> print "Writing 'connection_val' to connection foo"
> conn_dummy.foo = "connection_val"
> print "Value of transaction foo: '%s'" % trans_dummy.foo
AFAIU, what is going on there is:
-- the code opens the first connection (without a transaction);
-- a new table is created via connection 1;
-- a new row is inserted via connection 1;
-- the code opens another connection, and BEGINs a transaction ;
-- the code draws two copies of the row via connection1 and connection2;
-- the code changes the attribute .foo of the FIRST copy;
-- the code prints the attribute .foo of the SECOND copy that was drawn via
another transaction, and the value is still 'init' because it isn't
changed - the code hasn't changed trans_dummy.foo yet.
I consider this behaviour correct, there is nothing to fix. Two
different objects have two different values. They are two different objects
because they have been drawn via two different connections; every
connection has its own cache, and they are not synced.
If you want to "fix" this in your program - change the order of actions:
-- draw the first row, change it and COMMIT the transaction;
-- draw the second copy of the row.
Or:
-- draw the first row;
-- draw the second copy of the row.
-- change the first copy and COMMIT the transaction;
-- clear the cache of second connection and re-draw the second copy of the row.
Oleg.
--
Oleg Broytmann http://phd.pp.ru/ ph...@ph...
Programmers don't die, they just GOSUB without RETURN.
|