On Nov 1, 2011, at 5:49 PM, Benjamin Sims wrote:

> Thanks for this. As an update, I put in code that notifies me when an error 
> occurs. The real cause of this seems to still be the 'MySQL has gone away' 
> error. This occurs in various places, always as the first DB call in a 
> request (obviously).
> 
> Is my understanding correct that in order to solve this I have to make sure 
> that all DB calls do a session.close() or session.rollback() when finished?

If you're doing a web app with pyramid, there really should be built in, or 
very easy to implement, a single event at the end of the request - it calls 
session.close(), and that's it.   You shouldn't have to do defensive 
rollback()/close() calls anywhere else, it should be automatic, ultimately 
occurring on just one line of code, somewhere.   My understanding was that the 
whole zope sqlalchemy thing would at least do this, and the folks on the 
Pyramid list should be able to show you how.

If some other part of your app is not within the usual "web request" system, 
such as a script that runs in the background, you'd need to ensure that system 
cleans up after itself too, but always, there should be an architecture in 
place that takes care of this, without the need for you to "remember to do it 
everywhere" - that's not how things like this should be done.

A "MySQL has gone away" error can be a symptom of out-of-sequence calls on the 
connection, which would occur with concurrent access among multiple threads.   
You should make sure that no SQLAlchemy mapped object that you got from a 
Session is shared among threads, or pulled from a dictionary that threads 
share, or part of any kind of global registry, without special handling to 
ensure that individual threads get a local copy of the state before 
manipulating or reading from it.   This refers to instances of objects, not the 
classes or mappings themselves which aren't subject to these limitations.



-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@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.

Reply via email to