Note: This class is not available with the PO_NO_DATABASE parse option.
The DatasourcePool class provides transparent per-thread, per-transaction Datasource connection pooling.
In most cases, the DatasourcePool class can be used as a drop-in replacement for the Datasource class with autocommit disabled; when a transaction begins, a datasource will be automatically assigned to the calling thread, and it will only be released when a commit or rollback is called on the object. If no Datasource is available, the calling thread will block until a Datasource comes available.
Note that the same principles apply to SQL and database driver usage as with the Datasource class, see the Datasource class documentation for more information.
The DatasourcePool class uses Qore's thread resource tracking infrastructure to raise an exception if a thread terminates while a connection is allocated to it. If Qore user code enters a transaction with a DatasourcePool object and the thread terminates without closing the transaction (via DatasourcePool::commit() or DatasourcePool::rollback()), an exception will automatically be raised, the transaction will be rolled back, and the Datasource connection will be freed to the pool.
The following methods allocate a persistent connection to the calling thread: DatasourcePool::exec(), DatasourcePool::vexec(), DatasourcePool::execRaw(), and DatasourcePool::beginTransaction(). The connection is released to the pool when DatasourcePool::commit() or DatasourcePool::rollback() are called (or in the case the thread terminates, in which case an exception is raised as well).
To begin a transaction with one of the select methods (for example, with "select for update"), call DatasourcePool::beginTransaction() first to manually dedicate a Datasource to the thread before calling the select method. Otherwise statements that should be in the same transaction may be executed in different connections.
Executing a DatasourcePool method while not in a transaction is realized by allocating a temporary connection to the calling thread which is re-released when the method returns. No explicit commits are executed by the class, therefore it is an error to execute transaction-relevant commands without first calling DatasourcePool::exec(), DatasourcePool::vexec(), DatasourcePool::execRaw(), or DatasourcePool::beginTransaction().
Note that the SQLStatement class also grabs allocates a persistent connection to the calling thread when executing if it is created using a DatasourcePool object in the constructor; for more information see the SQLStatement class.
Table 4.630. DatasourcePool Method Overview
Method | Except? | Description |
|---|---|---|
| Y | Creates the DatasourcePool object; attempts to load a DBI driver if the driver is not already present in Qore. |
Y | Destroys the object. | |
Y | Throws an exception; currently DatasourcePool objects may not be copied. | |
Y | Commits the transaction and releases the connection to the pool. | |
Y | Rolls back the transaction and releases the connection to the pool. | |
| Y | Executes SQL code on the DB connection and dedicates a connection to the calling thread. |
Y | Executes SQL code (like DatasourcePool::exec()) on the DB connection without any variable binding and dedicates a connection to the calling thread. | |
| Y | Executes SQL code on the DB connection, taking a list for all bind arguments. Dedicates a connection to the calling thread. |
| Y | Executes a select statement on the server and returns the results in a hash (column names) of lists (rows). |
| Y | Executes a select statement on the server and returns the results in a hash (column names) of lists (rows), taking a list for all bind arguments. |
| Y | Executes a select statement on the server and returns the first row as a hash (column names and values). |
| Y | Executes a select statement on the server and returns the first row as a hash (column names and values), taking a list for all bind arguments. |
| Y | Executes a select statement on the server and returns the results in a list (rows) of hashes (column names and values). |
| Y | Executes a select statement on the server and returns the results in a list (rows) of hashes (column names and values), taking a list for all bind arguments. |
Y | Manually allocates a persistent connection to the calling thread. | |
N | Returns the username parameter as a string or NOTHING if none is set. | |
N | Returns the password parameter as a string or NOTHING if none is set. | |
N | Returns the dbname parameter as a string or NOTHING if none is set. | |
N | Returns the DBI driver specific charset name for the current connection as a string or NOTHING if none is set. | |
N | Returns the Qore charset name for the current connection as a string or NOTHING if none is set. | |
N | Returns the hostname parameter as a string or NOTHING if none is set. | |
N | Returns the port parameter as an integer or NOTHING if none is set. | |
N | Returns the name of the database driver used for this object as a string. | |
Y | Returns the driver-specific server version data for the current connection. | |
Y | Returns the driver-specific client library version data. Not implemented by all drivers. | |
N | Returns |
Creates a DatasourcePool object. The constructor taking separate arguments requires the database driver name as the first argument, and normally the dbname; the port number is supplied as the final parameter because support for the port number as a connection parameter was added after this class was already present in Qore. The constructor variant taking a hash accepts a hash as produced from parseDatasource().
DatasourcePool::constructor(string$driver, string$user = "", string$pass = "", string$dbname = "", string$encoding = "", string$host = "", softint$min = DP_MIN, softint$max = DP_MAX, softint$port = 0)
# open a Datasource pool to a PostgreSQL database, username="user", password="pass", dbname="database"
# use "utf8" for the character encoding for the connection, hostname="localhost", port=5432
# minimum 5 connections (opened immediately), with a maximum of 20
my DatasourcePool $pool(DSPGSQL, "user", "pass", "database", "utf8", "localhost", 5, 20, 5432);# same as above but using a hash argument with parseDatasource()my DatasourcePool $pool(parseDatasource("pgsql:user/pass@database(utf8)%localhost:5432{min=5,max=20}"));
Table 4.631. Arguments for DatasourcePool::constructor(string $driver, string $user = "", string $pass = "", $dbname = "", string $encoding = "", string $host = "", softint $min = DP_MIN, softint $max = DP_MAX, softint $port = 0) Variant
Argument | Description |
|---|---|
| The name of the DBI driver for the DatasourcePool. See SQL Constants for builtin constants for DBI drivers shipped with Qore, or see the DBI driver documentation to use an add-on driver. |
| The user name for the new pool. |
| The password for the new pool. |
| The database name for the new pool. |
| The database-specific name of the character encoding to use for the new pool. If no value is passed for this parameter, then the database character encoding corresponding to the default character encoding for the Qore process will be used instead. |
| The host name for the new pool. |
| The minimum number of connections for the new pool; this number of connections will be opened immediately when the pool is created. |
| The maximum number of connections for the new pool; must be greater than or equal to |
| The port number for the new pool. |
Table 4.632. Arguments for DatasourcePool::constructor(hash $params) Variant
Argument | Description |
|---|---|
| A hash of parameters for the DatasourcePool; see DatasourcePool Constructor Hash for more information. |
Table 4.633. DatasourcePool Constructor Hash
Key | Type | Description |
|---|---|---|
| The name of the database driver to use; this key is mandatory; if not present, an exception will be raised. See SQL Constants for builtin constants for DBI drivers shipped with Qore, or see the DBI driver documentation to use an add-on driver. | |
| The user name for the new connection. Also see Datasource::setUserName() for a method that allows this parameter to be set after the constructor. | |
| The password for the new connection. Also see Datasource::setPassword() for a method that allows this parameter to be set after the constructor. | |
| The database name for the new connection. Also see Datasource::setDBName() for a method that allows this parameter to be set after the constructor. | |
| The database-specific name of the character encoding to use for the new connection. Also see Datasource::setDBCharset() for a method that allows this parameter to be set after the constructor. If no value is passed for this parameter, then the database character encoding corresponding to the default character encoding for the Qore process will be used instead. | |
| The host name for the new connection. Also see Datasource::setHostName() for a method that allows this parameter to be set after the constructor. | |
| The port number for the new connection. Also see Datasource::setPort() for a method that allows this parameter to be set after the constructor. If this key is present and is 0 then an exception will be raised. | |
| A hash where the |
Table 4.634. Exceptions Thrown by DatasourcePool::constructor()
err | desc |
|---|---|
| Missing DBI driver, negative number of connections specified, or max < min. |
| Could not load a driver for the database identified. |
Throws an exception if any transactions are in progress and returns immediately. The object is destroyed after any in-progress requests are completed.
delete $pool;Table 4.635. Exceptions Thrown by DatasourcePool::destructor()
err | desc |
|---|---|
| The destructor was called while a transaction was still in progress. |
Thows an exception; DatasourcePool objects cannot be copied at the moment.
Manually allocates a persistent connection from the pool to the calling thread. This method should be called when a transaction will be started with a DatasourcePool::select() method (or vselect*, etc).
$pool.beginTransaction();
Commits the current transaction and releases the connection to the pool.
$pool.commit();
Table 4.636. Exceptions Thrown by DatasourcePool::commit()
err | desc |
|---|---|
depends on DBI driver | See documentation for the DBI driver for driver-specific exceptions. |
Returns True if there is a Datasource from the pool currently allocated to the calling thread, False if not.
my bool $in_trans = $pool.inTransaction();Table 4.637. Return Value for DatasourcePool::inTransaction()
Return Type | Description |
|---|---|
Returns |
Rolls back the current transaction and releases the connection to the pool.
$pool.rollback();
Table 4.638. Exceptions Thrown by DatasourcePool::rollback()
err | desc |
|---|---|
depends on DBI driver | See documentation for the DBI driver for driver-specific exceptions. |
Allocates a persistent connection to the calling thread, executes an SQL command on the server and returns either the row count (for example, for updates and inserts) or the data retrieved (for example, if a stored procedure is executed that returns values). This method takes a special syntax for binding values and placeholders; see the Datasource::exec() method for more information.
anyDatasourcePool::exec(string$sql, ...)
$rows = $pool.exec("insert into table (varchar_col, timestamp_col, blob_col, numeric_col)
values (%v, %v, %v, %d)", $string, now(), $binary, 100);Table 4.639. Arguments for DatasourcePool::exec()
Argument | Description |
|---|---|
| The SQL command to execute on the server. |
| Include any values to be bound (using |
Table 4.640. Return Values for DatasourcePool::exec()
Return Type | Description |
|---|---|
The return value depends on the DBI driver; normally, for commands with placeholders, a hash is returned holding the values acquired from executing the SQL statement. For all other commands, normally an int row count is returned. However, some DBI drivers also allow select statements to be executed through this interface, which would also return a hash of list. |
Table 4.641. Exceptions Thrown by DatasourcePool::exec()
err | desc |
|---|---|
depends on DBI driver | See documentation for the DBI driver for driver-specific exceptions. |
Grabs the transaction lock (if autocommit is disabled) and executes an SQL command on the server and returns either the row count (for example, for updates and inserts) or the data retrieved (for example, if a stored procedure is executed that returns values).
This method does not do any variable binding, so it's useful for example for DDL statements etc.
Using this method for OLTP statements can affect the application performance. See used DB server documentation for variable binding.
$rows = $pool.execRaw("create table my_tab (id number, some_text varchar2(30))");Table 4.642. Arguments for DatasourcePool::execRaw()
Argument | Description |
|---|---|
| The SQL command to execute on the server. |
Table 4.644. Exceptions Thrown by DatasourcePool::execRaw()
err | desc |
|---|---|
| Timeout trying to acquire the transaction lock. |
depends on DBI driver | See documentation for the DBI driver for driver-specific exceptions. |
Same as the DatasourcePool::exec() method, except this method takes a single argument after the SQL command giving the list of bind parameters.
This method allocates a persistent connection from the pool to the calling thread.
anyDatasourcePool::vexec(string$sql, list$args)
$rows = $pool.vexec("insert into example_table value (%v, %v, %v)", $arg_list);Table 4.646. Return Values for DatasourcePool::vexec()
Return Type | Description |
|---|---|
Integer or Hash | For commands with placeholders, a hash is returned holding the values acquired from executing the SQL statement. For all other commands, a row count is returned. |
Table 4.647. Exceptions Thrown by DatasourcePool::vexec()
err | desc |
|---|---|
The return value depends on the DBI driver; normally, for commands with placeholders, a hash is returned holding the values acquired from executing the SQL statement. For all other commands, normally an int row count is returned. However, some DBI drivers also allow select statements to be executed through this interface, which would also return a hash of list. |
Executes an SQL select statement on the server and returns the result as a hash (column names) of lists (rows). See Datasource::select() for more information.
Note that this method does not allocate a persistent connection to the calling thread; if the calling thread does not already have a connection allocated to it, a temporary connection will be allocated for executing the select query and returning the results; the temporary connection will be immediately returned to the pool when this method returns. It is an error to execute transaction relevant commands with this method without first allocating a persistent connection with DatasourcePool::exec(), DatasourcePool::vexec(), DatasourcePool::execRaw(), or DatasourcePool::beginTransaction().
anyDatasourcePool::select(string$sql, ...)
# bind a string and a date/time value by value in a query
$query = $pool.select("select * from table where varchar_column = %v and timestamp_column > %v",
$string, 2007-10-11T15:31:26.289);Table 4.648. Arguments for DatasourcePool::select()
Argument | Description |
|---|---|
| The SQL select command to execute on the server. |
| Include any values to be bound (using |
Table 4.650. Exceptions Thrown by DatasourcePool::select()
err | desc |
|---|---|
depends on DBI driver | See documentation for the DBI driver for driver-specific exceptions. |
Same as the DatasourcePool::select() method, except this method takes a single argument after the SQL command giving the list of bind value parameters.
Note that this method does not allocate a persistent connection to the calling thread; if the calling thread does not already have a connection allocated to it, a temporary connection will be allocated for executing the select query and returning the results; the temporary connection will be immediately returned to the pool when this method returns. It is an error to execute transaction relevant commands with this method without first allocating a persistent connection with DatasourcePool::exec(), DatasourcePool::vexec(), DatasourcePool::execRaw(), or DatasourcePool::beginTransaction().
anyDatasourcePool::vselect(string$sql, list$args)
$query = $pool.vselect("select * from example_table where id = %v and name = %v", $arg_list);Table 4.653. Exceptions Thrown by DatasourcePool::vselect()
err | desc |
|---|---|
depends on DBI driver | See documentation for the DBI driver for driver-specific exceptions. |
Executes an SQL select statement on the server and returns the first row as a hash (the column values). If more than one row is returned, then all but the first row are discarded. For a similar method taking a list for all bind arguments, see DatasourcePool::vselectRow().
This method also accepts all bind parameters (%d, %v, etc) as documented in Datasource Binding.
Note that this method does not allocate a persistent connection to the calling thread; if the calling thread does not already have a connection allocated to it, a temporary connection will be allocated for executing the select query and returning the results; the temporary connection will be immediately returned to the pool when this method returns. It is an error to execute transaction relevant commands with this method without first allocating a persistent connection with DatasourcePool::exec(), DatasourcePool::vexec(), DatasourcePool::execRaw(), or DatasourcePool::beginTransaction().
anyDatasourcePool::selectRow(string$sql, ...)
$list = $pool.selectRow("select * from example_table");Table 4.654. Arguments for DatasourcePool::selectRow()
Argument | Description |
|---|---|
| The SQL select command to execute on the server. |
| Include any values to be bound (using |
Table 4.656. Exceptions Thrown by DatasourcePool::selectRow()
err | desc |
|---|---|
depends on DBI driver | See documentation for the DBI driver for driver-specific exceptions. |
Same as the DatasourcePool::selectRow() method, except this method takes a single argument after the SQL command giving the list of bind value parameters.
Note that this method does not allocate a persistent connection to the calling thread; if the calling thread does not already have a connection allocated to it, a temporary connection will be allocated for executing the select query and returning the results; the temporary connection will be immediately returned to the pool when this method returns. It is an error to execute transaction relevant commands with this method without first allocating a persistent connection with DatasourcePool::exec(), DatasourcePool::vexec(), DatasourcePool::execRaw(), or DatasourcePool::beginTransaction().
anyDatasourcePool::vselectRow(string$sql, list$args)
$list = $pool.vselectRow("select * from example_table where id = %v and name = %v", $arg_list);Executes an SQL select statement on the server and returns the result as a list (rows) of hashes (the column values). This format is not as efficient as that returned by the DatasourcePool::select() method, therefore for larger amounts of data, it is recommended to use DatasourcePool::select().
This method also accepts all bind parameters (%d, %v, etc) as documented in Datasource Binding.
Note that this method does not allocate a persistent connection to the calling thread; if the calling thread does not already have a connection allocated to it, a temporary connection will be allocated for executing the select query and returning the results; the temporary connection will be immediately returned to the pool when this method returns. It is an error to execute transaction relevant commands with this method without first allocating a persistent connection with DatasourcePool::exec(), DatasourcePool::vexec(), DatasourcePool::execRaw(), or DatasourcePool::beginTransaction().
anyDatasourcePool::selectRows(string$sql, ...)
$list = $pool.selectRows("select * from example_table");Table 4.659. Arguments for DatasourcePool::selectRows()
Argument | Description |
|---|---|
| The SQL select command to execute on the server. |
| Include any values to be bound (using |
Table 4.660. Return Values for DatasourcePool::selectRows()
Return Type | Description |
|---|---|
Normally returns a list (rows) of hash (where the keys are the column names of each row) or nothing if no rows are found for the query. However, DBI could return other types; for DBI drivers that allow executing generic SQL through this method, any result sets returns should also be in the format list (rows) of hash. |
Table 4.661. Exceptions Thrown by DatasourcePool::selectRows()
err | desc |
|---|---|
depends on DBI driver | See documentation for the DBI driver for driver-specific exceptions. |
Same as the DatasourcePool::selectRows() method, except this method takes a single argument after the SQL command giving the list of bind value parameters.
Note that this method does not allocate a persistent connection to the calling thread; if the calling thread does not already have a connection allocated to it, a temporary connection will be allocated for executing the select query and returning the results; the temporary connection will be immediately returned to the pool when this method returns. It is an error to execute transaction relevant commands with this method without first allocating a persistent connection with DatasourcePool::exec(), DatasourcePool::vexec(), DatasourcePool::execRaw(), or DatasourcePool::beginTransaction().
anyDatasourcePool::vselectRows(string$sql, list$args)
$list = $pool.vselectRows("select * from example_table where id = %v and name = %v", $arg_list);Table 4.663. Return Values for DatasourcePool::vselectRows()
Return Type | Description |
|---|---|
Normally returns a list (rows) of hash (where the keys are the column names of each row) or nothing if no rows are found for the query. However, DBI could return other types; for DBI drivers that allow executing generic SQL through this method, any result sets returns should also be in the format list (rows) of hash. |
Table 4.664. Exceptions Thrown by DatasourcePool::vselectRows()
err | desc |
|---|---|
depends on DBI driver | See documentation for the DBI driver for driver-specific exceptions. |
Retrieves the username parameter for all connections in this pool.
my *string $str = $pool.getUserName();Retrieves the password connection parameter.
my *string $str = $pool.getPassword();Retrieves the database name connection parameter if any is set.
my *string $str = $pool.getDBName();Retrieves the database-specific charset set encoding for the current connection.
my *string $str = $pool.getDBCharset();Retrieves the Qore charset set encoding for the current connection.
my *string $str = $pool.getOSCharset();Table 4.669. Return Values for DatasourcePool::getOSCharset()
Return Type | Description |
|---|---|
Retrieves the Qore charset set encoding for the current connection or |
Retrieves the hostname connection parameter.
my *string $str = $pool.getHostName();Retrieves the port connection parameter.
my *int $port = $pool.getPort();Table 4.671. Return Values for DatasourcePool::getPort()
Return Type | Description |
|---|---|
Retrieves the port connection parameter or no value if no port is set. |
Returns the name of the DBI driver used for this object.
$name = $pool.getDriverName();
Table 4.672. Return Values for DatasourcePool::getDriverName()
Return Type | Description |
|---|---|
The name of the database driver used for this object. |
Retrieves the driver-specific server version information for the current connection.
$data = $pool.getServerVersion();
Table 4.673. Return Values for DatasourcePool::getServerVersion()
Return Type | Description |
|---|---|
The return type and value depends on the driver; see the documentation for the DBI driver used for the connection. |
Retrieves the driver-specific client library version information. Not implemented for all drivers.
$data = $pool.getClientVersion();
Table 4.674. Return Values for DatasourcePool::getClientVersion()
Return Type | Description |
|---|---|
The return type and value depends on the driver; see the documentation for the DBI driver used for the connection. |
There are no comments yet