mirror of
https://github.com/coleifer/peewee.git
synced 2026-05-06 07:56:41 -04:00
Raise OperationalError if you call connect() and it would leak
a connection. Refs #1108.
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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])
|
||||
|
||||
Reference in New Issue
Block a user