summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2006-05-28 17:46:45 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2006-05-28 17:46:45 +0000
commit6b03def6cc293bd0117a21999706f43e675f1005 (patch)
tree7937b0716ed40b3cae6de12f58d94eb2d89ebac7 /lib/sqlalchemy
parentb62451e321574f4a39ac0deee01f42c5846b2d38 (diff)
downloadsqlalchemy-6b03def6cc293bd0117a21999706f43e675f1005.tar.gz
TLEngine needed a partial rewrite....
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/engine/base.py10
-rw-r--r--lib/sqlalchemy/engine/strategies.py2
-rw-r--r--lib/sqlalchemy/engine/threadlocal.py91
3 files changed, 53 insertions, 50 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index 83dfad04f..16ca5299e 100644
--- a/lib/sqlalchemy/engine/base.py
+++ b/lib/sqlalchemy/engine/base.py
@@ -194,6 +194,8 @@ class Connection(Connectable):
return self.__transaction
else:
return self._create_transaction(self.__transaction)
+ def in_transaction(self):
+ return self.__transaction is not None
def _begin_impl(self):
if self.__engine.echo:
self.__engine.log("BEGIN")
@@ -210,13 +212,13 @@ class Connection(Connectable):
"""when no Transaction is present, this is called after executions to provide "autocommit" behavior."""
# TODO: have the dialect determine if autocommit can be set on the connection directly without this
# extra step
- if self.__transaction is None and re.match(r'UPDATE|INSERT|CREATE|DELETE|DROP', statement.lstrip().upper()):
+ if not self.in_transaction() and re.match(r'UPDATE|INSERT|CREATE|DELETE|DROP', statement.lstrip().upper()):
self._commit_impl()
def close(self):
if self.__connection is not None:
self.__connection.close()
self.__connection = None
- def scalar(self, object, parameters, **kwargs):
+ def scalar(self, object, parameters=None, **kwargs):
row = self.execute(object, parameters, **kwargs).fetchone()
if row is not None:
return row[0]
@@ -406,6 +408,10 @@ class ComposedSQLEngine(sql.Engine, Connectable):
conn.close()
def transaction(self, callable_, connection=None, *args, **kwargs):
+ """executes the given function within a transaction boundary. this is a shortcut for
+ explicitly calling begin() and commit() and optionally rollback() when execptions are raised.
+ The given *args and **kwargs will be passed to the function, as well as the Connection used
+ in the transaction."""
if connection is None:
conn = self.contextual_connect()
else:
diff --git a/lib/sqlalchemy/engine/strategies.py b/lib/sqlalchemy/engine/strategies.py
index fbd9b8bab..51496a67d 100644
--- a/lib/sqlalchemy/engine/strategies.py
+++ b/lib/sqlalchemy/engine/strategies.py
@@ -54,7 +54,7 @@ class ThreadLocalEngineStrategy(EngineStrategy):
dialect = module.dialect(**kwargs)
poolargs = {}
- for key in (('echo', 'echo_pool'), ('pool_size', 'pool_size'), ('max_overflow', 'max_overflow'), ('poolclass', 'poolclass'), ('pool_timeout','timeout')):
+ for key in (('echo', 'echo_pool'), ('pool_size', 'pool_size'), ('max_overflow', 'max_overflow'), ('poolclass', 'poolclass'), ('pool_timeout','timeout'), ('pool', 'pool')):
if kwargs.has_key(key[0]):
poolargs[key[1]] = kwargs[key[0]]
poolclass = getattr(module, 'poolclass', None)
diff --git a/lib/sqlalchemy/engine/threadlocal.py b/lib/sqlalchemy/engine/threadlocal.py
index 85628c208..610eedeaa 100644
--- a/lib/sqlalchemy/engine/threadlocal.py
+++ b/lib/sqlalchemy/engine/threadlocal.py
@@ -6,39 +6,42 @@ import base, default
will return the same connection for the same thread. also provides begin/commit methods on the engine itself
which correspond to a thread-local transaction."""
-class TLTransaction(base.Transaction):
- def rollback(self):
+class TLSession(object):
+ def __init__(self, engine):
+ self.engine = engine
+ self.__tcount = 0
+ def get_connection(self, close_with_result=False):
try:
- base.Transaction.rollback(self)
- finally:
+ return self.__transaction
+ except AttributeError:
+ return base.Connection(self.engine, close_with_result=close_with_result)
+ def begin(self):
+ if self.__tcount == 0:
+ self.__transaction = self.get_connection()
+ self.__trans = self.__transaction.begin()
+ self.__tcount += 1
+ def rollback(self):
+ if self.__tcount > 0:
try:
- del self.connection.engine.context.transaction
- except AttributeError:
- pass
+ self.__trans.rollback()
+ finally:
+ del self.__transaction
+ del self.__trans
+ self.__tcount = 0
def commit(self):
- try:
- base.Transaction.commit(self)
- stack = self.connection.engine.context.transaction
- stack.pop()
- if len(stack) == 0:
- del self.connection.engine.context.transaction
- except:
+ if self.__tcount == 1:
try:
- del self.connection.engine.context.transaction
- except AttributeError:
- pass
- raise
-
-class TLConnection(base.Connection):
- def _create_transaction(self, parent):
- return TLTransaction(self, parent)
- def begin(self):
- t = base.Connection.begin(self)
- if not hasattr(self.engine.context, 'transaction'):
- self.engine.context.transaction = []
- self.engine.context.transaction.append(t)
- return t
-
+ self._trans.commit()
+ finally:
+ del self.__transaction
+ del self._trans
+ self.__tcount = 0
+ elif self.__tcount > 1:
+ self.__tcount -= 1
+ def is_begun(self):
+ return self.__tcount > 0
+
+
class TLEngine(base.ComposedSQLEngine):
"""a ComposedSQLEngine that includes support for thread-local managed transactions. This engine
is better suited to be used with threadlocal Pool object."""
@@ -55,29 +58,23 @@ class TLEngine(base.ComposedSQLEngine):
"""returns a Connection that is not thread-locally scoped. this is the equilvalent to calling
"connect()" on a ComposedSQLEngine."""
return base.Connection(self, self.connection_provider.unique_connection())
+
+ def _session(self):
+ if not hasattr(self.context, 'session'):
+ self.context.session = TLSession(self)
+ return self.context.session
+ session = property(_session, doc="returns the current thread's TLSession")
+
def contextual_connect(self, **kwargs):
"""returns a TLConnection which is thread-locally scoped."""
- return TLConnection(self, **kwargs)
+ return self.session.get_connection(**kwargs)
+
def begin(self):
- return self.connect().begin()
+ return self.session.begin()
def commit(self):
- if hasattr(self.context, 'transaction'):
- self.context.transaction[-1].commit()
+ self.session.commit()
def rollback(self):
- if hasattr(self.context, 'transaction'):
- self.context.transaction[-1].rollback()
- def transaction(self, func, *args, **kwargs):
- """executes the given function within a transaction boundary. this is a shortcut for
- explicitly calling begin() and commit() and optionally rollback() when execptions are raised.
- The given *args and **kwargs will be passed to the function as well, which could be handy
- in constructing decorators."""
- trans = self.begin()
- try:
- func(*args, **kwargs)
- except:
- trans.rollback()
- raise
- trans.commit()
+ self.session.rollback()
class TLocalConnectionProvider(default.PoolConnectionProvider):
def unique_connection(self):