Class ConnectionHelper
- java.lang.Object
-
- org.apache.jackrabbit.core.util.db.ConnectionHelper
-
- Direct Known Subclasses:
DerbyConnectionHelper,OracleConnectionHelper,PostgreSQLConnectionHelper
public class ConnectionHelper extends java.lang.ObjectThis class provides convenience methods to execute SQL statements. They can be either executed in isolation or within the context of a JDBC transaction; the so-called batch mode (use thestartBatch()andendBatch(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 theblockargument of the constructor call wasfalsethen 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 ofSQLExceptionsis faultyConnectionswhich 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
Connectionthat is retrieved from theDataSourceingetConnection(boolean)may be broken. This is so because if an internalDataSourceis used, then this is a commons-dbcpDataSourcewith atestWhileIdlevalidation strategy (see theConnectionFactoryclass). Furthermore, if it is aDataSourceobtained 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classConnectionHelper.RetryManager<T>This class encapsulates the logic to retry a method invocation if it threw an SQLException.
-
Constructor Summary
Constructors Constructor Description ConnectionHelper(javax.sql.DataSource dataSrc, boolean block)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidendBatch(boolean commit)This method always ends the batch mode.voidexec(java.lang.String sql, java.lang.Object... params)Executes a general SQL statement and immediately closes all resources.java.sql.ResultSetexec(java.lang.String sql, java.lang.Object[] params, boolean returnGeneratedKeys, int maxRows)Executes a general SQL statement and returns theResultSetof the executed statement.java.lang.StringprepareDbIdentifier(java.lang.String identifier)A utility method that makes sure thatidentifierdoes only consist of characters that are allowed in names on the target database.java.sql.ResultSetquery(java.lang.String sql, java.lang.Object... params)Executes a SQL query and returns theResultSet.voidstartBatch()Starts the batch mode.booleantableExists(java.lang.String tableName)Checks whether the given table exists in the database.intupdate(java.lang.String sql, java.lang.Object... params)Executes an update or delete statement and returns the update count.
-
-
-
Constructor Detail
-
ConnectionHelper
public ConnectionHelper(javax.sql.DataSource dataSrc, boolean block)- Parameters:
dataSrc- theDataSourceon which this instance actsblock- whether the helper should transparently block on DB connection loss (otherwise it retries once and if that fails throws exception)
-
-
Method Detail
-
prepareDbIdentifier
public final java.lang.String prepareDbIdentifier(java.lang.String identifier) throws java.sql.SQLExceptionA utility method that makes sure thatidentifierdoes 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 the- Parameters:
identifier- the identifier to convert to a db specific identifier- Returns:
- the db-normalized form of the given identifier
- Throws:
java.sql.SQLException- if an error occurs
-
tableExists
public final boolean tableExists(java.lang.String tableName) throws java.sql.SQLExceptionChecks whether the given table exists in the database.- Parameters:
tableName- the name of the table- Returns:
- whether the given table exists
- Throws:
java.sql.SQLException- on error
-
startBatch
public final void startBatch() throws java.sql.SQLExceptionStarts the batch mode. If anSQLExceptionis thrown, then the batch mode is not started.Important: clients that call this method must make sure that
endBatch(boolean)is called eventually.- Throws:
java.sql.SQLException- on error
-
endBatch
public final void endBatch(boolean commit) throws java.sql.SQLExceptionThis method always ends the batch mode.- Parameters:
commit- whether the changes in the batch should be committed or rolled back- Throws:
java.sql.SQLException- if the commit or rollback of the underlying JDBC Connection threw anSQLException
-
exec
public final void exec(java.lang.String sql, java.lang.Object... params) throws java.sql.SQLExceptionExecutes a general SQL statement and immediately closes all resources. Note: We use a Statement if there are no parameters to avoid a problem on the Oracle 10g JDBC driver w.r.t. :NEW and :OLD keywords that triggers ORA-17041.- Parameters:
sql- an SQL statement stringparams- the parameters for the SQL statement- Throws:
java.sql.SQLException- on error
-
update
public final int update(java.lang.String sql, java.lang.Object... params) throws java.sql.SQLExceptionExecutes an update or delete statement and returns the update count.- Parameters:
sql- an SQL statement stringparams- the parameters for the SQL statement- Returns:
- the update count
- Throws:
java.sql.SQLException- on error
-
query
public final java.sql.ResultSet query(java.lang.String sql, java.lang.Object... params) throws java.sql.SQLExceptionExecutes a SQL query and returns theResultSet. The returnedResultSetshould be closed by clients.- Parameters:
sql- an SQL statement stringparams- the parameters for the SQL statement- Returns:
- a
ResultSet - Throws:
java.sql.SQLException
-
exec
public final java.sql.ResultSet exec(java.lang.String sql, java.lang.Object[] params, boolean returnGeneratedKeys, int maxRows) throws java.sql.SQLExceptionExecutes a general SQL statement and returns theResultSetof the executed statement. The returnedResultSetshould be closed by clients.- Parameters:
sql- an SQL statement stringparams- the parameters for the SQL statementreturnGeneratedKeys- whether generated keys should be returnedmaxRows- the maximum number of rows in a potentialResultSet(0 means no limit)- Returns:
- a
ResultSet - Throws:
java.sql.SQLException- on error
-
-