[mod_python] persistent db connections?

John Mudd mudd at vex.net
Sun Mar 28 06:40:34 EST 2004


I've looked at the following mod_python FAQ entry:

	3.3. What is the best way to make 
	persistent connections to a database?



So, I just need some global variables as in this example.

def handler(req):
                                                                        
    global  persist, conn
                                                                        
    try:
        persist += 1
        # No need to create the db connection here.
    except NameError:
        persist = 0
        conn = db.open()   # Create a db connection.


At least this is easy.  But there's no guarantee my next request will
reuse the same thread/process.  So I might (will) end up with extra db
connections in other threads/processes.  For my application that's not
so bad.

Hmmm... I may want to add code to also check if the session has changed
(e.g. the user has re-logged in).  Then close/reopen a db connection if
the user has logged in again.  That would give me an external control so
I can force a new db connection by re-logging into my application. 

And maybe I should also register a cleanup function to close the
connection.




Am I on the right track?



More information about the Mod_python mailing list