ConnectionPool
public class ConnectionPool
A pool of re-usable, interchangeable Connection instances.
Using a ConnectionPool allows an application to acquire an existing Connection from the
pool, use that connection to perform one or more SQL statements, and then release it for use
elsewhere in the application.
The ConnectionPoolConfiguration used to create a ConnectionPool specifies the number of
connections in the pool, how long a request for a connection will wait for a connection to
become available, and other characteristics of the pool.
All connections in a ConnectionPool are created from the same ConnectionConfiguration.
They also have the same ConnectionDelegate (if a delegate is specified). Consequently
any connection in a pool is interchangeable with any other.
Use ConnectionPool.acquireConnection(completionHandler:) to request a connection from a
ConnectionPool. This method is non-blocking: its completion handler is asynchronously
executed when a connection is successfully allocated to the request or if an error occurs.
To release the connection back to the pool, call ConnectionPool.releaseConnection(_:).
Alternately, use ConnectionPool.withConnection(completionHandler:) to acquire a connection
that is automatically released after execution of the completion handler.
When a connection is released to a ConnectionPool, there should be no transaction underway.
Pair each SQL BEGIN command with either a COMMIT or ROLLBACK command (or equivalently,
use the Connection.beginTransaction(), Connection.commitTransaction(), and
Connection.rollbackTransaction() methods).
In general, do not close a Connection acquired from a ConnectionPool. If a connection is
closed (whether explicitly or because of an unrecoverable error) then, when that connection is
released, it will be discarded from the pool, allowing a new connection to be created and added
to the pool.
The ConnectionPool class is threadsafe: multiple threads may concurrently operate against a
ConnectionPool instance. Connections acquired from the pool are subject to the threadsafety
constraints described by the API documentation for Connection.
-
Creates a
ConnectionPool.Declaration
Swift
public init(connectionPoolConfiguration: ConnectionPoolConfiguration, connectionConfiguration: ConnectionConfiguration, connectionDelegate: ConnectionDelegate? = nil)Parameters
connectionPoolConfigurationthe configuration for the
ConnectionPoolconnectionConfigurationthe configuration for
Connectioninstances in the poolconnectionDelegatean optional delegate for the
Connectioninstances -
The configuration of this
ConnectionPool.The
ConnectionPoolConfigurationis mutable. Configuration changes take effect for subsequent requests for connections.Declaration
Swift
public var connectionPoolConfiguration: ConnectionPoolConfiguration { get set } -
The configuration of
Connectioninstances in thisConnectionPool.Declaration
Swift
public let connectionConfiguration: ConnectionConfiguration -
An optional delegate for
Connectioninstances in thisConnectionPool.Declaration
Swift
public let connectionDelegate: ConnectionDelegate? -
Requests a
Connectionfrom thisConnectionPool.This method is non-blocking: its completion handler is asynchronously executed when a connection is successfully allocated or an error occurs.
ConnectionPoolConfiguration.dispatchQueuecontrols theDispatchQueueon which the completion handler is executed.If the request backlog is too large (
ConnectionPoolConfiguration.maximumPendingRequests), the request will fail withPostgresError.tooManyRequestsForConnections.If a connection is available, it will be allocated to the request and passed to the completion handler. Otherwise, the request will join the backlog of pending requests. As connections become available, they are allocated to the pending requests in the order those requests were received.
If a connection is not allocated to the request before it times out (
ConnectionPoolConfiguration.pendingRequestTimeout), the request will fail withPostgresError.timedOutAcquiringConnection.When a connection is successfully allocated to the request, the completion handler (or any code it triggers) can use that connection to perform one or more SQL statements. When finished with the connection, call
releaseConnection(_:)to release it back to thisConnectionPool. IfreleaseConnection(_:)is not called before the allocated connection times out (ConnectionPoolConfiguration.allocatedConnectionTimeout), the connection will be forcibly closed and removed from theConnectionPool.Example:
connectionPool.acquireConnection { result in do { let connection = try result.get() defer { connectionPool.releaseConnection(connection) } let statement = try connection.prepareStatement(text: ...) ... } catch { ... } }Declaration
Swift
public func acquireConnection( completionHandler: @escaping (Result<Connection, Error>) -> Void)Parameters
completionHandlera completion handler, passed a value that either indicates success (with an associated
Connection) or failure (with an associatedError) -
Releases a
Connectionback to thisConnectionPool.Each
Connectionacquired by callingacquireConnection(completionHandler:)should be released exactly once. After invoking this method, do not further operate on theConnectioninstance.Declaration
Swift
public func releaseConnection(_ connection: Connection)Parameters
connectionthe
Connectionto release -
Requests a
Connectionfrom thisConnectionPool, automatically releasing it after executing the specified completion handler.This method operates identically to
acquireConnection(completionHandler:), except that the acquired connection is automatically released after executing the completion handler.Do not call
releaseConnection(_:)on theConnectionpassed to the completion handler.Example:
connectionPool.withConnection { result in do { let connection = try result.get() let statement = try connection.prepareStatement(text: ...) ... } catch { ... } }Declaration
Swift
public func withConnection(completionHandler: @escaping (Result<Connection, Error>) -> Void)Parameters
completionHandlera completion handler, passed a value that either indicates success (with an associated
Connection) or failure (with an associatedError) -
Gets performance metrics for this
ConnectionPool.The returned metrics describe
ConnectionPoolperformance for the period starting when the metrics were last reset (or when thisConnectionPoolwas initialized, if the metrics have never been reset) and ending at the current moment in time.Performance metrics can also be periodically logged. See
ConnectionPoolConfiguration.metricsLoggingInterval.Declaration
Swift
@discardableResult public func computeMetrics(reset: Bool) -> ConnectionPoolMetricsParameters
resetwhether to reset the metrics
Return Value
the metrics
-
Whether this
ConnectionPoolis closed.Declaration
Swift
public var isClosed: Bool { get } -
Closes this
ConnectionPool.Any pending requests for connections are canceled.
If
forceistrue, allConnectioninstances in thisConnectionPoolare immediately forcibly closed.If
forceis false, only the unallocatedConnectioninstances are immediately closed. The connections currently allocated to requests will be closed as those requests complete and the connections are released.Has no effect if this
ConnectionPoolis already closed.Declaration
Swift
public func close(force: Bool = false)Parameters
forcewhether to immediately forcibly close
Connectioninstances currently allocated to requests