Package com.blackrook.sql
Class SQLPool
java.lang.Object
com.blackrook.sql.SQLPool
- All Implemented Interfaces:
AutoCloseable
This is a database connection pool class for a bunch of shared, managed connections.
Meant to be accessed by many threads in an enterprise setting.
If a connection is requested that is not available, the requesting thread will wait
until a connection is found or until it times out.
- Author:
- Matthew Tropiano
-
Constructor Summary
ConstructorDescriptionSQLPool
(SQLConnector connector, int connectionCount) Creates a new connection pool from aSQLConnector
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes all open connections in the pool.Retrieves an available connection from the pool.getAvailableConnection
(long waitMillis) Retrieves an available connection from the pool.int
Gets the number of available connections.void
getConnectionAnd
(long waitMillis, SQLConnectionConsumer handler) Retrieves a connection from this pool, passes it to the providedSQLConnectionConsumer
function, calls it, then returns it to the pool.<R> R
getConnectionAnd
(long waitMillis, SQLConnectionFunction<R> handler) Retrieves a connection from this pool, passes it to the providedSQLConnectionFunction
, calls it, returns it to the pool, and returns the result.void
getConnectionAnd
(SQLConnectionConsumer handler) Retrieves a connection from this pool, passes it to the providedSQLConnectionConsumer
function, then returns it to the pool.<R> R
getConnectionAnd
(SQLConnectionFunction<R> handler) Retrieves a connection from this pool, passes it to the providedSQLConnectionFunction
, calls it, returns it to the pool, and returns the result.int
Gets the number of total connections.int
Gets the number of connections in use.void
releaseConnection
(SQLConnection connection) Releases a connection back to the pool.
-
Constructor Details
-
SQLPool
Creates a new connection pool from aSQLConnector
.- Parameters:
connector
- the connector to use.connectionCount
- the number of connections to pool.- Throws:
SQLException
- if a connection cannot be established.
-
-
Method Details
-
getConnectionAnd
public void getConnectionAnd(SQLConnectionConsumer handler) throws InterruptedException, SQLException Retrieves a connection from this pool, passes it to the providedSQLConnectionConsumer
function, then returns it to the pool.- Parameters:
handler
- the consumer function that accepts the retrieved connection.- Throws:
InterruptedException
- if an interrupt is thrown by the current thread waiting for an available connection.SQLException
- if a connection cannot be re-created or re-established.
-
getConnectionAnd
public void getConnectionAnd(long waitMillis, SQLConnectionConsumer handler) throws InterruptedException, TimeoutException, SQLException Retrieves a connection from this pool, passes it to the providedSQLConnectionConsumer
function, calls it, then returns it to the pool.- Parameters:
waitMillis
- the amount of time (in milliseconds) to wait for a connection.handler
- the consumer function that accepts the retrieved connection.- Throws:
InterruptedException
- if an interrupt is thrown by the current thread waiting for an available connection.TimeoutException
- if the wait lapses and there are no available connections.SQLException
- if a connection cannot be re-created or re-established.
-
getConnectionAnd
public <R> R getConnectionAnd(SQLConnectionFunction<R> handler) throws InterruptedException, SQLException Retrieves a connection from this pool, passes it to the providedSQLConnectionFunction
, calls it, returns it to the pool, and returns the result.- Type Parameters:
R
- the return type.- Parameters:
handler
- the consumer function that accepts the retrieved connection and returns a value.- Returns:
- the return value of the handler function.
- Throws:
InterruptedException
- if an interrupt is thrown by the current thread waiting for an available connection.SQLException
- if a connection cannot be re-created or re-established.
-
getConnectionAnd
public <R> R getConnectionAnd(long waitMillis, SQLConnectionFunction<R> handler) throws InterruptedException, TimeoutException, SQLException Retrieves a connection from this pool, passes it to the providedSQLConnectionFunction
, calls it, returns it to the pool, and returns the result.- Type Parameters:
R
- the return type.- Parameters:
waitMillis
- the amount of time (in milliseconds) to wait for a connection.handler
- the consumer function that accepts the retrieved connection and returns a value.- Returns:
- the return value of the handler function.
- Throws:
InterruptedException
- if an interrupt is thrown by the current thread waiting for an available connection.TimeoutException
- if the wait lapses and there are no available connections.SQLException
- if a connection cannot be re-created or re-established.
-
getAvailableConnection
Retrieves an available connection from the pool.- Returns:
- a connection to use.
- Throws:
InterruptedException
- if an interrupt is thrown by the current thread waiting for an available connection.SQLException
- if a connection cannot be re-created or re-established.
-
getAvailableConnection
public SQLConnection getAvailableConnection(long waitMillis) throws InterruptedException, TimeoutException, SQLException Retrieves an available connection from the pool.- Parameters:
waitMillis
- the amount of time (in milliseconds) to wait for a connection.- Returns:
- a connection to use.
- Throws:
InterruptedException
- if an interrupt is thrown by the current thread waiting for an available connection.TimeoutException
- if the wait lapses and there are no available connections.SQLException
- if a connection cannot be re-created or re-established.
-
getAvailableConnectionCount
public int getAvailableConnectionCount()Gets the number of available connections.- Returns:
- the amount of connections currently used.
-
getUsedConnectionCount
public int getUsedConnectionCount()Gets the number of connections in use.- Returns:
- the amount of connections currently used.
-
getTotalConnectionCount
public int getTotalConnectionCount()Gets the number of total connections.- Returns:
- the total amount of managed connections.
-
releaseConnection
Releases a connection back to the pool. Also cancels a transaction that it may still be in, if any.- Parameters:
connection
- the connection to release.
-
close
public void close()Closes all open connections in the pool.- Specified by:
close
in interfaceAutoCloseable
-