Magnus Hult ha scritto:
> Thanks for your swift reply!
>> On 6/13/07, Manlio Perillo <manlio_perillo at libero.it> wrote:
>> Magnus Hult ha scritto:
>> > When executing the following program, changes in the database (other
>> > than those made by this program) aren't visible until it is restarted,
>> > due to some form of SQLAlchemy caching:
>>>> Are you using PostgreSQL?
>> Yes, I am. With psycopg2.
>>> [...]
>> The solution is: make sure to add a commit after every select.
>> It doesn't seem to help though. In fact, the following little program
>> t = objectstore.create_transaction()
> m = model.MyModel.get(1)
> print m.some_column
> t.commit()
> sys.stdin.readline()
> t = objectstore.create_transaction()
> m = model.MyModel.get(1)
> print m.some_column
> t.commit()
>> only seems to execute one select statement. The other result seems to
> be taken from the cache. If this is the case, then this question would
> rather belong in an SQLAlchemy mailing list, I guess.
>
The problem can be this:
http://www.sqlalchemy.org/docs/documentation.html#unitofwork_identitymap
After the first "select" you do not reset the Unif Of Work.
In my queries I always do:
def _sessionTransaction(self, callable_, *args, **kwargs):
from sqlalchemy import orm
conn = self.contextual_connect()
sess = orm.create_session(bind_to=conn) # This cannot fail
try:
trans = sess.create_transaction()
try:
ret = callable_(conn, sess, *args, **kwargs)
trans.commit()
return ret
except:
trans.rollback()
raise
finally:
sess.close() # This cannot fail
conn.close()
I create and close a new session object for every request.
You can give a look at my nadbapi:
http://developer.berlios.de/projects/nadbapi/
Regards Manlio Perillo