From: Mike Bayer Date: Sun, 5 Dec 2010 07:10:49 +0000 (-0500) Subject: - reduce some pool overhead X-Git-Tag: rel_0_7b1~209 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8e6b5ab1bd541d5f6267ebdbcca1389e00b00d02;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - reduce some pool overhead --- diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 77aa950c05..907b1930b8 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -830,6 +830,7 @@ class Connection(Connectable): """ self.engine = engine + self.dialect = engine.dialect self.__connection = connection or engine.raw_connection() self.__transaction = None self.should_close_with_result = close_with_result @@ -886,12 +887,6 @@ class Connection(Connectable): c._execution_options = c._execution_options.union(opt) return c - @property - def dialect(self): - "Dialect used by this Connection." - - return self.engine.dialect - @property def closed(self): """Return True if this connection is closed.""" diff --git a/lib/sqlalchemy/pool.py b/lib/sqlalchemy/pool.py index 02d56dead7..58b3a39c1b 100644 --- a/lib/sqlalchemy/pool.py +++ b/lib/sqlalchemy/pool.py @@ -61,6 +61,8 @@ def clear_managers(): class Pool(log.Identified): """Abstract base class for connection pools.""" + _no_finalize = False + def __init__(self, creator, recycle=-1, echo=None, use_threadlocal=False, @@ -226,19 +228,13 @@ class Pool(log.Identified): has its ``close()`` method called. """ - if self._use_threadlocal and hasattr(self._threadconns, "current"): - del self._threadconns.current + if self._use_threadlocal: + try: + del self._threadconns.current + except AttributeError: + pass self._do_return_conn(record) - def _get(self): - """Return a non-instrumented DBAPI connection from this :class:`.Pool`. - - This is called by ConnectionRecord in order to get its DBAPI - resource. - - """ - return self._do_get() - def _do_get(self): """Implementation for :meth:`get`, supplied by subclasses.""" @@ -324,12 +320,14 @@ class _ConnectionRecord(object): raise -def _finalize_fairy(connection, connection_record, pool, ref=None): +def _finalize_fairy(connection, connection_record, pool, ref, echo): _refs.discard(connection_record) + + if pool._no_finalize: + return if ref is not None and \ - (connection_record.fairy is not ref or - isinstance(pool, AssertionPool)): + connection_record.fairy is not ref: return if connection is not None: @@ -347,7 +345,9 @@ def _finalize_fairy(connection, connection_record, pool, ref=None): if connection_record is not None: connection_record.fairy = None - pool.logger.debug("Connection %r being returned to pool", connection) + if echo: + pool.logger.debug("Connection %r being returned to pool", + connection) if pool.dispatch.on_checkin: pool.dispatch.on_checkin(connection, connection_record) pool._return_conn(connection_record) @@ -359,17 +359,19 @@ class _ConnectionFairy(object): support.""" __slots__ = '_pool', '__counter', 'connection', \ - '_connection_record', '__weakref__', '_detached_info' + '_connection_record', '__weakref__', \ + '_detached_info', '_echo' def __init__(self, pool): self._pool = pool self.__counter = 0 + self._echo = _echo = pool._should_log_debug() try: - rec = self._connection_record = pool._get() + rec = self._connection_record = pool._do_get() conn = self.connection = self._connection_record.get_connection() rec.fairy = weakref.ref( self, - lambda ref:_finalize_fairy(conn, rec, pool, ref) + lambda ref:_finalize_fairy(conn, rec, pool, ref, _echo) ) _refs.add(rec) except: @@ -377,7 +379,8 @@ class _ConnectionFairy(object): self.connection = None self._connection_record = None raise - self._pool.logger.debug("Connection %r checked out from pool" % + if self._echo: + self._pool.logger.debug("Connection %r checked out from pool" % self.connection) @property @@ -482,7 +485,8 @@ class _ConnectionFairy(object): self._close() def _close(self): - _finalize_fairy(self.connection, self._connection_record, self._pool) + _finalize_fairy(self.connection, self._connection_record, + self._pool, None, self._echo) self.connection = None self._connection_record = None @@ -822,7 +826,8 @@ class AssertionPool(Pool): than desired. """ - + _no_finalize = True + def __init__(self, *args, **kw): self._conn = None self._checked_out = False