Class DatabaseConnection
- java.lang.Object
-
- com.adaptris.core.AdaptrisConnectionImp
-
- com.adaptris.core.AllowsRetriesConnection
-
- com.adaptris.core.jdbc.DatabaseConnection
-
- All Implemented Interfaces:
AdaptrisComponent
,AdaptrisConnection
,ComponentLifecycle
,ComponentLifecycleExtension
,StateManagedComponent
- Direct Known Subclasses:
FailoverJdbcConnection
,JdbcConnection
,JdbcPooledConnectionImpl
public abstract class DatabaseConnection extends AllowsRetriesConnection
Abstract class containing configuration for JDBC Connection classes.
-
-
Field Summary
-
Fields inherited from class com.adaptris.core.AdaptrisConnectionImp
log
-
-
Constructor Summary
Constructors Constructor Description DatabaseConnection()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
alwaysValidateConnection()
javax.sql.DataSource
asDataSource()
Expose this DatabaseConnection as a DataSourceboolean
autoCommit()
protected void
checkInternalState()
protected void
closeConnection()
Close the underlying connection.protected abstract void
closeDatabaseConnection()
Initialise the underlying database connection.java.sql.Connection
connect()
Returns the underlyingSQLConnection
.protected java.util.Properties
connectionProperties()
boolean
debugMode()
abstract boolean
equals(java.lang.Object o)
Force implementations to over-ride equals with a semantic implementation.java.lang.Boolean
getAlwaysValidateConnection()
Returns whether to always validate the database connection.java.lang.Boolean
getAutoCommit()
Returns whether to auto-commit.protected abstract java.lang.String
getConnectionName()
Returns a name for this connection for logging purposes.KeyValuePairSet
getConnectionProperties()
Get any additional connection properties that have been configured.java.lang.Boolean
getDebugMode()
Returns this connection's debug mode.java.lang.String
getDriverImp()
The driver implementation to use.java.lang.String
getPassword()
java.lang.String
getUsername()
abstract int
hashCode()
Force implementations to over-ride hashcode.protected void
initConnection()
Initialisation ensures that the configured driver implementation is available for use.protected abstract void
initialiseDatabaseConnection()
Initialise the underlying database connection.protected static void
loadDriverClass(java.lang.String clazz)
protected abstract java.sql.Connection
makeConnection()
Make the connection.protected void
prepareConnection()
void
setAlwaysValidateConnection(java.lang.Boolean b)
Set whether to always validate the database connection.void
setAutoCommit(java.lang.Boolean b)
Sets whether to auto-commit.void
setConnectionProperties(KeyValuePairSet p)
Set any additional connection properties.void
setDebugMode(java.lang.Boolean dbg)
Set this connection's debug mode.void
setDriverImp(java.lang.String driverImp)
The driver implementation to use.void
setPassword(java.lang.String s)
Set the password used to access the database.void
setUsername(java.lang.String s)
Set the username used to access the database.protected void
startConnection()
Starting this connection means that an initial attempt is made to connect to the database.protected abstract void
startDatabaseConnection()
Initialise the underlying database connection.protected void
stopConnection()
Stop the underlying connection.protected abstract void
stopDatabaseConnection()
Initialise the underlying database connection.-
Methods inherited from class com.adaptris.core.AllowsRetriesConnection
connectionAttempts, connectionRetryInterval, createLoggingStatement, getConnectionAttempts, getConnectionRetryInterval, logWarning, setConnectionAttempts, setConnectionRetryInterval
-
Methods inherited from class com.adaptris.core.AdaptrisConnectionImp
addExceptionListener, addMessageConsumer, addMessageProducer, changeState, cloneForTesting, close, connectionErrorHandler, getConnectionErrorHandler, getUniqueId, getWorkersFirstOnShutdown, init, prepare, requestClose, requestInit, requestStart, requestStop, retrieveComponentState, retrieveConnection, retrieveExceptionListeners, retrieveMessageConsumers, retrieveMessageProducers, setConnectionErrorHandler, setUniqueId, setWorkersFirstOnShutdown, start, stop, workersFirstOnShutdown
-
-
-
-
Method Detail
-
prepareConnection
protected void prepareConnection() throws CoreException
- Specified by:
prepareConnection
in classAdaptrisConnectionImp
- Throws:
CoreException
-
initConnection
protected final void initConnection() throws CoreException
Initialisation ensures that the configured driver implementation is available for use. It does not perform connection verification. This is done by
start
or upon the first call toprepareStatement()
- Specified by:
initConnection
in classAdaptrisConnectionImp
- Throws:
CoreException
- if the driver implementation was not available.
-
loadDriverClass
protected static void loadDriverClass(java.lang.String clazz)
-
startConnection
protected final void startConnection() throws CoreException
Starting this connection means that an initial attempt is made to connect to the database. The connection could previously have been started by a call to
getConnection
- Specified by:
startConnection
in classAdaptrisConnectionImp
- Throws:
CoreException
- if the connection could not be started.- See Also:
AdaptrisConnectionImp.startConnection()
,connect()
-
stopConnection
protected final void stopConnection()
Description copied from class:AdaptrisConnectionImp
Stop the underlying connection.- Specified by:
stopConnection
in classAdaptrisConnectionImp
- See Also:
AdaptrisConnectionImp.stopConnection()
-
closeConnection
protected final void closeConnection()
Description copied from class:AdaptrisConnectionImp
Close the underlying connection.- Specified by:
closeConnection
in classAdaptrisConnectionImp
-
setAutoCommit
public void setAutoCommit(java.lang.Boolean b)
Sets whether to auto-commit.
- Parameters:
b
- whether to auto-commit; default is true.
-
getAutoCommit
public java.lang.Boolean getAutoCommit()
Returns whether to auto-commit.
- Returns:
- whether to auto-commit
-
autoCommit
public boolean autoCommit()
-
setDebugMode
public void setDebugMode(java.lang.Boolean dbg)
Set this connection's debug mode.
In debug mode there is additional logging for reconnection attempts.
- Parameters:
dbg
- this connection's debug mode
-
getDebugMode
public java.lang.Boolean getDebugMode()
Returns this connection's debug mode.
- Returns:
- this connection's debug mode
-
debugMode
public boolean debugMode()
-
asDataSource
public javax.sql.DataSource asDataSource() throws java.sql.SQLException
Expose this DatabaseConnection as a DataSource- Returns:
- a DataSource implementation
- Throws:
java.sql.SQLException
- See Also:
DataSource
-
connect
public final java.sql.Connection connect() throws java.sql.SQLException
Returns the underlying
SQLConnection
.- Returns:
- the underlying
SQLConnection
- Throws:
java.sql.SQLException
- if the connection was not valid, and reconnection failed
-
makeConnection
protected abstract java.sql.Connection makeConnection() throws java.sql.SQLException
Make the connection.
This abstract method should be implemented by concrete sub-classes to make or check the current connection to the Jdbc source.
- Returns:
- a java.sql.Connection
- Throws:
java.sql.SQLException
- if the connection could not be made.- See Also:
connect()
-
initialiseDatabaseConnection
protected abstract void initialiseDatabaseConnection() throws CoreException
Initialise the underlying database connection.
This abstract method should be implemented by concrete sub-classes to initialise any components other than the DriverManager.
- Throws:
CoreException
- wrapping any underlying exception.
-
startDatabaseConnection
protected abstract void startDatabaseConnection() throws CoreException
Initialise the underlying database connection.
This abstract method should be implemented by concrete sub-classes to initialise any components other than the DriverManager.
- Throws:
CoreException
- wrapping any underlying exception.
-
stopDatabaseConnection
protected abstract void stopDatabaseConnection()
Initialise the underlying database connection.
This abstract method should be implemented by concrete sub-classes to stop any components other than the DriverManager.
-
closeDatabaseConnection
protected abstract void closeDatabaseConnection()
Initialise the underlying database connection.
This abstract method should be implemented by concrete sub-classes to close any components.
-
getConnectionName
protected abstract java.lang.String getConnectionName()
Returns a name for this connection for logging purposes.
- Returns:
- a name for this connection for logging purposes
-
checkInternalState
protected void checkInternalState() throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
setAlwaysValidateConnection
public void setAlwaysValidateConnection(java.lang.Boolean b)
Set whether to always validate the database connection.
Validating the connection means that the test-statement is executed every time
connect()
is invoked. Depending on the test statement in question this might have an impact upon performance.- Parameters:
b
- whether to always validate the database connection; defaults to false.
-
getAlwaysValidateConnection
public java.lang.Boolean getAlwaysValidateConnection()
Returns whether to always validate the database connection.
- Returns:
- whether to always validate the database connection
-
alwaysValidateConnection
public boolean alwaysValidateConnection()
-
equals
public abstract boolean equals(java.lang.Object o)
Force implementations to over-ride equals with a semantic implementation.
- Overrides:
equals
in classjava.lang.Object
- See Also:
Object.equals(java.lang.Object)
-
hashCode
public abstract int hashCode()
Force implementations to over-ride hashcode.
- Overrides:
hashCode
in classjava.lang.Object
- See Also:
Object.hashCode()
-
getUsername
public java.lang.String getUsername()
-
setUsername
public void setUsername(java.lang.String s)
Set the username used to access the database.- Parameters:
s
-
-
getPassword
public java.lang.String getPassword()
-
setPassword
public void setPassword(java.lang.String s)
Set the password used to access the database.- Parameters:
s
- the password which might be encoded using an available password scheme fromPassword
-
getConnectionProperties
public KeyValuePairSet getConnectionProperties()
Get any additional connection properties that have been configured.- Returns:
- any additional properties
-
setConnectionProperties
public void setConnectionProperties(KeyValuePairSet p)
Set any additional connection properties.If additional connection properties are set, then any configured username/password will be applied to the connection properties against the key
'user'
and'password'
respectively. If you have configured thos properties in the connection-properties element, then make sure that you do not configuresetUsername(String)
orsetPassword(String)
.- Parameters:
p
- any additional properties over and above username/password.
-
connectionProperties
protected java.util.Properties connectionProperties() throws PasswordException
- Throws:
PasswordException
-
getDriverImp
public java.lang.String getDriverImp()
The driver implementation to use.Later versions of the JDBC standard allow auto-discovery via the SPI and manual loading of the driver class is not necessary. As a result this defaults to 'null' and no driver loading is done.
-
setDriverImp
public void setDriverImp(java.lang.String driverImp)
The driver implementation to use.Later versions of the JDBC standard allow auto-discovery via the SPI and manual loading of the driver class is not necessary. As a result this defaults to 'null' and no driver loading is done.
-
-