adsva:
Actually yesterday I coded something _very_ similar
Maybe it would be better to test the actual connection
before returning
it... Just a thought that crossed my mind. Hmm, I have just
looked on
the connection from MySQLdb and it doesn't have a check
function... It
would be pretty useful - like having an isConnectionOpen
method or
something similar.
To get the timeout one can do this:
mysqld --verbose --help
Find interactive_timeout.
My code
========================================================
timeout = (28800-(60*60)) #MySQL interactive timeout. Is set
1 hour
before the actual value (so no chances are taken).
class ConnectionPool:
def __init__(self, constructor=NiceShoeWrapper,
pool_count=5):
self.q = Queue.Queue(pool_count)
self.constructor = constructor
for i in range(pool_count):
self.releaseConnection( constructor() )
def getSize(self):
return self.q.qsize()
def getConnection(self):
try:
put_stamp, obj = self.q.get(False)
if time()-put_stamp < timeout:
return obj
else:
obj.close()
return self.constructor()
except Queue.Empty:
return self.constructor()
def releaseConnection(self, obj):
try:
return self.q.put((time(), obj), False)
except Queue.Full:
#The queue is full, close the connection and ignore it
obj.close()
My unit test
========================================================
def test_pool():
assert amiweb.db_pool.getSize() == 5
#Test that the pool gets smaller
connections = []
for i in range(0, 5):
connections.append(amiweb.db_pool.getConnection())
assert amiweb.db_pool.getSize() == 0
#Test that connections can get back into the pool
map(amiweb.db_pool.releaseConnection, connections)
assert amiweb.db_pool.getSize() == 5
#Test that the connections in the pool are equal to the
first ones
#i.e. connections should be reused
new_connections = []
for i in range(0, 5):
new_connections.append(amiweb.db_pool.getConnection())
assert new_connections == connections
#The pool is empty, a new connection should be returned
assert amiweb.db_pool.getSize() == 0
con = amiweb.db_pool.getConnection()
assert con != None
assert con not in new_connections
#Put this connection back in
amiweb.db_pool.releaseConnection(con)
assert amiweb.db_pool.getSize() == 1
#Put the new_connections back in and except the pool size
to be size
5
map(amiweb.db_pool.releaseConnection, new_connections[:4])
assert amiweb.db_pool.getSize() == 5
#The last connection should be closed
#The last is the magic number...
con_last = new_connections[-1]
assert con_last.is_closed == False
amiweb.db_pool.releaseConnection(con_last)
assert con_last.is_closed == True
assert amiweb.db_pool.getSize() == 5
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users googlegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribe googlegroups.com
For more options, visit this group at http://
groups.google.com/group/cherrypy-users
-~----------~----~----~----~------~----~------~--~---
|