]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- StaticPool now initializes, disposes and recreates without
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 19 Mar 2010 21:51:50 +0000 (17:51 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 19 Mar 2010 21:51:50 +0000 (17:51 -0400)
opening a new connection - the connection is only opened when
first requested. dispose() also works on AssertionPool now.
[ticket:1728]

CHANGES
lib/sqlalchemy/pool.py
test/engine/test_pool.py

diff --git a/CHANGES b/CHANGES
index abf9e775fda1249d6d724ae1ef691b0cc4d894aa..36be2bc4684e38cc5bd95bbfde6e4ced32d5ee95 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -308,7 +308,12 @@ CHANGES
     is assembled into a first_connect/connect pool listener by the 
     connection strategy if non-None.   Provides a simpler interface 
     for dialects.
-        
+    
+  - StaticPool now initializes, disposes and recreates without 
+    opening a new connection - the connection is only opened when 
+    first requested. dispose() also works on AssertionPool now.
+    [ticket:1728]
+    
 - metadata
   - Added the ability to strip schema information when using
     "tometadata" by passing "schema=None" as an argument. If schema
index 3be63ced39d24d7d48a481d3da57f5b9ee0d1601..31ab7facc5027eb39d36f06a829ab39882c8c7b8 100644 (file)
@@ -747,35 +747,10 @@ class StaticPool(Pool):
 
     """
 
-    def __init__(self, creator, **params):
-        """
-        Construct a StaticPool.
-
-        :param creator: a callable function that returns a DB-API
-          connection object.  The function will be called with
-          parameters.
-
-        :param echo: If True, connections being pulled and retrieved
-          from the pool will be logged to the standard output, as well
-          as pool sizing information.  Echoing can also be achieved by
-          enabling logging for the "sqlalchemy.pool"
-          namespace. Defaults to False.
-
-        :param reset_on_return: If true, reset the database state of
-          connections returned to the pool.  This is typically a
-          ROLLBACK to release locks and transaction resources.
-          Disable at your own peril.  Defaults to True.
-
-        :param listeners: A list of
-          :class:`~sqlalchemy.interfaces.PoolListener`-like objects or
-          dictionaries of callables that receive events when DB-API
-          connections are created, checked out and checked in to the
-          pool.
+    @memoized_property
+    def _conn(self):
+        return self._creator()
 
-        """
-        Pool.__init__(self, creator, **params)
-        self._conn = creator()
-    
     @memoized_property
     def connection(self):
         return _ConnectionRecord(self)
@@ -784,8 +759,9 @@ class StaticPool(Pool):
         return "StaticPool"
 
     def dispose(self):
-        self._conn.close()
-        self._conn = None
+        if '_conn' in self.__dict__:
+            self._conn.close()
+            self._conn = None
 
     def recreate(self):
         self.logger.info("Pool recreating")
@@ -837,7 +813,8 @@ class AssertionPool(Pool):
     
     def dispose(self):
         self._checked_out = False
-        self._conn.close()
+        if self._conn:
+            self._conn.close()
 
     def recreate(self):
         self.logger.info("Pool recreating")
index 93bcae654b4dcf95d3a31ba47382c55d9dda0047..6b0b187e6f2901771ba015febe3bf242ac752782 100644 (file)
@@ -81,7 +81,26 @@ class PoolTest(PoolTestBase):
         expected = [(1,)]
         for row in cursor:
             eq_(row, expected.pop(0))
+    
+    def test_no_connect_on_recreate(self):
+        def creator():
+            raise Exception("no creates allowed")
+        
+        for cls in (pool.SingletonThreadPool, pool.StaticPool, 
+                    pool.QueuePool, pool.NullPool, pool.AssertionPool):
+            p = cls(creator=creator)
+            p.dispose()
+            p.recreate()
         
+            mock_dbapi = MockDBAPI()
+            p = cls(creator=mock_dbapi.connect)
+            conn = p.connect()
+            conn.close()
+            mock_dbapi.throw_error = True
+            p.dispose()
+            p.recreate()
+            
+            
     def testthreadlocal_del(self):
         self._do_testthreadlocal(useclose=False)