twisted.enterprise.adbapi.ConnectionPool
class documentationtwisted.enterprise.adbapi
View Source
(View In Hierarchy)
Represent a pool of connections to a DB-API 2.0 compliant database.
Instance Variable | connectionFactory | factory for connections, default to Connection . (type: any callable.) |
Instance Variable | transactionFactory | factory for transactions, default to Transaction . (type: any callable) |
Instance Variable | shutdownID | None
or a handle on the shutdown event trigger which will be used to stop the
connection pool workers when the reactor stops. |
Method | __init__ | Create a new ConnectionPool . |
Method | start | Start the connection pool. |
Method | runWithConnection | Execute a function with a database connection and return the result. |
Method | runInteraction | Interact with the database and return the result. |
Method | runQuery | Execute an SQL query and return the result. |
Method | runOperation | Execute an SQL query and return None . |
Method | close | Close all pool connections and shutdown the pool. |
Method | finalClose | This should only be called by the shutdown trigger. |
Method | connect | Return a database connection when one becomes available. |
Method | disconnect | Disconnect a database connection associated with this pool. |
Method | __getstate__ | Undocumented |
Method | __setstate__ | Undocumented |
Instance Variable | _reactor | The reactor which will be used to schedule startup and shutdown events. (type: IReactorCore
provider) |
Method | _start | Undocumented |
Method | _runWithConnection | Undocumented |
Method | _close | Undocumented |
Method | _runInteraction | Undocumented |
Method | _runQuery | Undocumented |
Method | _runOperation | Undocumented |
None
or a handle on the shutdown event trigger which will be used to stop the
connection pool workers when the reactor stops.
IReactorCore
provider)
Create a new ConnectionPool
.
Any positional or keyword arguments other than those documented here are passed to the DB-API object when connecting. Use these arguments to pass database names, usernames, passwords, etc.
Parameters | dbapiName | an import string to use to obtain a DB-API compatible module (e.g.
'pyPgSQL.PgSQL' ) |
cp_min | the minimum number of connections in pool (default 3) | |
cp_max | the maximum number of connections in pool (default 5) | |
cp_noisy | generate informational log messages during operation (default
False ) | |
cp_openfun | a callback invoked after every connect() on the underlying
DB-API object. The callback is passed a new DB-API connection object. This
callback can setup per-connection state such as charset, timezone, etc. | |
cp_reconnect | detect connections which have failed and reconnect (default
False ). Failed connections may result in ConnectionLost
exceptions, which indicate the query may need to be re-sent. | |
cp_good_sql | an sql query which should always succeed and change no state (default
'select 1' ) | |
cp_reactor | use this reactor instead of the global reactor (added in Twisted 10.2). (type: IReactorCore
provider) |
Start the connection pool.
If you are using the reactor normally, this function does *not* need to be called.
Execute a function with a database connection and return the result.
Parameters | func | A callable object of one argument which will be executed in a thread with a
connection from the pool. It will be passed as its first argument a Connection
instance (whose interface is mostly identical to that of a connection
object for your DB-API module of choice), and its results will be returned
as a Deferred . If
the method raises an exception the transaction will be rolled back.
Otherwise, the transaction will be committed. Note that this
function is not run in the main thread: it must be threadsafe. |
*args | positional arguments to be passed to func | |
**kw | keyword arguments to be passed to func | |
Returns | a Deferred
which will fire the return value of func(Transaction(...), *args,
**kw) , or a twisted.python.failure.Failure . |
Interact with the database and return the result.
The 'interaction' is a callable object which will be executed in a
thread using a pooled connection. It will be passed an Transaction
object as an argument (whose interface is identical to that of the database
cursor for your DB-API module of choice), and its results will be returned
as a Deferred
. If
running the method raises an exception, the transaction will be rolled
back. If the method returns a value, the transaction will be committed.
NOTE that the function you pass is *not* run in the main thread: you may have to worry about thread-safety in the function you pass to this if it tries to use non-local objects.
Parameters | interaction | a callable object whose first argument is an adbapi.Transaction . |
*args | additional positional arguments to be passed to interaction | |
**kw | keyword arguments to be passed to interaction | |
Returns | a Deferred which will fire the return value of
interaction(Transaction(...), *args, **kw) , or a twisted.python.failure.Failure . |
Execute an SQL query and return the result.
A DB-API cursor which will be invoked with cursor.execute(*args,
**kw)
. The exact nature of the arguments will depend on the specific
flavor of DB-API being used, but the first argument in *args
be an SQL statement. The result of a subsequent
cursor.fetchall()
will be fired to the Deferred
which
is returned. If either the 'execute' or 'fetchall' methods raise an
exception, the transaction will be rolled back and a twisted.python.failure.Failure
returned.
The *args
and **kw
arguments will be passed to
the DB-API cursor's 'execute' method.
Returns | a Deferred
which will fire the return value of a DB-API cursor's 'fetchall' method, or
a twisted.python.failure.Failure . |
Execute an SQL query and return None
.
A DB-API cursor which will be invoked with cursor.execute(*args,
**kw)
. The exact nature of the arguments will depend on the specific
flavor of DB-API being used, but the first argument in *args
will be an SQL statement. This method will not attempt to fetch any results
from the query and is thus suitable for INSERT
,
DELETE
, and other SQL statements which do not return values.
If the 'execute' method raises an exception, the transaction will be rolled
back and a Failure
returned.
The *args
and *kw
arguments will be passed to
the DB-API cursor's 'execute' method.
Returns | a Deferred
which will fire with None
or a twisted.python.failure.Failure . |
Return a database connection when one becomes available.
This method blocks and should be run in a thread from the internal threadpool. Don't call this method directly from non-threaded code. Using this method outside the external threadpool may exceed the maximum number of connections in the pool.
Returns | a database connection from the pool. |
Disconnect a database connection associated with this pool.
Note: This function should only be used by the same thread which called
ConnectionPool.connect
.
As with connect
, this function is not used in normal
non-threaded Twisted code.