On Dec 2, 2003, at 2:29 PM, Eduardo Elgueta wrote:
I usually store a database connection in my sessions. How can I make the
database connection pickable? Are regular classes pickable or I have to
add custom code? Sessions expire because of this or they're just not saved
to disk?

Most classes are pickleable, but only if they don't have anything transient -- like an open file object, a socket, or in this case a database connection.


One way to handle this would be to put the connection parameters in the session, and create the connection in your servlet's awake() method (or probably your SitePage's awake). If you use DBPool, it will reuse old connections based on those parameters, or make a new connection if necessary. So when you restart the server, the connection will be recreated.

If you are doing long transactions then you need to be more sure that you get the exact connection back. Then you might put a connection ID (that you make yourself) into the session, and keep the actual connections in a module global. Like:

_connections = {}
_count = 0
_connLock = threading.Lock()
def saveConnection(conn):
    _connLock.acquire()
    connID = _count
    _count += 1
    _connLock.release()
    # Make sure it's extra unique:
    connID = '%s_%s' % (time.time, connID)
    _connections[connID] = connection
    return connID

def getConnection(connID):
try:
return _connections[connID]
except KeyError:
raise KeyError, "Your connection has expired, been closed, or has been lost in a server restart"
# and maybe redirect or something intelligent based on this


--
Ian Bicking | [EMAIL PROTECTED] | http://blog.ianbicking.org



-------------------------------------------------------
This SF.net email is sponsored by OSDN's Audience Survey.
Help shape OSDN's sites and tell us what you think. Take this
five minute survey and you could win a $250 Gift Certificate.
http://www.wrgsurveys.com/2003/osdntech03.php?site=8
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to