From: Mike Bayer Date: Tue, 14 Mar 2006 19:51:03 +0000 (+0000) Subject: added unique_connection() method to engine, connection pool to return a connection X-Git-Tag: rel_0_1_5~77 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7f98b58c871c690e861c16091b3c507f90b1ddda;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added unique_connection() method to engine, connection pool to return a connection that is not part of the thread-local context or any current transaction --- diff --git a/CHANGES b/CHANGES index 3ac27f1999..8810b1db16 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,8 @@ 0.1.5 - fixed attributes bug where if an object is committed, its lazy-loaded list got blown away if it hadnt been loaded +- added unique_connection() method to engine, connection pool to return a connection +that is not part of the thread-local context or any current transaction 0.1.4 - create_engine() now uses genericized parameters; host/hostname, db/dbname/database, diff --git a/lib/sqlalchemy/engine.py b/lib/sqlalchemy/engine.py index 8a29b847fc..44ffadfeb3 100644 --- a/lib/sqlalchemy/engine.py +++ b/lib/sqlalchemy/engine.py @@ -395,6 +395,10 @@ class SQLEngine(schema.SchemaEngine): """returns a managed DBAPI connection from this SQLEngine's connection pool.""" return self._pool.connect() + def unique_connection(self): + """returns a DBAPI connection from this SQLEngine's connection pool that is distinct from the current thread's connection.""" + return self._pool.unique_connection() + def multi_transaction(self, tables, func): """provides a transaction boundary across tables which may be in multiple databases. If you have three tables, and a function that operates upon them, providing the tables as a diff --git a/lib/sqlalchemy/pool.py b/lib/sqlalchemy/pool.py index 783b4450bf..ad65f7462a 100644 --- a/lib/sqlalchemy/pool.py +++ b/lib/sqlalchemy/pool.py @@ -75,7 +75,10 @@ class Pool(object): self._use_threadlocal = use_threadlocal self._echo = echo self._logger = logger or util.Logger(origin='pool') - + + def unique_connection(self): + return ConnectionFairy(self) + def connect(self): if not self._use_threadlocal: return ConnectionFairy(self) @@ -120,14 +123,17 @@ class Pool(object): self.logger.write(msg) class ConnectionFairy(object): - def __init__(self, pool): + def __init__(self, pool, connection=None): self.pool = pool - try: - self.connection = pool.get() - except: - self.connection = None - self.pool.return_invalid() - raise + if connection is not None: + self.connection = connection + else: + try: + self.connection = pool.get() + except: + self.connection = None + self.pool.return_invalid() + raise def cursor(self): return CursorFairy(self, self.connection.cursor()) def __getattr__(self, key): @@ -157,6 +163,9 @@ class SingletonThreadPool(Pool): def status(self): return "SingletonThreadPool size: %d" % len(self._conns) + def unique_connection(self): + return ConnectionFairy(self, self._creator()) + def do_return_conn(self, conn): pass def do_return_invalid(self):