Commit Graph

100 Commits

Author SHA1 Message Date
Mike Bayer 1e278de4cc Post black reformatting
Applied on top of a pure run of black -l 79 in
I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9,  this set of changes
resolves all remaining flake8 conditions for those codes
we have enabled in setup.cfg.

Included are resolutions for all remaining flake8 issues
including shadowed builtins, long lines, import order, unused
imports, duplicate imports, and docstring issues.

Change-Id: I4f72d3ba1380dd601610ff80b8fb06a2aff8b0fe
2019-01-06 18:23:11 -05:00
Mike Bayer 1e1a38e780 Run black -l 79 against all source files
This is a straight reformat run using black as is, with no edits
applied at all.

The black run will format code consistently, however in
some cases that are prevalent in SQLAlchemy code it produces
too-long lines.   The too-long lines will be resolved in the
following commit that will resolve all remaining flake8 issues
including shadowed builtins, long lines, import order, unused
imports, duplicate imports, and docstring issues.

Change-Id: I7eda77fed3d8e73df84b3651fd6cfcfe858d4dc9
2019-01-06 17:34:50 +00:00
Taem Park b64a3dd87a Add LIFO for connection pooling
Added new "lifo" mode to :class:`.QueuePool`, typically enabled by setting
the flag :paramref:`.create_engine.pool_use_lifo` to True.   "lifo" mode
means the same connection just checked in will be the first to be checked
out again, allowing excess connections to be cleaned up from the server
side during periods of the pool being only partially utilized.  Pull request
courtesy Taem Park.

Change-Id: Idb5e299c5082b3e6b547bd03022acf65fdc34f35
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/467
2018-09-18 21:58:02 -04:00
Mike Bayer 9700eb126b Refactor pool.py into a package
for better separation of concerns split out the
core pooling constructs from the implementations.  also
makes more room for new pool classes should we decide to
add them.  The DBProxy feature is never used so remove it
from documentation and split it out.

Change-Id: I04c9d7ae1882b17f3cf5c37ed05b781b05ef88fa
Fixes: #4255
2018-07-09 16:59:00 -04:00
Mike Bayer dada909a10 Prevent double-checkins and guard during reset-on-return invalidations
Fixed connection pool issue whereby if a disconnection error were raised
during the connection pool's "reset on return" sequence in conjunction with
an explicit transaction opened against the enclosing :class:`.Connection`
object (such as from calling :meth:`.Session.close` without a rollback or
commit, or calling :meth:`.Connection.close` without first closing a
transaction declared with :meth:`.Connection.begin`), a double-checkin would
result, which could then lead towards concurrent checkouts of the same
connection. The double-checkin condition is now prevented overall by an
assertion, as well as the specific double-checkin scenario has been
fixed.

Change-Id: If5bb6941e36326846b14918c33ebfdd5604f642e
Fixes: #4252
2018-05-15 16:16:59 -04:00
Mike Bayer c543729671 Invalidate on failed connect handler
Fixed bug in connection pool where a connection could be present in the
pool without all of its "connect" event handlers called, if a previous
"connect" handler threw an exception; note that the dialects themselves
have connect handlers that emit SQL, such as those which set transaction
isolation, which can fail if the database is in a non-available state, but
still allows a connection.  The connection is now invalidated first if any
of the connect handlers fail.

Change-Id: I61d6f4827a98ab8455f1c3e1c55d046eeccec09a
Fixes: #4225
2018-03-28 15:49:17 -04:00
Mike Bayer 1e6b9b04de Ensure weakref finalize_fairy operates upon the current connection
Fixed a fairly serious connection pool bug where a connection that is
acquired after being refreshed as a result of a user-defined
:class:`.DisconnectionError` or due to the 1.2-released "pre_ping" feature
would not be correctly reset if the connection were returned to the pool by
weakref cleanup (e.g. the front-facing object is garbage collected); the
weakref would still refer to the previously invalidated DBAPI connection
which would have the reset operation erroneously called upon it instead.
This would lead to stack traces in the logs and a connection being checked
into the pool without being reset, which can cause locking issues.

Change-Id: Iabd9f3a63a1d0207d0de0054a6ced3560818cf9c
Fixes: #4184
2018-02-08 17:40:09 -05:00
Mike Bayer cdd090b11e Disable the singletonthreadpool test cleanup no gc
This test often fails on CI under load and reveals a small
race condition within SingleonThreadPool's "cleanup" system.
As this pool's "cleanup" system is pretty much never used in
any case since this pool is no longer used for SQLite file
databases, this race should have little impact, especially
since the occasional non-closed connection will get closed
out when it's GC'ed in any case.

