5. API: ConnectionPool Objects
5.1. ConnectionPool Class
- class oracledb.ConnectionPool(dsn: str | None = None, *, params: PoolParams | None = None, cache_name: str | None = None, **kwargs)
Constructor for creating a connection pool.
The new ConnectionPool class is synonymous with SessionPool. The SessionPool class is deprecated in python-oracledb. The preferred function to create pools is now
This object is an extension to the DB API definition.oracledb.create_pool(). (The name SessionPool came from the Oracle Call Interface (OCI) session pool. This implementation is only used in python-oracledb Thick mode and is not available in Thin mode).
In python-oracledb, the type pool will show the class oracledb.ConnectionPool. This only affects the name.
The following code will continue to work providing backward compatibility with the obsolete cx_Oracle driver:
issubclass(cls, oracledb.SessionPool) == True
isinstance(pool, oracledb.SessionPool) == True
The following code will also work:
issubclass(cls, oracledb.ConnectionPool) == True
isinstance(pool, oracledb.ConnectionPool) == True
The function oracledb.SessionPool() that is used to create pools is
deprecated in python-oracledb 1.0 and has been deprecated by the function
oracledb.create_pool().
5.2. ConnectionPool Methods
- ConnectionPool.acquire(user: str | None = None, password: str | None = None, cclass: str | None = None, purity: int = oracledb.PURITY_DEFAULT, tag: str | None = None, matchanytag: bool = False, shardingkey: list | None = None, supershardingkey: list | None = None) Connection
Acquires a connection from the session pool and returns a connection object.
If the pool is homogeneous, the
userandpasswordparameters cannot be specified. If they are, an exception will be raised.The
cclassparameter, if specified, should be a string corresponding to the connection class for Database Resident Connection Pooling (DRCP).The
purityparameter is expected to be one ofPURITY_NEW,PURITY_SELF, orPURITY_DEFAULT.The
tagparameter, if specified, is expected to be a string with name=value pairs like “k1=v1;k2=v2” and will limit the connections that can be returned from a connection pool unless thematchanytagparameter is set to True. In that case, connections with the specified tag will be preferred over others, but if no such connections are available, then a connection with a different tag may be returned instead. In any case, untagged connections will always be returned if no connections with the specified tag are available. Connections are tagged when they arereleasedback to the pool.The
shardingkeyandsupershardingkeyparameters, if specified, are expected to be a sequence of values which will be used to identify the database shard to connect to. The key values can be strings, numbers, bytes, or dates. See Connecting to Oracle Globally Distributed Database.When using the connection pool cache, calling
oracledb.connect()with apool_aliasparameter is the same as callingpool.acquire().
- ConnectionPool.close(force: bool = False) None
Closes the pool now, rather than when the last reference to it is released, which makes it unusable for further work.
If any connections have been acquired and not released back to the pool, this method will fail unless the
forceparameter is set to True.
- ConnectionPool.drop(connection: Connection) None
Drops the connection from the pool which is useful if the connection is no longer usable (such as when the session is killed).
- ConnectionPool.reconfigure(min: int | None = None, max: int | None = None, increment: int | None = None, getmode: int | None = None, timeout: int | None = None, wait_timeout: int | None = None, max_lifetime_session: int | None = None, max_sessions_per_shard: int | None = None, soda_metadata_cache: bool | None = None, stmtcachesize: int | None = None, ping_interval: int | None = None) None
Reconfigures various parameters of a connection pool. The pool size can be altered with
reconfigure()by passing values formin,maxorincrement. Thegetmode,timeout,wait_timeout,max_lifetime_session,max_sessions_per_shard,soda_metadata_cache,stmtcachesizeandping_intervalattributes can be set directly or withreconfigure().All parameters are optional. Unspecified parameters will leave those pool attributes unchanged. The parameters are processed in two stages. After any size change has been processed, reconfiguration on the other parameters is done sequentially. If an error such as an invalid value occurs when changing one attribute, then an exception will be generated but any already changed attributes will retain their new values.
During reconfiguration of a pool’s size, the behavior of
ConnectionPool.acquire()depends on thegetmodein effect whenacquire()is called:With mode
POOL_GETMODE_FORCEGET, anacquire()call will wait until the pool has been reconfigured.With mode
POOL_GETMODE_TIMEDWAIT, anacquire()call will try to acquire a connection in the time specified by pool.wait_timeout and return an error if the time taken exceeds that value.With mode
POOL_GETMODE_WAIT, anacquire()call will wait until after the pool has been reconfigured and a connection is available.With mode
POOL_GETMODE_NOWAIT, if the number of busy connections is less than the pool size,acquire()will return a new connection after pool reconfiguration is complete.
Closing connections with
ConnectionPool.release()orConnection.close()will wait until any pool size reconfiguration is complete.Closing the connection pool with
ConnectionPool.close()will wait until reconfiguration is complete.Reconfigures various parameters of a connection pool. The pool size can be altered with
reconfigure()by passing values formin,maxorincrement. Thegetmode,timeout,wait_timeout,max_lifetime_session,max_sessions_per_shard,soda_metadata_cache,stmtcachesizeandping_intervalattributes can be set directly or withreconfigure().All parameters are optional. Unspecified parameters will leave those pool attributes unchanged. The parameters are processed in two stages. After any size change has been processed, reconfiguration on the other parameters is done sequentially. If an error such as an invalid value occurs when changing one attribute, then an exception will be generated but any already changed attributes will retain their new values.
During reconfiguration of a pool’s size, the behavior of
ConnectionPool.acquire()depends on thegetmodein effect whenacquire()is called:With mode
POOL_GETMODE_FORCEGET, anacquire()call will wait until the pool has been reconfigured.With mode
POOL_GETMODE_TIMEDWAIT, anacquire()call will try to acquire a connection in the time specified by pool.wait_timeout and return an error if the time taken exceeds that value.With mode
POOL_GETMODE_WAIT, anacquire()call will wait until after the pool has been reconfigured and a connection is available.With mode
POOL_GETMODE_NOWAIT, if the number of busy connections is less than the pool size,acquire()will return a new connection after pool reconfiguration is complete.
Closing connections with
ConnectionPool.release()orConnection.close()will wait until any pool size reconfiguration is complete.Closing the connection pool with
ConnectionPool.close()will wait until reconfiguration is complete.
- ConnectionPool.release(connection: Connection, tag: str | None = None) None
Releases the connection back to the pool now, rather than whenever __del__ is called. The connection will be unusable from this point forward; an Error exception will be raised if any operation is attempted with the connection. Any cursors or LOBs created by the connection will also be marked unusable and an Error exception will be raised if any operation is attempted with them.
Internally, references to the connection are held by cursor objects, LOB objects, etc. Once all of these references are released, the connection itself will be released back to the pool automatically. Either control references to these related objects carefully or explicitly release connections back to the pool in order to ensure sufficient resources are available.
If the tag is not None, it is expected to be a string with name=value pairs like “k1=v1;k2=v2” and will override the value in the property
Connection.tag. If eitherConnection.tagor the tag parameter are not None, the connection will be retagged when it is released back to the pool.
5.3. ConnectionPool Attributes
- property ConnectionPool.busy: int
This read-only attribute returns the number of connections currently acquired.
- property ConnectionPool.dsn: str
This read-only attribute returns the TNS entry of the database to which a connection has been established.
- property ConnectionPool.getmode: PoolGetMode
This read-write attribute determines how connections are returned from the pool. If
POOL_GETMODE_FORCEGETis specified, a new connection will be returned even if there are no free connections in the pool.POOL_GETMODE_NOWAITwill raise an exception if there are no free connections are available in the pool. IfPOOL_GETMODE_WAITis specified and there are no free connections in the pool, the caller will wait until a free connection is available.POOL_GETMODE_TIMEDWAITuses the value ofwait_timeoutto determine how long the caller should wait for a connection to become available before returning an error.
- property ConnectionPool.homogeneous: bool
This read-only boolean attribute indicates whether the pool is considered homogeneous or not. If the pool is not homogeneous, different authentication can be used for each connection acquired from the pool.
- property ConnectionPool.increment: int
This read-only attribute returns the number of connections that will be established when additional connections need to be created.
- property ConnectionPool.max: int
This read-only attribute returns the maximum number of connections that the pool can control.
- property ConnectionPool.max_lifetime_session: int
This read-write attribute is the maximum length of time (in seconds) that a pooled connection may exist since first being created. A value of 0 means there is no limit. Connections become candidates for termination when they are acquired or released back to the pool, and have existed for longer than
max_lifetime_sessionseconds. Connections that are in active use will not be closed. In python-oracledb Thick mode, Oracle Client libraries 12.1 or later must be used and, prior to Oracle Client 21, cleanup only occurs when the pool is accessed.Changed in version 3.0.0: This attribute was added to python-oracledb Thin mode.
- property ConnectionPool.max_sessions_per_shard: int
This read-write attribute returns the number of sessions that can be created per shard in the pool. Setting this attribute greater than zero specifies the maximum number of sessions in the pool that can be used for any given shard in a sharded database. This lets connections in the pool be balanced across the shards. A value of 0 will not set any maximum number of sessions for each shard. This attribute is only available in Oracle Client 18.3 and higher.
- property ConnectionPool.min: int
This read-only attribute returns the number of connections with which the connection pool was created and the minimum number of connections that will be controlled by the connection pool.
- property ConnectionPool.name: str
This read-only attribute returns the name assigned to the pool by Oracle.
- property ConnectionPool.opened: int
This read-only attribute returns the number of connections currently opened by the pool.
- property ConnectionPool.ping_interval: int
This read-write integer attribute specifies the pool ping interval in seconds. When a connection is acquired from the pool, a check is first made to see how long it has been since the connection was put into the pool. If this idle time exceeds
ping_interval, then a round-trip ping to the database is performed. If the connection is unusable, it is discarded and a different connection is selected to be returned byacquire(). Settingping_intervalto a negative value disables pinging. Setting it to 0 forces a ping for everyacquire()and is not recommended.Prior to cx_Oracle 8.2, the ping interval was fixed at 60 seconds.
- property ConnectionPool.soda_metadata_cache: bool
This read-write boolean attribute returns whether the SODA metadata cache is enabled or not. Enabling the cache significantly improves the performance of methods
SodaDatabase.createCollection()(when not specifying a value for themetadataparameter) andSodaDatabase.openCollection(). Note that the cache can become out of date if changes to the metadata of cached collections are made externally.
- property ConnectionPool.stmtcachesize: int
This read-write attribute specifies the size of the statement cache that will be used for connections obtained from the pool. Once a connection is created, that connection’s statement cache size can only be changed by setting the
stmtcachesizeattribute on the connection itself.See Statement Caching for more information.
- property ConnectionPool.thin: bool
This read-only attribute returns a boolean indicating if python-oracledb is in Thin mode (True) or Thick mode (False).
- property ConnectionPool.timeout: int
This read-write attribute specifies the time (in seconds) after which idle connections will be terminated in order to maintain an optimum number of open connections. A value of 0 means that no idle connections are terminated. Note that in python-oracledb Thick mode with older Oracle Client Libraries, the termination only occurs when the pool is accessed.
- property ConnectionPool.username: str
This read-only attribute returns the name of the user which established the connection to the database.
- property ConnectionPool.wait_timeout: int
This read-write attribute specifies the time (in milliseconds) that the caller should wait for a connection to become available in the pool before returning with an error. This value is only used if the
getmodeparameter tooracledb.create_pool()was the valueoracledb.POOL_GETMODE_TIMEDWAIT.