From 0c3e2b49b99655edba28230a70622982faba7185 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 30 Sep 2012 11:54:40 -0400 Subject: [PATCH] - add a glossary b.c. hey its a good idea --- doc/build/core/connections.rst | 4 +-- doc/build/core/pooling.rst | 50 ++++++++++++++++++++-------------- doc/build/glossary.rst | 38 ++++++++++++++++++++++++++ doc/build/orm/session.rst | 7 ++--- doc/build/static/docs.css | 4 +++ 5 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 doc/build/glossary.rst diff --git a/doc/build/core/connections.rst b/doc/build/core/connections.rst index 8111a3803e..3c403c1cba 100644 --- a/doc/build/core/connections.rst +++ b/doc/build/core/connections.rst @@ -64,7 +64,7 @@ an UPDATE statement (without any returned rows), releases cursor resources immediately upon construction. When the :meth:`~.Connection.close` method is called, the referenced DBAPI -connection is returned to the connection pool. From the perspective +connection is :term:`released` to the connection pool. From the perspective of the database itself, nothing is actually "closed", assuming pooling is in use. The pooling mechanism issues a ``rollback()`` call on the DBAPI connection so that any transactional state or locks are removed, and @@ -440,7 +440,7 @@ call :meth:`.Engine.contextual_connect`:: call_operation3(conn) conn.close() -Calling :meth:`~.Connection.close` on the "contextual" connection does not release +Calling :meth:`~.Connection.close` on the "contextual" connection does not :term:`release` its resources until all other usages of that resource are closed as well, including that any ongoing transactions are rolled back or committed. diff --git a/doc/build/core/pooling.rst b/doc/build/core/pooling.rst index 394fa86003..eb54630753 100644 --- a/doc/build/core/pooling.rst +++ b/doc/build/core/pooling.rst @@ -6,7 +6,7 @@ Connection Pooling .. module:: sqlalchemy.pool A connection pool is a standard technique used to maintain -long running connections in memory for efficient re-use, +long running connections in memory for efficient re-use, as well as to provide management for the total number of connections an application might use simultaneously. @@ -16,7 +16,7 @@ server-side web applications, a connection pool is the standard way to maintain a "pool" of active database connections in memory which are reused across requests. -SQLAlchemy includes several connection pool implementations +SQLAlchemy includes several connection pool implementations which integrate with the :class:`.Engine`. They can also be used directly for applications that want to add pooling to an otherwise plain DBAPI approach. @@ -48,12 +48,12 @@ dataset within the scope of a single connection. All SQLAlchemy pool implementations have in common that none of them "pre create" connections - all implementations wait until first use before creating a connection. At that point, if -no additional concurrent checkout requests for more connections +no additional concurrent checkout requests for more connections are made, no additional connections are created. This is why it's perfectly fine for :func:`.create_engine` to default to using a :class:`.QueuePool` of size five without regard to whether or not the application really needs five connections queued up - the pool would only grow to that size if the application -actually used five connections concurrently, in which case the usage of a +actually used five connections concurrently, in which case the usage of a small pool is an entirely appropriate default behavior. Switching Pool Implementations @@ -72,13 +72,13 @@ Disabling pooling using :class:`.NullPool`:: from sqlalchemy.pool import NullPool engine = create_engine( - 'postgresql+psycopg2://scott:tiger@localhost/test', + 'postgresql+psycopg2://scott:tiger@localhost/test', poolclass=NullPool) Using a Custom Connection Function ---------------------------------- -All :class:`.Pool` classes accept an argument ``creator`` which is +All :class:`.Pool` classes accept an argument ``creator`` which is a callable that creates a new connection. :func:`.create_engine` accepts this function to pass onto the pool via an argument of the same name:: @@ -127,18 +127,26 @@ within a transparent proxy:: cursor.execute("select foo") The purpose of the transparent proxy is to intercept the ``close()`` call, -such that instead of the DBAPI connection being closed, its returned to the +such that instead of the DBAPI connection being closed, it's returned to the pool:: # "close" the connection. Returns # it to the pool. conn.close() -The proxy also returns its contained DBAPI connection to the pool +The proxy also returns its contained DBAPI connection to the pool when it is garbage collected, though it's not deterministic in Python that this occurs immediately (though it is typical with cPython). +The ``close()`` step also performs the important step of calling the +``rollback()`` method of the DBAPI connection. This is so that any +existing transaction on the connection is removed, not only ensuring +that no existing state remains on next usage, but also so that table +and row locks are released as well as that any isolated data snapshots +are removed. This behavior can be disabled using the ``reset_on_return`` +option of :class:`.Pool`. + A particular pre-created :class:`.Pool` can be shared with one or more engines by passing it to the ``pool`` argument of :func:`.create_engine`:: @@ -148,24 +156,24 @@ Pool Events ----------- Connection pools support an event interface that allows hooks to execute -upon first connect, upon each new connection, and upon checkout and +upon first connect, upon each new connection, and upon checkout and checkin of connections. See :class:`.PoolEvents` for details. Dealing with Disconnects ------------------------ -The connection pool has the ability to refresh individual connections as well as +The connection pool has the ability to refresh individual connections as well as its entire set of connections, setting the previously pooled connections as -"invalid". A common use case is allow the connection pool to gracefully recover +"invalid". A common use case is allow the connection pool to gracefully recover when the database server has been restarted, and all previously established connections are no longer functional. There are two approaches to this. Disconnect Handling - Optimistic ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -The most common approach is to let SQLAlchemy handle disconnects as they -occur, at which point the pool is refreshed. This assumes the :class:`.Pool` -is used in conjunction with a :class:`.Engine`. The :class:`.Engine` has +The most common approach is to let SQLAlchemy handle disconnects as they +occur, at which point the pool is refreshed. This assumes the :class:`.Pool` +is used in conjunction with a :class:`.Engine`. The :class:`.Engine` has logic which can detect disconnection events and refresh the pool automatically. When the :class:`.Connection` attempts to use a DBAPI connection, and an @@ -187,14 +195,14 @@ that they are replaced with new ones upon next checkout:: if e.connection_invalidated: print "Connection was invalidated!" - # after the invalidate event, a new connection + # after the invalidate event, a new connection # starts with a new Pool c = e.connect() c.execute("SELECT * FROM table") The above example illustrates that no special intervention is needed, the pool continues normally after a disconnection event is detected. However, an exception is -raised. In a typical web application using an ORM Session, the above condition would +raised. In a typical web application using an ORM Session, the above condition would correspond to a single request failing with a 500 error, then the web application continuing normally beyond that. Hence the approach is "optimistic" in that frequent database restarts are not anticipated. @@ -202,7 +210,7 @@ database restarts are not anticipated. Setting Pool Recycle ~~~~~~~~~~~~~~~~~~~~~~~ -An additional setting that can augment the "optimistic" approach is to set the +An additional setting that can augment the "optimistic" approach is to set the pool recycle parameter. This parameter prevents the pool from using a particular connection that has passed a certain age, and is appropriate for database backends such as MySQL that automatically close connections that have been stale after a particular @@ -219,8 +227,8 @@ of the :class:`.Pool` itself, independent of whether or not an :class:`.Engine` Disconnect Handling - Pessimistic ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -At the expense of some extra SQL emitted for each connection checked out from the pool, -a "ping" operation established by a checkout event handler +At the expense of some extra SQL emitted for each connection checked out from the pool, +a "ping" operation established by a checkout event handler can detect an invalid connection before it's used:: from sqlalchemy import exc @@ -244,7 +252,7 @@ can detect an invalid connection before it's used:: Above, the :class:`.Pool` object specifically catches :class:`~sqlalchemy.exc.DisconnectionError` and attempts to create a new DBAPI connection, up to three times, before giving up and then raising -:class:`~sqlalchemy.exc.InvalidRequestError`, failing the connection. This recipe will ensure +:class:`~sqlalchemy.exc.InvalidRequestError`, failing the connection. This recipe will ensure that a new :class:`.Connection` will succeed even if connections in the pool have gone stale, provided that the database server is actually running. The expense is that of an additional execution performed per checkout. When using the ORM :class:`.Session`, @@ -252,7 +260,7 @@ there is one connection checkout per transaction, so the expense is fairly low. above also works with straight connection pool usage, that is, even if no :class:`.Engine` were involved. -The event handler can be tested using a script like the following, restarting the database +The event handler can be tested using a script like the following, restarting the database server at the point at which the script pauses for input:: from sqlalchemy import create_engine diff --git a/doc/build/glossary.rst b/doc/build/glossary.rst new file mode 100644 index 0000000000..8c40fcec00 --- /dev/null +++ b/doc/build/glossary.rst @@ -0,0 +1,38 @@ +.. _glossary: + +======== +Glossary +======== + +.. note:: + + The Glossary is a brand new addition to the documentation. While + sparse at the moment we hope to fill it up with plenty of new + terms soon! + +.. glossary:: + + release + releases + released + This term refers to when an operation terminates some state which + corresponds to a service of some kind. Specifically within + SQLAlchemy, it usually refers to a reference to a database connection, + and typically a transaction associated with that connection. + When we say "the operation releases transactional resources", + it means basically that we have a :class:`.Connection` object + and we are calling the :meth:`.Connection.close` method, which has + the effect of the underlying DBAPI connection being returned + to the connection pool. The connection pool, when it receives + a connection for return, unconditionally calls the ``rollback()`` + method of the DBAPI connection, so that any locks or data snapshots within + that connection are removed. Then, the connection is either + stored locally in memory, still connected but not in a transaction, + for subsequent reuse by another operation, or it is closed + immediately, depending on the configuration and current + state of the connection pool. + + .. seealso:: + + :ref:`pooling_toplevel` + diff --git a/doc/build/orm/session.rst b/doc/build/orm/session.rst index 7807b09e8f..320d2fc888 100644 --- a/doc/build/orm/session.rst +++ b/doc/build/orm/session.rst @@ -870,7 +870,7 @@ Closing ------- The :meth:`~.Session.close` method issues a -:meth:`~.Session.expunge_all`, and releases any +:meth:`~.Session.expunge_all`, and :term:`releases` any transactional/connection resources. When connections are returned to the connection pool, transactional state is rolled back as well. @@ -1211,8 +1211,7 @@ rolled back corresponding to the invocation of the also call the :meth:`.TwoPhaseTransaction.prepare` method on all transactions if applicable. When the transactional state is completed after a rollback or commit, the :class:`.Session` -releases all :class:`.Transaction` and :class:`.Connection` resources (which has the effect -of returning DBAPI connections to the connection pool of each :class:`.Engine`), +:term:`releases` all :class:`.Transaction` and :class:`.Connection` resources, and goes back to the "begin" state, which will again invoke new :class:`.Connection` and :class:`.Transaction` objects as new requests to emit SQL statements are received. @@ -1328,7 +1327,7 @@ specifically when the "begin" state occurs. A session which is configured with ``autocommit=True`` may be placed into the "begin" state using the :meth:`.Session.begin` method. After the cycle completes upon :meth:`.Session.commit` or :meth:`.Session.rollback`, -connection and transaction resources are released and the :class:`.Session` +connection and transaction resources are :term:`released` and the :class:`.Session` goes back into "autocommit" mode, until :meth:`.Session.begin` is called again:: Session = sessionmaker(bind=engine, autocommit=True) diff --git a/doc/build/static/docs.css b/doc/build/static/docs.css index b8e334616b..be13e5a754 100644 --- a/doc/build/static/docs.css +++ b/doc/build/static/docs.css @@ -355,6 +355,10 @@ dl.exception > dt padding: 0px 10px; } +dl.glossary > dt { + font-weight:bold; + font-size:1.1em; +} dt:target, span.highlight { -- 2.47.3