Raise OperationalError if you call connect() and it would leak

a connection. Refs #1108.
This commit is contained in:
Charles Leifer
2016-10-25 14:32:30 -05:00
parent c259cad60a
commit 9e76c99986
2 changed files with 6 additions and 2 deletions
+3 -2
View File
@@ -3602,8 +3602,9 @@ class Database(object):
def connect(self):
with self._conn_lock:
if self.deferred:
raise Exception('Error, database not properly initialized '
'before opening connection')
raise OperationalError('Database has not been initialized')
if not self._local.closed:
raise OperationalError('Connection already open')
self._local.conn = self._create_connection()
self._local.closed = False
with self.exception_wrapper:
+3
View File
@@ -108,8 +108,10 @@ class TestPooledDatabase(PeeweeTestCase):
def test_max_conns(self):
for i in range(self.db.max_connections):
self.db._local.closed = True
self.db.connect()
self.assertEqual(self.db.get_conn(), i + 1)
self.db._local.closed = True
self.assertRaises(ValueError, self.db.connect)
def test_stale_timeout(self):
@@ -215,6 +217,7 @@ class TestPooledDatabase(PeeweeTestCase):
self.assertEqual(db._connections, [(now, 4)])
# Since conn 4 is closed, we will open a new conn.
db._local.closed = True # Pretend we're in a different thread.
db.connect()
self.assertEqual(db.get_conn(), 5)
self.assertEqual(sorted(db._in_use.keys()), [3, 5])