Class ConnectionPool
- All Implemented Interfaces:
AutoCloseable
Connections, keyed by
DatabaseConnectionConfig, so repeated scans and queries against the
same connection avoid the cost of opening a fresh physical connection each time.
Usage is borrow/release: borrow(com.darkcollective.relix.lang.ast.ConnectionDeclaration) hands out a validated idle
connection (or opens a new one), and release(com.darkcollective.relix.lang.ast.ConnectionDeclaration, java.sql.Connection) returns it for reuse. A
released connection that is broken — or that would exceed the per-config idle
bound — is closed rather than pooled. close() closes every idle
connection; it should be called once the owning connector is done.
The pool itself never closes a borrowed connection: the caller decides via
release(com.darkcollective.relix.lang.ast.ConnectionDeclaration, java.sql.Connection) (reuse) versus closing it directly (e.g. after a setup
failure, where the connection may be in an unknown state).
Fresh connections come from a ConnectionProvider, so what a
connection is stays out of the pool. A provider that
manages its own pooling is not
pooled again on top: those connections are opened on demand and closed on
release, which is what returns them to the pool that actually owns them. The
idle map is keyed by the JDBC coordinates rather than by the declaration, so
two declarations naming the same database still share, exactly as before — and
a provider-pooled connection needs no key at all, since nothing is retained.
Thread safety
Backed by concurrent collections and safe for concurrent borrow/release.
The per-config idle bound is a soft limit (a brief concurrent overshoot is
harmless). close() is intended to run once, after execution has
finished using the pool.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a pool with the default per-config idle bound (8), opening fresh connections fromConnectionProvider.FROM_URL.ConnectionPool(int maxIdlePerConfig) Creates a pool keeping at mostmaxIdlePerConfigidle connections per distinct connection config.ConnectionPool(int maxIdlePerConfig, ConnectionProvider provider) Creates a pool keeping at mostmaxIdlePerConfigidle connections per distinct connection config, opening fresh ones fromprovider.ConnectionPool(ConnectionProvider provider) Creates a pool with the default idle bound, opening fresh connections fromprovider. -
Method Summary
Modifier and TypeMethodDescriptionborrow(ConnectionDeclaration connection) Borrows a usable connection forconnection: a validated idle one if available, otherwise a freshly opened one from the provider.voidclose()Closes every idle connection.voidrelease(ConnectionDeclaration connection, Connection conn) Returns a connection for reuse.
-
Constructor Details
-
ConnectionPool
public ConnectionPool()Creates a pool with the default per-config idle bound (8), opening fresh connections fromConnectionProvider.FROM_URL. -
ConnectionPool
Creates a pool with the default idle bound, opening fresh connections fromprovider.- Parameters:
provider- the source of new connections; must not be null
-
ConnectionPool
public ConnectionPool(int maxIdlePerConfig) Creates a pool keeping at mostmaxIdlePerConfigidle connections per distinct connection config.- Parameters:
maxIdlePerConfig- the per-config idle bound; must be positive
-
ConnectionPool
Creates a pool keeping at mostmaxIdlePerConfigidle connections per distinct connection config, opening fresh ones fromprovider.- Parameters:
maxIdlePerConfig- the per-config idle bound; must be positiveprovider- the source of new connections; must not be null
-
-
Method Details
-
borrow
Borrows a usable connection forconnection: a validated idle one if available, otherwise a freshly opened one from the provider.When the provider manages its own pooling for this declaration, nothing idle is consulted and a connection is opened on demand.
- Parameters:
connection- the declared connection; must not be null- Returns:
- an open connection the caller must
release(com.darkcollective.relix.lang.ast.ConnectionDeclaration, java.sql.Connection)or close - Throws:
SQLException- if the pool is closed or a new connection cannot be opened
-
release
Returns a connection for reuse. A broken connection, or one beyond the idle bound, is closed instead of pooled. Passingnullis a no-op.When the provider manages its own pooling for this declaration the connection is closed rather than retained — that is what returns it to the pool that owns it.
- Parameters:
connection- the declaration the connection belongs toconn- the connection to release; may be null
-
close
public void close()Closes every idle connection. Borrowed connections are unaffected.- Specified by:
closein interfaceAutoCloseable
-