[okfn-help] sqlalchemy model sessions

Rufus Pollock rufus.pollock at okfn.org
Fri Mar 14 09:16:53 GMT 2008


John Bywater wrote:
> Hi Rufus,
> 
> I'm having trouble with sessions and threads.....

We need to be a little careful here. The setup we're using should 
provide threadlocal sessions:

<http://www.sqlalchemy.org/docs/04/session.html#unitofwork_contextual>

Also vdm makes heavy use of this setup so if we start messing about too 
much with the session there will be problems.

> Do you mind if we move to having explicit sessions in microfacts? It 
> would seem to solve the problem I'm experiencing where sqlalchemy 
> bitches that my object:
> 
> "'X' is already attached to session 'Y' (this is 'Z')"
> 
> http://www.mail-archive.com/sqlalchemy-users@lists.sourceforge.net/msg03361.html 
> 
> 
> I regenerate this problem when I create from my web browser a second 
> thread or a second factlet after having previously restarted paster. 
> That is, when the create mode class makes a call to 
> session.save(new_entity) only the first create request is successful, 
> and succeeding requests all fail with this error. With the call is not 
> made, the created object is never persistent, but there is no error.

This seems a pylons issue. It should be clearing the session 
(model.session.clear at the start of each run). I implemented sqlalchemy 
into pylons following:

<http://docs.pythonweb.org:8080/display/pylonscookbook/SQLAlchemy+0.4+for+people+in+a+hurry>

We should check this stuff first (perhaps we aren't clearing the (model) 
session as we should be). In fact just looking i notice it is marked as 
deprecated but at least it indicates what i did and where you can find 
the sqlalc setup code:

myapp/config/environment.py

> Is the current model session config the Pylons project initial default, 
> ie what paster pastes? We're having problems, so let's make it explicit?

I'm not clear what should be explicit but if you mean we should clear 
the session ourselves then that may be necessary.

~rufus



More information about the okfn-help mailing list