Class PoolConnectionProvider
- java.lang.Object
-
- org.linuxforhealth.fhir.database.utils.pool.PoolConnectionProvider
-
- All Implemented Interfaces:
IConnectionProvider
public class PoolConnectionProvider extends java.lang.Object implements IConnectionProvider
Provides a simple connection pool with some thread-local behavior so that requests for a connection within the same thread will get the same connection back. Cooperates with the TransactionFactory to provide a JEE-flavored experience, and makes it easy to write code which will work in both J2SE and JEE environments. Does not support distributed transactions.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) intallocated
-
Constructor Summary
Constructors Constructor Description PoolConnectionProvider(IConnectionProvider cp, int maxPoolSize)Public constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancheckConnectionFailure(java.sql.SQLException x)Simple check to see if the exception is related to a connection error, in which case the connection shouldn't be returned to the pool when closedprotected voidclearActiveConnection()Remove the active connection on this thread.voidclose()The caller is telling us they no longer need to use the pool so we can free any internal resources.voidcommitTransaction()Commit the current transaction if there's an actual connection associated with this thread (i.e.protected voidconfigureConnection(java.sql.Connection c)Apply an configuration steps to a new connectionvoiddescribe(java.lang.String prefix, java.lang.StringBuilder cfg, java.lang.String key)Describe self, for writing configuration information to log file for test record purposesjava.sql.ConnectiongetConnection()Obtain a new connection to a data-source which this provider has been configured for.intgetPoolSize()gets the pool size for the pooled connectionIDatabaseTranslatorgetTranslator()Get the translator associated with this connection provider.protected voidreturnConnection(PooledConnection pc, boolean reuse)Called when the connection is closed, which in this case is pretty much a NOP, because this connection will stay active on this thread until the transaction commitsvoidrollbackTransaction()Roll back the transaction for the connection associated with this thread, if one existsvoidsetCloseOnAnyError()Configure the pool to close connections after an error instead of returning them to the pool.voidsetNewConnectionHandler(java.util.function.Consumer<java.sql.Connection> handler)Setter for the newConnectionHandler
-
-
-
Constructor Detail
-
PoolConnectionProvider
public PoolConnectionProvider(IConnectionProvider cp, int maxPoolSize)
Public constructor- Parameters:
cp-maxPoolSize-
-
-
Method Detail
-
setCloseOnAnyError
public void setCloseOnAnyError()
Configure the pool to close connections after an error instead of returning them to the pool. Exceptions related to connection errors will always mark the connection as no longer usable.
-
setNewConnectionHandler
public void setNewConnectionHandler(java.util.function.Consumer<java.sql.Connection> handler)
Setter for the newConnectionHandler- Parameters:
handler-
-
configureConnection
protected void configureConnection(java.sql.Connection c)
Apply an configuration steps to a new connection- Parameters:
c-
-
getConnection
public java.sql.Connection getConnection() throws java.sql.SQLExceptionDescription copied from interface:IConnectionProviderObtain a new connection to a data-source which this provider has been configured for.- Specified by:
getConnectionin interfaceIConnectionProvider- Returns:
- Throws:
java.sql.SQLException
-
returnConnection
protected void returnConnection(PooledConnection pc, boolean reuse)
Called when the connection is closed, which in this case is pretty much a NOP, because this connection will stay active on this thread until the transaction commits- Parameters:
pc-
-
clearActiveConnection
protected void clearActiveConnection() throws java.sql.SQLExceptionRemove the active connection on this thread. Called when the transaction completes- Throws:
java.sql.SQLException
-
checkConnectionFailure
protected boolean checkConnectionFailure(java.sql.SQLException x)
Simple check to see if the exception is related to a connection error, in which case the connection shouldn't be returned to the pool when closed- Parameters:
x-- Returns:
-
getTranslator
public IDatabaseTranslator getTranslator()
Description copied from interface:IConnectionProviderGet the translator associated with this connection provider. Supports interpretation of SQLExceptions and the ability to tweak SQL statements to handle differences between databases- Specified by:
getTranslatorin interfaceIConnectionProvider- Returns:
-
commitTransaction
public void commitTransaction() throws java.sql.SQLExceptionDescription copied from interface:IConnectionProviderCommit the current transaction if there's an actual connection associated with this thread (i.e. if getConnection() has been called at least once), otherwise do nothing.- Specified by:
commitTransactionin interfaceIConnectionProvider- Throws:
java.sql.SQLException
-
rollbackTransaction
public void rollbackTransaction() throws java.sql.SQLExceptionDescription copied from interface:IConnectionProviderRoll back the transaction for the connection associated with this thread, if one exists- Specified by:
rollbackTransactionin interfaceIConnectionProvider- Throws:
java.sql.SQLException
-
describe
public void describe(java.lang.String prefix, java.lang.StringBuilder cfg, java.lang.String key)Description copied from interface:IConnectionProviderDescribe self, for writing configuration information to log file for test record purposes- Specified by:
describein interfaceIConnectionProvider
-
getPoolSize
public int getPoolSize()
gets the pool size for the pooled connection- Specified by:
getPoolSizein interfaceIConnectionProvider
-
close
public void close()
The caller is telling us they no longer need to use the pool so we can free any internal resources. This also let's us check for anything currently in-use that shouldn't be- Throws:
java.lang.IllegalStateException- if there are open connections or a transaction is active.
-
-