[sqlalchemy] Configuring an existing session

2010-01-21 Thread Tarek Ziadé
Hi,

I use a global session instance to work with my DB, so all module
import it and use it. But at some point I sometimes need to
reconfigure the engine to use another DB. I want to reconfigure the
existing instance so other modules can still use the same session
object to work with the DB.

can I safely change the bind attribute of an existing scoped session
or is there any things to be taken care of before I do it ? (like
closing active connections in the pool maybe ?)

I've tried to use configure to reset the engine, but it doesn't work
(the bind attribute remain unchanged) :

 from sqlalchemy.orm import scoped_session, sessionmaker
 from sqlalchemy import create_engine
 Session = scoped_session(sessionmaker())
 Session.configure(bind=create_engine('sqlite:///tmp/db1'))
 Session.bind
Engine(sqlite:///tmp/db1)
 Session.configure(bind=create_engine('sqlite:///tmp/db2'))
 Session.bind
Engine(sqlite:///tmp/db1)  --- same !

So, it this safe :

 from sqlalchemy.orm import scoped_session, sessionmaker
 from sqlalchemy import create_engine
 Session = scoped_session(sessionmaker())
 Session.configure(bind=create_engine('sqlite:///tmp/db1'))
 Session.bind
Engine(sqlite:///tmp/db1)
 Session.bind = create_engine('sqlite:///tmp/db2')
 Session.bind
Engine(sqlite:///tmp/db2)

Thanks
Tarek
-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.




Re: [sqlalchemy] Configuring an existing session

2010-01-21 Thread Michael Bayer
Tarek Ziadé wrote:
 Hi,

 I use a global session instance to work with my DB, so all module
 import it and use it. But at some point I sometimes need to
 reconfigure the engine to use another DB. I want to reconfigure the
 existing instance so other modules can still use the same session
 object to work with the DB.

 can I safely change the bind attribute of an existing scoped session
 or is there any things to be taken care of before I do it ? (like
 closing active connections in the pool maybe ?)

 I've tried to use configure to reset the engine, but it doesn't work
 (the bind attribute remain unchanged) :

 from sqlalchemy.orm import scoped_session, sessionmaker
 from sqlalchemy import create_engine
 Session = scoped_session(sessionmaker())
 Session.configure(bind=create_engine('sqlite:///tmp/db1'))
 Session.bind
 Engine(sqlite:///tmp/db1)


add  Session.remove() here.   configure() only works for the
not-yet-created session.

Otherwise, you can just say Session.bind = create_engine(...) which will
only change the current session.

configure() is more so that various parts of an applications startup can
add their elements to a global session config.





 Session.configure(bind=create_engine('sqlite:///tmp/db2'))
 Session.bind
 Engine(sqlite:///tmp/db1)  --- same !

 So, it this safe :

 from sqlalchemy.orm import scoped_session, sessionmaker
 from sqlalchemy import create_engine
 Session = scoped_session(sessionmaker())
 Session.configure(bind=create_engine('sqlite:///tmp/db1'))
 Session.bind
 Engine(sqlite:///tmp/db1)
 Session.bind = create_engine('sqlite:///tmp/db2')
 Session.bind
 Engine(sqlite:///tmp/db2)

 Thanks
 Tarek
 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalch...@googlegroups.com.
 To unsubscribe from this group, send email to
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.




-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.