summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/pool/impl.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-04-03 10:03:17 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2019-04-03 10:12:57 -0400
commita326869cf9c166afea43b1bd7d7f5dbab27abb75 (patch)
tree657c26947834a98a754be1ec8c2064e36750e4c5 /lib/sqlalchemy/pool/impl.py
parentfd2ecb5f87c1c0132263b5a35067c4bb76160fb2 (diff)
downloadsqlalchemy-a326869cf9c166afea43b1bd7d7f5dbab27abb75.tar.gz
Restore use_threadlocal equivalent behavior to SingletonThreadPool
Fixed behavioral regression as a result of deprecating the "use_threadlocal" flag for :class:`.Pool`, where the :class:`.SingletonThreadPool` no longer makes use of this option which causes the "rollback on return" logic to take place when the same :class:`.Engine` is used multiple times in the context of a transaction to connect or implicitly execute, thereby cancelling the transaction. While this is not the recommended way to work with engines and connections, it is nonetheless a confusing behavioral change as when using :class:`.SingletonThreadPool`, the transaction should stay open regardless of what else is done with the same engine in the same thread. The ``use_threadlocal`` flag remains deprecated however the :class:`.SingletonThreadPool` now implements its own version of the same logic. Fixes: #4585 Change-Id: I906293f2d0a5d14ed46cd9e64305a6481505a5a3
Diffstat (limited to 'lib/sqlalchemy/pool/impl.py')
-rw-r--r--lib/sqlalchemy/pool/impl.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/sqlalchemy/pool/impl.py b/lib/sqlalchemy/pool/impl.py
index ada319661..e1a457bf3 100644
--- a/lib/sqlalchemy/pool/impl.py
+++ b/lib/sqlalchemy/pool/impl.py
@@ -13,6 +13,7 @@
import traceback
import weakref
+from .base import _ConnectionFairy
from .base import _ConnectionRecord
from .base import Pool
from .. import exc
@@ -288,6 +289,7 @@ class SingletonThreadPool(Pool):
def __init__(self, creator, pool_size=5, **kw):
Pool.__init__(self, creator, **kw)
self._conn = threading.local()
+ self._fairy = threading.local()
self._all_conns = set()
self.size = pool_size
@@ -346,6 +348,25 @@ class SingletonThreadPool(Pool):
self._all_conns.add(c)
return c
+ def connect(self):
+ # vendored from Pool to include use_threadlocal behavior
+ try:
+ rec = self._fairy.current()
+ except AttributeError:
+ pass
+ else:
+ if rec is not None:
+ return rec._checkout_existing()
+
+ return _ConnectionFairy._checkout(self, self._fairy)
+
+ def _return_conn(self, record):
+ try:
+ del self._fairy.current
+ except AttributeError:
+ pass
+ self._do_return_conn(record)
+
class StaticPool(Pool):