Package twisted :: Package enterprise :: Module adbapi :: Class ConnectionPool

[frames | no frames]

## Class ConnectionPool

I represent a pool of connections to a DB-API 2.0 compliant database.

You can pass cp_min, cp_max or both to set the minimum and maximum number of connections that will be opened by the pool. You can pass the noisy arg which determines whether informational log messages are generated during the pool's operation.

Method Summary
| `__init__(self, dbapiName, *connargs, **connkw)`
| `__getstate__(self)`
| `__setstate__(self, state)`
| ``_deferToThread`(self, f, *args, **kwargs)` Internal function.
| `_runInteraction(self, interaction, *args, **kw)`
| `_runOperation(self, *args, **kw)`
| `_runQuery(self, *args, **kw)`
| ``close`(self)` Close all pool connections and shutdown the pool.
| ``connect`(self)` Return a database connection when one becomes available.
| ``finalClose`(self)` This should only be called by the shutdown trigger.
| `interaction(self, interaction, callback, errback, *args, **kw)`
| `operation(self, callback, errback, *args, **kw)`
| `query(self, callback, errback, *args, **kw)`
| ``runInteraction`(self, interaction, *args, **kw)` Interact with the database and return the result.
| ``runOperation`(self, *args, **kw)` Execute an SQL query and return None.
| ``runQuery`(self, *args, **kw)` Execute an SQL query and return the result.
| ``start`(self)` Start the connection pool.
| `synchronousOperation(self, *args, **kw)`

Class Variable Summary
`int` | `max` = 5
`int` | `min` = 3
`int` | `noisy` = 1
`int` | `running` = 0

Method Details

### _deferToThread(self, f, *args, **kwargs)

Internal function.

Call f in one of the connection pool's threads.

### close(self)

Close all pool connections and shutdown the pool.

### connect(self)

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 twisted code.

  Returns:
    a database connection from the pool.

### finalClose(self)

This should only be called by the shutdown trigger.

### runInteraction(self, interaction, *args, **kw)

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 `adbapi.Transaction`. *args,**kw will be passed as additional arguments.
  Returns:
    a Deferred which will fire the return value of 'interaction(Transaction(...))', or a Failure.

### runOperation(self, *args, **kw)

Execute an SQL query and return None.

A DB-API cursor will 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.

return: a Deferred which will fire None or a Failure.

### runQuery(self, *args, **kw)

Execute an SQL query and return the result.

A DB-API cursor will 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 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 Failure.

### start(self)

Start the connection pool.

If you are using the reactor normally, this function does *not* need to be called.

Class Variable Details

### max

  Type:
    `int`
  Value:

        5

### min

  Type:
    `int`
  Value:

        3

### noisy

  Type:
    `int`
  Value:

        1

### running

  Type:
    `int`
  Value:

        0

Generated by Epydoc 2.0 on Sat May 15 20:08:48 2004 | http://epydoc.sf.net