Change-Id: I68aefc0f9bbfc708c95af0497844e65366fc8429
2018-01-09 23:35:23 -05:00
Mike Bayer cedb11925f - keep trying to identify the race here. can reproduce locally
now and it seems like mock might not be doing the right thing.

Change-Id: I5c108d82631c9217da54a8ace68d7728c3e204d8
2017-04-27 17:44:29 -04:00
Mike Bayer 1b63306a97 - try to work around a race that can occur in STP when
used in this intentionally broken scenario

Change-Id: I88ea6fa710da2189e6d47e2d12f5f0fd6f6bb7d4
2017-04-27 15:26:35 -04:00
Ville Skyttä e47063bfe0 Spelling fixes 2016-10-08 20:42:50 +03:00
Mike Bayer 01a0a2d542 Additions to support HAAlchemy plugin
- add a connect=True key to connection record to support
  pre-loading of _ConnectionRecord objects
- ensure _ConnectionRecord.close() leaves the record in a good
  state for reopening
- add _ConnectionRecord.record_info for persistent storage
- add "in_use" accessor based on fairy_ref being present or not
- allow for the exclusions system and SuiteRequirements to be
  usable without the full plugin_base setup.
- move some Python-env requirements to the importable
  requirements.py module.
- allow starttime to be queried
- add additional events for engine plugins
- have "dialect" be a first-class parameter to the pool,
  ensure the engine strategy supplies it up front

Change-Id: Ibf549f7a1766e49d335cd6f5e26bacfaef9a8229
2016-09-16 16:20:18 -04:00
Mike Bayer e5bdf96bc0 - dont use id() to test identity as these can be recycled
Change-Id: Ie4cb4924909d55c5962f66e36cd5325e8e8f0538
2016-06-15 18:15:46 -04:00
Mike Bayer 345cc1e304 - flake8
Change-Id: I74e369d1d71c98cb0af58dde80b7d535d17cbdf7
2016-06-15 18:10:46 -04:00
Mike Bayer 74ca7f627e - use a mock here, do away with timing problems for good. need to do this
for the rest of the timing tests

Change-Id: I06a815b1500222126a4dbc2a9a5da3ef7847e321
2016-06-02 18:40:46 -04:00
Mike Bayer 8a776122a3 - Added connection pool events :meth:ConnectionEvents.close,
:meth:`.ConnectionEvents.detach`,
:meth:`.ConnectionEvents.close_detached`.
2016-03-25 18:31:17 -04:00
Mike Bayer 4096ad0f09 - Fixed critical issue whereby the pool "checkout" event handler
may be called against a stale connection without the "connect"
event handler having been called, in the case where the pool
attempted to reconnect after being invalidated and failed; the stale
connection would remain present and would be used on a subsequent
attempt.  This issue has a greater impact in the 1.0 series subsequent
to 1.0.2, as it also delivers a blanked-out ``.info`` dictionary to
the event handler; prior to 1.0.2 the ``.info`` dictionary is still
the previous one.
fixes #3497
2015-07-22 18:03:36 -04:00
Mike Bayer 4a0e51e7d2 - Fixed bug where in the case that a pool checkout event handler is used
and the database can no longer be connected towards, that the checkout
handler failure is caught, the attempt to re-acquire the connection
also raises an exception, but the underlying connection record
is not immediately re-checked in before the exception is propagated
outwards, having the effect that the checked-out record does not close
itself until the stack trace it's associated with is garbage collected,
preventing that record from being used for a new checkout until we
leave the scope of the stack trace.   This can lead to confusion
in the specific case of when the number of current stack traces
in memory exceeds the number of connections the pool can return,
as the pool will instead begin to raise errors about no more checkouts
available, rather than attempting a connection again.   The fix
applies a checkin of the record before re-raising.
fixes #3419
2015-05-14 13:59:04 -04:00
Mike Bayer 4992aafecc - revise the last commit with a more traditional approach
using descriptors; ensure that mock.patch() honors descriptor setters
2015-04-30 20:20:02 -04:00
Mike Bayer e0f9b279f4 - work the wrapping of the "creator" to be as resilient to
old / new style, direct access, and ad-hoc patching and
unpatching as possible
2015-04-30 19:55:28 -04:00
Mike Bayer 0e98795ff2 - New features added to support engine/pool plugins with advanced
functionality.   Added a new "soft invalidate" feature to the
connection pool at the level of the checked out connection wrapper
as well as the :class:`._ConnectionRecord`.  This works similarly
to a modern pool invalidation in that connections aren't actively
closed, but are recycled only on next checkout; this is essentially
a per-connection version of that feature.  A new event
:class:`.PoolEvents.soft_invalidate` is added to complement it.
fixes #3379

