public abstract class DatabaseConnection extends AllowsRetriesConnection
Abstract class containing configuration for JDBC Connection classes.
log
Constructor and Description |
---|
DatabaseConnection()
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
boolean |
alwaysValidateConnection() |
javax.sql.DataSource |
asDataSource()
Expose this DatabaseConnection as a DataSource
|
boolean |
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 underlying
SQLConnection . |
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()
Returns the driver implementation to use.
|
java.lang.String |
getPassword() |
java.lang.String |
getTestStatement()
Returns the SQL statement used to test this connection.
|
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 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 s)
Sets the driver implementation to use.
|
void |
setPassword(java.lang.String s)
Set the password used to access the database.
|
void |
setTestStatement(java.lang.String s)
Set the SQL statement used to test this connection.
|
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.
|
connectionAttempts, connectionRetryInterval, createLoggingStatement, getConnectionAttempts, getConnectionRetryInterval, logWarning, setConnectionAttempts, setConnectionRetryInterval
addExceptionListener, addMessageConsumer, addMessageProducer, changeState, cloneForTesting, close, connectionErrorHandler, getConnectionErrorHandler, getLookupName, getUniqueId, getWorkersFirstOnShutdown, init, prepare, requestClose, requestInit, requestStart, requestStop, retrieveComponentState, retrieveConnection, retrieveExceptionListeners, retrieveMessageConsumers, retrieveMessageProducers, setConnectionErrorHandler, setLookupName, setUniqueId, setWorkersFirstOnShutdown, start, stop, workersFirstOnShutdown
public DatabaseConnection()
Creates a new instance. Defaults are auto-commit and MySQL. Always validate is false and the test statement set to
SELECT DATABASE(), VERSION(), NOW(), USER();
.
protected void prepareConnection() throws CoreException
prepareConnection
in class AdaptrisConnectionImp
CoreException
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 to prepareStatement()
initConnection
in class AdaptrisConnectionImp
CoreException
- if the driver implementation was not available.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
startConnection
in class AdaptrisConnectionImp
CoreException
- if the connection could not be started.AdaptrisConnectionImp.startConnection()
,
connect()
protected final void stopConnection()
AdaptrisConnectionImp
stopConnection
in class AdaptrisConnectionImp
AdaptrisConnectionImp.stopConnection()
protected final void closeConnection()
AdaptrisConnectionImp
closeConnection
in class AdaptrisConnectionImp
public void setDriverImp(java.lang.String s)
Sets the driver implementation to use.
s
- the driver implementation to usepublic java.lang.String getDriverImp()
Returns the driver implementation to use.
public void setAutoCommit(java.lang.Boolean b)
Sets whether to auto-commit.
b
- whether to auto-commit; default is true.public java.lang.Boolean getAutoCommit()
Returns whether to auto-commit.
public boolean autoCommit()
public void setDebugMode(java.lang.Boolean dbg)
Set this connection's debug mode.
In debug mode there is additional logging for reconnection attempts.
dbg
- this connection's debug modepublic java.lang.Boolean getDebugMode()
Returns this connection's debug mode.
public boolean debugMode()
public void setTestStatement(java.lang.String s)
The default test statement is SELECT DATABASE(), VERSION(), NOW(), USER()
which may not be suitable for your
database driver. Additionally depending on the JDBC driver implementation certain statements may be 'cached' and might never
hit the database, so you need to be aware of that as you will be relying on this test-statement to verify the connection
validity.
s
- the SQL statement used to test this connection; the default is SELECT DATABASE(), VERSION(), NOW(), USER()setAlwaysValidateConnection(Boolean)
public java.lang.String getTestStatement()
Returns the SQL statement used to test this connection.
public javax.sql.DataSource asDataSource() throws java.sql.SQLException
java.sql.SQLException
DataSource
public final java.sql.Connection connect() throws java.sql.SQLException
Returns the underlying SQLConnection
.
SQLConnection
java.sql.SQLException
- if the connection was not valid, and reconnection failedprotected 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.
java.sql.SQLException
- if the connection could not be made.connect()
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.
CoreException
- wrapping any underlying exception.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.
CoreException
- wrapping any underlying exception.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.
protected abstract void closeDatabaseConnection()
Initialise the underlying database connection.
This abstract method should be implemented by concrete sub-classes to close any components.
protected abstract java.lang.String getConnectionName()
Returns a name for this connection for logging purposes.
protected void checkInternalState() throws java.sql.SQLException
java.sql.SQLException
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.
b
- whether to always validate the database connection; defaults to false.public java.lang.Boolean getAlwaysValidateConnection()
Returns whether to always validate the database connection.
public boolean alwaysValidateConnection()
public abstract boolean equals(java.lang.Object o)
Force implementations to over-ride equals with a semantic implementation.
equals
in class java.lang.Object
Object.equals(java.lang.Object)
public abstract int hashCode()
Force implementations to over-ride hashcode.
hashCode
in class java.lang.Object
Object.hashCode()
public java.lang.String getUsername()
public void setUsername(java.lang.String s)
s
- public java.lang.String getPassword()
public void setPassword(java.lang.String s)
s
- the password which might be encoded using an available password scheme from
Password
public KeyValuePairSet getConnectionProperties()
public void setConnectionProperties(KeyValuePairSet p)
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 configure setUsername(String)
or
setPassword(String)
.
p
- any additional properties over and above username/password.protected java.util.Properties connectionProperties() throws PasswordException
PasswordException