On Nov 6, 2008, at 1:29 PM, Randy Syring wrote:

>
> I am developing a WSGI based web framework with sqlalchemy.  I am
> unclear about when create_engine() should be called.  I initially
> thought that engine creation and metadata would be initialized per
> process and each thread/request would just get a new session.
> However, I have recently run into error messages when using sqlite
> with the framework in a threaded WSGI server:
>
> "SQLite objects created in a thread can only be used in that same
> thread..."
>
> That lead me to this thread:
>
> http://groups.google.com/group/pylons-discuss/browse_thread/thread/3d3009cd7421c45a/ed0f3dde401ff474?lnk=gst
>
> Can someone weigh in on this issue?  What are the performance
> ramifications of needing to create an engine on each request as
> opposed to each process?  Do I also need to load my meta data on each
> request or could I just re-bind the engine to the metadata on each
> request?  Should I not bind the engine to the metadata at all but just
> bind it to the session?
>

you should definitely create the engine and metadata on a per-process  
basis.   When using SQLite, the engine automatically chooses the  
"SingletonThreadPool" connection pool, which will maintain a single  
SQLite connection per application thread, which is never moved across  
threads (unless you did so explicitly).   The error you're getting  
would only occur if you are sharing the connection returned by the  
engine across threads, which can also occur if you're using a single  
Session that's bound to a connection across threads.  When using the  
scoped_session() manager, this also should not occur - some  
description of this lifecycle is at 
http://www.sqlalchemy.org/docs/05/session.html#unitofwork_contextual_lifespan 
  .

The scoped_session approach is widely used in conjunction with sqlite  
in many web frameworks including Pylons, Turbogears and Zope so you'd  
have to ensure that your specific approach is not sharing a single  
Connection or Session between threads.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to