- Added new flag
:attr:`.ExceptionContext.invalidate_pool_on_disconnect`.
Allows an error handler within :meth:`.ConnectionEvents.handle_error`
to maintain a "disconnect" condition, but to handle calling invalidate
on individual connections in a specific manner within the event.

- Added new event :class:`.DialectEvents.do_connect`, which allows
interception / replacement of when the :meth:`.Dialect.connect`
hook is called to create a DBAPI connection.  Also added
dialect plugin hooks :meth:`.Dialect.get_dialect_cls` and
:meth:`.Dialect.engine_created` which allow external plugins to
add events to existing dialects using entry points.
fixes #3355
2015-04-30 17:51:14 -04:00
Mike Bayer 623e5b2149 Merge remote-tracking branch 'origin/pr/163' into pr163 2015-04-12 14:56:15 -04:00
Mike Bayer 44a9820b4e - add some teardown for connections that are held open after tests
in some cases, interfering with tests that check pool._refs
2015-04-08 00:10:54 -04:00
Eric Streeper 0f0e305d25 PEP8 cleanup in /test/engine 2015-03-20 00:32:05 -07:00
Mike Bayer 6fd073b912 - take out the iterator approach here as it does not support concurrent access 2014-08-07 12:36:50 -04:00
Mike Bayer 0bf33068f4 - ensure all tests are named test_* 2014-07-30 12:18:33 -04:00
Mike Bayer 54592942c4 - add support for tags, including include/exclude support.
simplify tox again now that we can exclude tests more easily
2014-07-27 18:46:20 -04:00
Mike Bayer fe878f5aff - more pg8000 tests passing 2014-07-25 16:39:44 -04:00
Mike Bayer 761c8ff15d - work on fixing some race-condition failures:
1. make sure pool._invalidate() sets the timestamp up before
invalidating the target connection.  we can otherwise show how the
conn.invalidate() + pool._invalidate() can lead to an extra connection
being made.
2. to help with that, soften up the check on connection.invalidate()
when connection is already closed. a warning is fine here
3. add a mutex to test_max_overflow() when we connect, because the way
we're using mock depends on an iterator, that needs to be synchronized
2014-03-26 16:31:52 -04:00
Mike Bayer ebd24974f4 take out accidental PG dialect use here 2014-03-24 10:09:07 -04:00
Mike Bayer eed9cfc3ae - A major improvement made to the mechanics by which the :class:.Engine
recycles the connection pool when a "disconnect" condition is detected;
instead of discarding the pool and explicitly closing out connections,
the pool is retained and a "generational" timestamp is updated to
reflect the current time, thereby causing all existing connections
to be recycled when they are next checked out.   This greatly simplifies
the recycle process, removes the need for "waking up" connect attempts
waiting on the old pool and eliminates the race condition that many
immediately-discarded "pool" objects could be created during the
recycle operation. fixes #2985
2014-03-22 18:45:39 -04:00
Mike Bayer dd32540dab - Fixed a critical regression caused by 🎫2880 where the newly
concurrent ability to return connections from the pool means that the
"first_connect" event is now no longer synchronized either, thus leading
to dialect mis-configurations under even minimal concurrency situations.
2014-02-19 10:48:32 -05:00
Mike Bayer 9c64607572 - :class:.Connection now associates a new
:class:`.RootTransaction` or :class:`.TwoPhaseTransaction`
with its immediate :class:`._ConnectionFairy` as a "reset handler"
for the span of that transaction, which takes over the task
of calling commit() or rollback() for the "reset on return" behavior
of :class:`.Pool` if the transaction was not otherwise completed.
This resolves the issue that a picky transaction
like that of MySQL two-phase will be
properly closed out when the connection is closed without an
explicit rollback or commit (e.g. no longer raises "XAER_RMFAIL"
in this case - note this only shows up in logging as the exception
is not propagated within pool reset).
This issue would arise e.g. when using an orm
:class:`.Session` with ``twophase`` set, and then
:meth:`.Session.close` is called without an explicit rollback or
commit.   The change also has the effect that you will now see
an explicit "ROLLBACK" in the logs when using a :class:`.Session`
object in non-autocommit mode regardless of how that session was
discarded.  Thanks to Jeff Dairiki and Laurence Rowe for isolating
the issue here. [ticket:2907]
2014-01-12 19:43:13 -05:00
Mike Bayer c91fd822bc - add new event PoolEvents.invalidate(). allows interception of invalidation
events including auto-invalidation, which is useful both for tests here as well as
detecting failure conditions within the "reset" or "close" cases.
- rename the argument for PoolEvents.reset() to dbapi_connection and connection_record
to be consistent with everything else.
- add new documentation sections on invalidation, including auto-invalidation
and the invalidation process within the pool.
- add _ConnectionFairy and _ConnectionRecord to the pool documentation.  Establish
docs for common _ConnectionFairy/_ConnectionRecord methods and accessors and
have PoolEvents docs refer to _ConnectionRecord,
since it is passed to all events.  Rename a few _ConnectionFairy methods that are actually
private to pool such as _checkout(), _checkin() and _checkout_existing(); there should not
be any external code calling these
2014-01-12 17:34:20 -05:00
Mike Bayer d1cc78479d - The :class:.QueuePool has been enhanced to not block new connection
attempts when an existing connection attempt is blocking.  Previously,
the production of new connections was serialized within the block
that monitored overflow; the overflow counter is now altered within
it's own critical section outside of the connection process itself.
[ticket:2880]
2013-12-06 19:57:19 -05:00
Mike Bayer 003b288b76 - Made a slight adjustment to the logic which waits for a pooled
connection to be available, such that for a connection pool
with no timeout specified, it will every half a second break out of
the wait to check for the so-called "abort" flag, which allows the
waiter to break out in case the whole connection pool was dumped;
normally the waiter should break out due to a notify_all() but it's
possible this notify_all() is missed in very slim cases.
This is an extension of logic first introduced in 0.8.0, and the
issue has only been observed occasionally in stress tests.
2013-12-06 16:33:42 -05:00
Mike Bayer 604df86190 make sure thread.join() is used completely here 2013-11-30 16:54:44 -05:00
Mike Bayer 1a809b300e - apply a timeout to all join() calls for test_pool
- use thread.join() for waiters_handled test
2013-11-17 14:35:11 -05:00
Mike Bayer cdc9475b0b try to get this to pass on slow environmnets 2013-10-21 23:17:36 -04:00
Mike Bayer 08a6a8b519 - Removed some now unneeded version checks [ticket:2829] courtesy alex gaynor 2013-09-22 20:35:40 -04:00
Mike Bayer 59675be673 try seeing if just clearing this ahead of time helps 2013-07-27 18:32:38 -04:00
Mike Bayer 5dbe771922 further tweaking 2013-07-13 16:41:16 -04:00
Mike Bayer c896c75971 fix the scoping here 2013-07-13 16:37:10 -04:00
Mike Bayer f390639bf1 Fixed bug where :class:.QueuePool would lose the correct
checked out count if an existing pooled connection failed to reconnect
after an invalidate or recycle event.  Also in 0.8.3.
[ticket:2772]
2013-07-04 13:18:00 -04:00
Mike Bayer d3d10c982c - refactor pool a bit so that intent between ConnectionRecord/ConnectionFairy is clear;
make sure that the DBAPI connection passed to the reset-on-return events/dialect hooks
is also a "fairy", so that dictionaries like "info" are available.  [ticket:2770]
- rework the execution_options system so that the dialect is given the job of making
any immediate adjustments based on a set event.  move the "isolation level" logic to use
this new system.   Also work things out so that even engine-level execution options
can be used for things like isolation level; the dialect attaches a connect-event
handler in this case to handle the task.
- to support this new system as well as further extensibiltiy of execution options
add events engine_connect(), set_connection_execution_options(), set_engine_execution_options()
2013-07-02 13:14:21 -04:00
Mike Bayer 38c5e870a7 mark the tests that seem to be segfauling py3k+coverage so that we can exclude them
at the nose command line
2013-06-30 23:55:32 -04:00
Mike Bayer b38a76cd1d - replace most explicitly-named test objects called "Mock..." with
actual mock objects from the mock library.  I'd like to use mock
for new tests so we might as well use it in obvious places.
- use unittest.mock in py3.3
- changelog
- add a note to README.unittests
- add tests_require in setup.py
- have tests import from sqlalchemy.testing.mock
- apply usage of mock to one of the event tests.  we can be using
this approach all over the place.
2013-06-30 18:35:12 -04:00
Eevee 23ce1d8b12 Preserve reset_on_return when recreating a Pool. 2013-06-10 15:04:39 -07:00
Mike Bayer cca97433c1 merge default 2013-05-05 01:42:26 -04:00
Mike Bayer 31792e86ae - cleanup
- move the timeout here to 14 seconds as jenkins still chokes occasionally
2013-05-05 01:39:35 -04:00