Skip to content
Snippets Groups Projects
Commit cf8a960d authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

Option to regenerate connection pool added

Sometimes, after you fork(), your connections are in pretty bad state.
This call will get them right on their feet.
parent fa32c6aa
No related branches found
No related tags found
No related merge requests found
......@@ -161,6 +161,15 @@ class ConnectionPool(object):
for i in xrange(0, connections):
self.connections.put(self.dd.get_connection())
def regenerate_all(self):
"""Sanitize connections. Use it for occassions
such as fork() and so on"""
for i in xrange(0, self.c_num):
con = self.connections.get()
con.close()
for i in xrange(0, self.c_num):
self.connections.put(self.dd.get_connection())
def close(self):
"""Closes all connections in this connection pool.
Use only when you are sure that no-one is using the
......
......@@ -42,6 +42,19 @@ class PoolTest(unittest.TestCase):
cp.close()
def test_pool_regenerate_query(self):
"""Creates a pool with a single connection and does SELECT 2+2 with that.
Regenerates connections in the meantime"""
cp = ConnectionPool(self.dd)
cp.regenerate_all()
with cp.cursor() as cur:
cur.execute('SELECT 2+2')
a, = cur.fetchone()
self.assertEquals(a, 4)
cp.close()
def test_pool_onetime_reconnect(self):
"""Creates a pool with a single connection, get the connection, close it
and return it, anticipating that it will be regenerated"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment