diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-09-02 15:29:29 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-09-02 15:29:29 -0400 |
commit | 280e6878b22ff17d0e3a087b85717892387b79f1 (patch) | |
tree | 3716d5160fbb9b51f15ac20e00055bd6367bc598 | |
parent | 88e448f99474f44b42ab9e6ce48e7b4238657d1b (diff) | |
download | sqlalchemy-280e6878b22ff17d0e3a087b85717892387b79f1.tar.gz |
- add _ConnectionRecord.record_info for persistent storage
Change-Id: I55de02d9421a3da7b17e24de6fdcaf5daacbba4f
-rw-r--r-- | lib/sqlalchemy/pool.py | 46 | ||||
-rw-r--r-- | test/engine/test_pool.py | 28 |
2 files changed, 74 insertions, 0 deletions
diff --git a/lib/sqlalchemy/pool.py b/lib/sqlalchemy/pool.py index 087f22c60..554f01bcd 100644 --- a/lib/sqlalchemy/pool.py +++ b/lib/sqlalchemy/pool.py @@ -469,6 +469,31 @@ class _ConnectionRecord(object): This dictionary is shared among the :attr:`._ConnectionFairy.info` and :attr:`.Connection.info` accessors. + .. note:: + + The lifespan of this dictionary is linked to the + DBAPI connection itself, meaning that it is **discarded** each time + the DBAPI connection is closed and/or invalidated. The + :attr:`._ConnectionRecord.record_info` dictionary remains + persistent throughout the lifespan of the + :class:`._ConnectionRecord` container. + + """ + return {} + + @util.memoized_property + def record_info(self): + """An "info' dictionary associated with the connection record + itself. + + Unlike the :attr:`._ConnectionRecord.info` dictionary, which is linked + to the lifespan of the DBAPI connection, this dictionary is linked + to the lifespan of the :class:`._ConnectionRecord` container itself + and will remain persisent throughout the life of the + :class:`._ConnectionRecord`. + + .. versionadded:: 1.1 + """ return {} @@ -814,9 +839,30 @@ class _ConnectionFairy(object): with the :attr:`._ConnectionRecord.info` and :attr:`.Connection.info` accessors. + The dictionary associated with a particular DBAPI connection is + discarded when the connection itself is discarded. + """ return self._connection_record.info + @property + def record_info(self): + """Info dictionary associated with the :class:`._ConnectionRecord + container referred to by this :class:`.ConnectionFairy`. + + Unlike the :attr:`._ConnectionFairy.info` dictionary, the lifespan + of this dictionary is persistent across connections that are + disconnected and/or invalidated within the lifespan of a + :class:`._ConnectionRecord`. + + .. versionadded:: 1.1 + + """ + if self._connection_record: + return self._connection_record.record_info + else: + return None + def invalidate(self, e=None, soft=False): """Mark this connection as invalidated. diff --git a/test/engine/test_pool.py b/test/engine/test_pool.py index 56a3d6d23..5b87c90b8 100644 --- a/test/engine/test_pool.py +++ b/test/engine/test_pool.py @@ -232,6 +232,34 @@ class PoolTest(PoolTestBase): assert not c2.info assert 'foo2' in c.info + def test_rec_info(self): + p = self._queuepool_fixture(pool_size=1, max_overflow=0) + + c = p.connect() + self.assert_(not c.record_info) + self.assert_(c.record_info is c._connection_record.record_info) + + c.record_info['foo'] = 'bar' + c.close() + del c + + c = p.connect() + self.assert_('foo' in c.record_info) + + c.invalidate() + c = p.connect() + self.assert_('foo' in c.record_info) + + c.record_info['foo2'] = 'bar2' + c.detach() + is_(c.record_info, None) + is_(c._connection_record, None) + + c2 = p.connect() + + assert c2.record_info + assert 'foo2' in c2.record_info + def test_rec_unconnected(self): # test production of a _ConnectionRecord with an # initally unconnected state. |