class ConnectionPool: (source)
Method | __getstate__ |
Undocumented |
Method | __init__ |
Create a new ConnectionPool . |
Method | __setstate__ |
Undocumented |
Method | close |
Close all pool connections and shutdown the pool. |
Method | connect |
Return a database connection when one becomes available. |
Method | disconnect |
Disconnect a database connection associated with this pool. |
Method | finalClose |
This should only be called by the shutdown trigger. |
Method | runInteraction |
Interact with the database and return the result. |
Method | runOperation |
Execute an SQL query and return None . |
Method | runQuery |
Execute an SQL query and return the result. |
Method | runWithConnection |
Execute a function with a database connection and return the result. |
Method | start |
Start the connection pool. |
Constant | CP_ARGS |
Undocumented |
Class Variable | good_sql |
Undocumented |
Class Variable | name |
Undocumented |
Class Variable | noisy |
Undocumented |
Class Variable | openfun |
Undocumented |
Class Variable | reconnect |
Undocumented |
Instance Variable | __dict__ |
Undocumented |
Instance Variable | connargs |
Undocumented |
Instance Variable | connectionFactory |
factory for connections, default to Connection . |
Instance Variable | connections |
Undocumented |
Instance Variable | connkw |
Undocumented |
Instance Variable | dbapi |
Undocumented |
Instance Variable | dbapiName |
Undocumented |
Instance Variable | max |
Undocumented |
Instance Variable | min |
Undocumented |
Instance Variable | running |
Undocumented |
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. |
Instance Variable | startID |
Undocumented |
Instance Variable | threadID |
Undocumented |
Instance Variable | threadpool |
Undocumented |
Instance Variable | transactionFactory |
factory for transactions, default to Transaction . |
Method | _close |
Undocumented |
Method | _runInteraction |
Undocumented |
Method | _runOperation |
Undocumented |
Method | _runQuery |
Undocumented |
Method | _runWithConnection |
Undocumented |
Method | _start |
Undocumented |
Instance Variable | _reactor |
The reactor which will be used to schedule startup and shutdown events. |
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') |
*connargs | Undocumented |
**connkw | Undocumented |
cp_reactor:IReactorCore provider | use this reactor instead of the global reactor (added in Twisted 10.2). |
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') |
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.
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 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 . |
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 . |
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 . |
Start the connection pool.
If you are using the reactor normally, this function does *not* need to be called.
None
or a handle on the shutdown event trigger which will be used to stop the connection pool workers when the reactor stops.