public class ConnectionHelper
extends java.lang.Object
startBatch()
and endBatch(boolean)
methods for this).
This class contains logic to retry execution of SQL statements. If this helper is not in batch mode
and if a statement fails due to an SQLException
, then it is retried. If the block
argument
of the constructor call was false
then it is retried only once. Otherwise the statement is retried
until either it succeeds or the thread is interrupted. This clearly assumes that the only cause of SQLExceptions
is faulty Connections
which are restored eventually.
Note:
This retry logic only applies to the following methods:
This class is not thread-safe and if it is to be used by multiple threads then the clients must make sure that access to this class is properly synchronized.
Implementation note: The Connection
that is retrieved from the DataSource
in getConnection(boolean)
may be broken. This is so because if an internal DataSource
is used,
then this is a commons-dbcp DataSource
with a testWhileIdle
validation strategy (see
the ConnectionFactory
class). Furthermore, if it is a DataSource
obtained through JNDI then we
can make no assumptions about the validation strategy. This means that our retry logic must either assume that
the SQL it tries to execute can do so without errors (i.e., the statement is valid), or it must implement its
own validation strategy to apply. Currently, the former is in place.
Modifier and Type | Class and Description |
---|---|
class |
ConnectionHelper.RetryManager<T>
This class encapsulates the logic to retry a method invocation if it threw an SQLException.
|
Constructor and Description |
---|
ConnectionHelper(javax.sql.DataSource dataSrc,
boolean block) |
Modifier and Type | Method and Description |
---|---|
void |
endBatch(boolean commit)
This method always ends the batch mode.
|
void |
exec(java.lang.String sql,
java.lang.Object... params)
Executes a general SQL statement and immediately closes all resources.
|
java.sql.ResultSet |
exec(java.lang.String sql,
java.lang.Object[] params,
boolean returnGeneratedKeys,
int maxRows)
Executes a general SQL statement and returns the
ResultSet of the executed statement. |
java.lang.String |
prepareDbIdentifier(java.lang.String identifier)
A utility method that makes sure that
identifier does only consist of characters that are
allowed in names on the target database. |
java.sql.ResultSet |
query(java.lang.String sql,
java.lang.Object... params)
Executes a SQL query and returns the
ResultSet . |
void |
startBatch()
Starts the batch mode.
|
boolean |
tableExists(java.lang.String tableName)
Checks whether the given table exists in the database.
|
int |
update(java.lang.String sql,
java.lang.Object... params)
Executes an update or delete statement and returns the update count.
|
public ConnectionHelper(javax.sql.DataSource dataSrc, boolean block)
dataSrc
- the DataSource
on which this instance actsblock
- whether the helper should transparently block on DB connection loss (otherwise it retries
once and if that fails throws exception)public final java.lang.String prepareDbIdentifier(java.lang.String identifier) throws java.sql.SQLException
identifier
does only consist of characters that are
allowed in names on the target database. Illegal characters will be escaped as necessary.
This method is not affected by theidentifier
- the identifier to convert to a db specific identifierjava.sql.SQLException
- if an error occurspublic final boolean tableExists(java.lang.String tableName) throws java.sql.SQLException
tableName
- the name of the tablejava.sql.SQLException
- on errorpublic final void startBatch() throws java.sql.SQLException
SQLException
is thrown, then the batch mode is not started.
Important: clients that call this method must make sure that
endBatch(boolean)
is called eventually.
java.sql.SQLException
- on errorpublic final void endBatch(boolean commit) throws java.sql.SQLException
commit
- whether the changes in the batch should be committed or rolled backjava.sql.SQLException
- if the commit or rollback of the underlying JDBC Connection threw an SQLException
public final void exec(java.lang.String sql, java.lang.Object... params) throws java.sql.SQLException
sql
- an SQL statement stringparams
- the parameters for the SQL statementjava.sql.SQLException
- on errorpublic final int update(java.lang.String sql, java.lang.Object... params) throws java.sql.SQLException
sql
- an SQL statement stringparams
- the parameters for the SQL statementjava.sql.SQLException
- on errorpublic final java.sql.ResultSet query(java.lang.String sql, java.lang.Object... params) throws java.sql.SQLException
ResultSet
. The
returned ResultSet
should be closed by clients.sql
- an SQL statement stringparams
- the parameters for the SQL statementResultSet
java.sql.SQLException
public final java.sql.ResultSet exec(java.lang.String sql, java.lang.Object[] params, boolean returnGeneratedKeys, int maxRows) throws java.sql.SQLException
ResultSet
of the executed statement. The
returned ResultSet
should be closed by clients.sql
- an SQL statement stringparams
- the parameters for the SQL statementreturnGeneratedKeys
- whether generated keys should be returnedmaxRows
- the maximum number of rows in a potential ResultSet
(0 means no limit)ResultSet
java.sql.SQLException
- on errorCopyright © 2010 - 2023 Adobe. All Rights Reserved