public abstract class ExtendedDatabaseConnection
extends java.lang.Object
implements java.io.Serializable
Connection
that allows to reconnect after a failed execution of a statement.Modifier and Type | Class and Description |
---|---|
static class |
ExtendedDatabaseConnection.ExtendedDatabaseConnectionPublic
Provides a wrapper for the
ExtendedDatabaseConnection that offers
all the methods publicly. |
Modifier | Constructor and Description |
---|---|
protected |
ExtendedDatabaseConnection(ExtendedDatabaseConnection sourceConnection)
Creates a new extended database connection with parameters taken from another connection.
|
protected |
ExtendedDatabaseConnection(ExtendedDatabaseConnection sourceConnection,
boolean lazyConnection)
Creates a new extended database connection with parameters taken from another connection.
|
protected |
ExtendedDatabaseConnection(java.lang.String dbConnUrl,
java.util.Properties dbConnInfo,
java.lang.String dbDriverClass)
Creates a new extended database connection.
|
protected |
ExtendedDatabaseConnection(java.lang.String dbConnUrl,
java.util.Properties dbConnInfo,
java.lang.String dbDriverClass,
boolean lazyConnection)
Creates a new extended database connection.
|
Modifier and Type | Method and Description |
---|---|
protected void |
closeConnection()
Closes connection to the database.
|
static java.sql.Connection |
createConnection(java.lang.String dbConnUrl,
java.util.Properties dbConnInfo,
java.lang.String dbDriverClass)
Creates a new database connection.
|
static java.sql.Driver |
createDriver(java.lang.String dbDriverClass)
Creates a new instance of a database driver for the given driver class.
|
protected java.lang.Object |
executeDataManipulation(java.lang.String sql,
boolean returnGeneratedKeys,
java.lang.Object... parameters)
Executes a data modification SQL command (i.e.
|
protected java.lang.Object |
executeSingleValue(java.lang.String sql,
java.lang.Object... parameters)
Returns the first column of the first row returned by the given SQL command.
|
void |
finalize() |
protected java.sql.Connection |
getConnection()
Returns the database connection of this storage.
|
protected java.sql.PreparedStatement |
prepareAndExecute(java.sql.PreparedStatement statement,
java.lang.String sql,
boolean returnGeneratedKeys,
java.lang.Object... parameters)
Prepares and executes an SQL command using this storage's database connection.
|
protected java.sql.PreparedStatement |
prepareAndExecute(java.sql.PreparedStatement statement,
java.lang.String sql,
int fetchSize,
boolean returnGeneratedKeys,
java.lang.Object... parameters)
Prepares and executes an SQL command using this storage's database connection.
|
protected <K,V> java.util.Map<K,V> |
resultSetToMap(java.sql.ResultSet rs,
int keyColumn,
java.lang.Class<? extends K> keyClass,
int valueColumn,
java.lang.Class<? extends V> valueClass)
Creates a map of key-value pairs from a given result set.
|
java.lang.String |
toString() |
protected ExtendedDatabaseConnection(java.lang.String dbConnUrl, java.util.Properties dbConnInfo, java.lang.String dbDriverClass, boolean lazyConnection) throws java.lang.IllegalArgumentException, java.sql.SQLException
dbConnUrl
- the database connection URL (e.g. "jdbc:mysql://localhost/somedb")dbConnInfo
- additional parameters of the connection (e.g. "user" and "password")dbDriverClass
- class of the database driver to use (can be null if the driver is already registered)lazyConnection
- flag whether to open the database connection immediately (false)
or when a first command is executed (true)java.lang.IllegalArgumentException
- if the connection url is null or the driver class cannot be registeredjava.sql.SQLException
- if there was a problem connecting to the databaseprotected ExtendedDatabaseConnection(java.lang.String dbConnUrl, java.util.Properties dbConnInfo, java.lang.String dbDriverClass) throws java.lang.IllegalArgumentException, java.sql.SQLException
dbConnUrl
- the database connection URL (e.g. "jdbc:mysql://localhost/somedb")dbConnInfo
- additional parameters of the connection (e.g. "user" and "password")dbDriverClass
- class of the database driver to use (can be null if the driver is already registered)java.lang.IllegalArgumentException
- if the connection url is null or the driver class cannot be registeredjava.sql.SQLException
- if there was a problem connecting to the databaseprotected ExtendedDatabaseConnection(ExtendedDatabaseConnection sourceConnection, boolean lazyConnection) throws java.sql.SQLException
sourceConnection
- the database connection from which to get the connection string and infolazyConnection
- flag whether to open the database connection immediately (false)
or when a first command is executed (true)java.sql.SQLException
- if there was a problem connecting to the databaseprotected ExtendedDatabaseConnection(ExtendedDatabaseConnection sourceConnection) throws java.sql.SQLException
sourceConnection
- the database connection from which to get the connection string and infojava.sql.SQLException
- if there was a problem connecting to the databasepublic void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public static java.sql.Driver createDriver(java.lang.String dbDriverClass) throws java.lang.IllegalArgumentException
DriverManager
,
so use of the driver directly should be avoided.dbDriverClass
- class of the database driver to usejava.lang.IllegalArgumentException
- if the driver class cannot be createdpublic static java.sql.Connection createConnection(java.lang.String dbConnUrl, java.util.Properties dbConnInfo, java.lang.String dbDriverClass) throws java.lang.IllegalArgumentException, java.sql.SQLException
dbConnUrl
- the database connection URL (e.g. "jdbc:mysql://localhost/somedb")dbConnInfo
- additional parameters of the connection (e.g. "user" and "password")dbDriverClass
- class of the database driver to use (can be null if the driver is already registered)java.lang.IllegalArgumentException
- if the connection url is null or the driver class cannot be registeredjava.sql.SQLException
- if there was a problem connecting to the databaseprotected final java.sql.Connection getConnection() throws java.sql.SQLException
java.sql.SQLException
- if there was a problem connecting to the databaseprotected void closeConnection() throws java.sql.SQLException
java.sql.SQLException
- if there was an error while closing the connectionprotected final java.sql.PreparedStatement prepareAndExecute(java.sql.PreparedStatement statement, java.lang.String sql, int fetchSize, boolean returnGeneratedKeys, java.lang.Object... parameters) throws java.sql.SQLFeatureNotSupportedException, java.sql.SQLException
ResultSet
returned by the execution can be retrieved by Statement.getResultSet()
.
Note that if a SQLRecoverableException
is thrown while executing,
the current connection is closed
and the command
retried. For select queries, the number of results fetched
hint
can be specified if the driver supports it. Note, that e.g. for the MySQL driver,
additional useCursorFetch=true option must be given.statement
- the previous cached statement that matches the given sql
(can be null)sql
- the SQL command to prepare and executefetchSize
- the number of results fetched in one go, only applicable for SELECT SQL queries, should be zero otherwise or if a driver default should be usedreturnGeneratedKeys
- flag whether to set the Statement.RETURN_GENERATED_KEYS
on the prepared statementparameters
- the values for the SQL parameters (denoted by "?" chars in the SQL command)java.sql.SQLFeatureNotSupportedException
- if the Statement.RETURN_GENERATED_KEYS
is not supported by the driverjava.sql.SQLException
- if there was an unrecoverable error when parsing or executing the SQL commandprotected final java.sql.PreparedStatement prepareAndExecute(java.sql.PreparedStatement statement, java.lang.String sql, boolean returnGeneratedKeys, java.lang.Object... parameters) throws java.sql.SQLFeatureNotSupportedException, java.sql.SQLException
ResultSet
returned by the execution can be retrieved by Statement.getResultSet()
.
Note that if a SQLRecoverableException
is thrown while executing,
the current connection is closed
and the command
retried.statement
- the previous cached statement that matches the given sql
(can be null)sql
- the SQL command to prepare and executereturnGeneratedKeys
- flag whether to set the Statement.RETURN_GENERATED_KEYS
on the prepared statementparameters
- the values for the SQL parameters (denoted by "?" chars in the SQL command)java.sql.SQLFeatureNotSupportedException
- if the Statement.RETURN_GENERATED_KEYS
is not supported by the driverjava.sql.SQLException
- if there was an unrecoverable error when parsing or executing the SQL commandprotected final java.lang.Object executeSingleValue(java.lang.String sql, java.lang.Object... parameters) throws java.util.NoSuchElementException, java.sql.SQLException
sql
- the SQL command to executeparameters
- parameters for the "?" placeholders inside the SQL commandjava.util.NoSuchElementException
- if the SQL command does not return any rowjava.sql.SQLException
- if there was a problem parsing or executing the SQL commandprotected final java.lang.Object executeDataManipulation(java.lang.String sql, boolean returnGeneratedKeys, java.lang.Object... parameters) throws java.util.NoSuchElementException, java.sql.SQLException
generatedKeys
is true or the number of affected rows Statement.getUpdateCount()
if available.sql
- the SQL command to executereturnGeneratedKeys
- the flag whether to retrieve the keys generated automatically
by the database (e.g. when inserting)parameters
- the values for the parameters in the SQL statement;
there must be one value for every ? in the SQL statementjava.util.NoSuchElementException
- if the SQL command does not return any rowjava.sql.SQLException
- if there was a problem parsing or executing the SQL commandprotected final <K,V> java.util.Map<K,V> resultSetToMap(java.sql.ResultSet rs, int keyColumn, java.lang.Class<? extends K> keyClass, int valueColumn, java.lang.Class<? extends V> valueClass) throws java.sql.SQLException
keyColumn
and
values from the valueColumn
.K
- the class of the keysV
- the class of the valuesrs
- the result set to get the key-value pairs fromkeyColumn
- the identification of the column that contains the keys (1 means the first column, 2 the second, etc.)keyClass
- the class of the keysvalueColumn
- the identification of the column that contains the values (1 means the first column, 2 the second, etc.)valueClass
- the class of the valuesjava.sql.SQLException
- if there was an error reading values from the result setpublic java.lang.String toString()
toString
in class java.lang.Object