IConnectionPool

Pool handles several connections to one or more SQL servers. If connection is lost, pool tries to reconnect over reconnectTime duration. *

Members

Functions

activeConnections
size_t activeConnections()

Returns current alive connections number.

addServer
void addServer(string connString, size_t connNum)

Adds connection string to a SQL server with maximum connections count.

dateFormat
DateFormat dateFormat()

Returns date format used in ONE OF sql servers. Warning: This method can be trusted only the pool conns are connected to the same sql server. TODO: Make a way to get such configs for particular connection.

execTransaction
InputRange!(immutable Bson) execTransaction(string[] commands, string[] params = [], uint[] argnums = [], string[string] vars = null, bool[] oneRowConstraint = [])

Performs several SQL commands on single connection wrapped in a transaction (BEGIN/COMMIT in PostgreSQL). Each command should use '$n' notation to refer params values. Before any command occurs in transaction the local SQL variables is set from vars.

fetchFreeConnection
shared(IConnection) fetchFreeConnection()

Returns first free connection from the pool.

finalize
void finalize()

Awaits all queries to finish and then closes each connection.

freeConnTimeout
Duration freeConnTimeout()

If there is no free connection for specified duration while trying to initialize SQL query, then the pool throws ConnTimeoutException exception.

getTransaction
InputRange!(immutable Bson) getTransaction(immutable ITransaction transaction)

Retrieves SQL result from specified transaction.

inactiveConnections
size_t inactiveConnections()

Returns current frozen connections number.

isTransactionReady
bool isTransactionReady(immutable ITransaction transaction)

Returns true if transaction processing is finished (doesn't matter the actual reason, error or transaction object is invalid, or successful completion).

loggingAllTransactions
void loggingAllTransactions(bool val)

Enables/disables logging for all transactions.

loggingAllTransactions
bool loggingAllTransactions()

Returns true if the pool logs all transactions.

postTransaction
immutable(ITransaction) postTransaction(string[] commands, string[] params = [], uint[] argnums = [], string[string] vars = null, bool[] oneRowConstraint = [])

Asynchronous way to execute transaction. User can check transaction status by calling isTransactionReady method. When isTransactionReady method returns true, the transaction can be finalized by getTransaction method.

reconnectTime
Duration reconnectTime()

If connection to a SQL server is down, the pool tries to reestablish it every time units returned by the method.

timeZone
immutable(TimeZone) timeZone()

Returns server time zone used in ONE OF sql servers. Warning: This method can be trusted only the pool conns are connected to the same sql server. TODO: Make a way to get such configs for particular connection.

timestampFormat
TimestampFormat timestampFormat()

Returns timestamp format used in ONE OF sql servers. Warning: This method can be trusted only the pool conns are connected to the same sql server. TODO: Make a way to get such configs for particular connection.

Interfaces

ITransaction
interface ITransaction

Transaction that should be executed by one of connections on remote SQL server. Client code shouldn't know anything about the interface but only store it as unique key to acquire finished transaction.

Examples

1     scope(exit) pool.finalize();
2     std.stdio.writeln(pool.timeZone); // know server time zone
3 
4     import pgator.db.pq.types.time; // for PGTimeStamp
5 *
6     // blocking quering
7     auto time = pool.execTransaction(["SELECT TIMESTAMP 'epoch' as field;"]).deserializeBson!PGTimeStamp;
8     assert(time == SysTime.fromSimpleString("1970-Jan-01 00:00:00Z"));
9 *
10     // or can use asynchronous alternative
11     auto transaction = pool.postTransaction(["SELECT TIMESTAMP 'epoch' as field;"]);
12     while(!isTransactionReady(transaction))
13     {
14        // do something
15     }
16     time = getTransaction(transaction).deserializeBson!PGTimeStamp;
17     assert(time == SysTime.fromSimpleString("1970-Jan-01 00:00:00Z"));

Meta