org.clazzes.util.aop.jdbc
Class JdbcDAOSupport

java.lang.Object
  extended by org.clazzes.util.aop.jdbc.JdbcDAOSupport

public class JdbcDAOSupport
extends Object

A base class for JDBC DAO classes, which provides access to a thread-bound JDBC connection, which is established by a JdbcTransactionInterceptor.


Constructor Summary
protected JdbcDAOSupport()
           
 
Method Summary
protected static void closeStatement(Statement statement)
          Close an SQL statement.
protected  Connection getConnection()
          Fetch previously bound connection from ThreadLocalManager.
 String getThreadLocalKey()
           
protected
<T> T
performWithPreparedStatement(String sql, int resultSetType, int resultSetConcurrency, JdbcPreparedStatementAction<T> action)
          Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause.
protected
<T> T
performWithPreparedStatement(String sql, int autoGeneratedKeys, JdbcPreparedStatementAction<T> action)
          Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause.
protected
<T> T
performWithPreparedStatement(String sql, JdbcPreparedStatementAction<T> action)
          Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause.
protected
<T> T
performWithStatement(int resultSetType, int resultSetConcurrency, JdbcStatementAction<T> action)
          Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause.
protected
<T> T
performWithStatement(JdbcStatementAction<T> action)
          Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause.
 void setThreadLocalKey(String key)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcDAOSupport

protected JdbcDAOSupport()
Method Detail

getConnection

protected Connection getConnection()

Fetch previously bound connection from ThreadLocalManager.

Please carefully call closeStatement(Statement) in a finally clause on statements you get from the retrieved connection or you will run in severe resource leakages and threading problems.

It is recommended to use performWithPreparedStatement(String, JdbcPreparedStatementAction) and performWithStatement(JdbcStatementAction) whenever possible in order to circumvent these problems.

If you set up JDBC statements on your own, please follow the pattern outlined below in your implementation:

 public RetValType myDAOFunction() {
 
   Connection connection = this.getConnection();
   Statement statement1 = null;
   PreparedStatement statement2 = null;
   try
   {
       statement1 = connection.createStatement();
       statement2 = connection.prepareStatement("select foo from bar where id = ?");
       
       ...your code operating with the two statements...
       
       return __your_result_here__;
       
   } catch (Exception e)
   {
       throw new DAOException(e);
   }
   finally {
       closeStatement(statement1);
       closeStatement(statement2);
   }
 }  
 

Returns:
A JDBC transaction, which has previously been bound to the current thread using the configured thread-local key to ThreadLocalManager.bindResource(String, Object).

closeStatement

protected static void closeStatement(Statement statement)
Close an SQL statement. Any exception thrown will be logged an this function silently returns. Call this function from a finally clause inside your DAO implementation, if you do not use performWithPreparedStatement(String, JdbcPreparedStatementAction) or performWithStatement(JdbcStatementAction).

Parameters:
statement - The statement to close. If null is passed, this method silently exits.

performWithStatement

protected <T> T performWithStatement(JdbcStatementAction<T> action)

Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause.

This method uses a statement with a result set type of ResultSet.TYPE_FORWARD_ONLY and concurrency ResultSet.CONCUR_READ_ONLY. If you want to specify the result set type and concurrency, use the method performWithStatement(int, int, JdbcStatementAction).

Type Parameters:
T - The intended return value.
Parameters:
action - The action to perform on a newly created statement.
Returns:
The value returned by JdbcStatementAction.perform(Statement).

performWithStatement

protected <T> T performWithStatement(int resultSetType,
                                     int resultSetConcurrency,
                                     JdbcStatementAction<T> action)

Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause.

This method allows to specify the result set type and concurrency.

Type Parameters:
T - The intended return value.
Parameters:
resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency - a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
action - The action to perform on a newly created statement.
Returns:
The value returned by JdbcStatementAction.perform(Statement).

performWithPreparedStatement

protected <T> T performWithPreparedStatement(String sql,
                                             JdbcPreparedStatementAction<T> action)

Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause.

This method uses a statement with a result set type of ResultSet.TYPE_FORWARD_ONLY and concurrency ResultSet.CONCUR_READ_ONLY. If you want to specify the result set type and concurrency, use the method performWithPreparedStatement(String, int, int, JdbcPreparedStatementAction).

Type Parameters:
T - The intended return value.
Parameters:
sql - The SQL code for the prepared statement.
action - The action to perform on a newly created prepared statement based on the given SQL code.
Returns:
The value returned by JdbcStatementAction.perform(Statement).

performWithPreparedStatement

protected <T> T performWithPreparedStatement(String sql,
                                             int resultSetType,
                                             int resultSetConcurrency,
                                             JdbcPreparedStatementAction<T> action)

Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause.

This method allows to specify the result set type and concurrency.

Type Parameters:
T - The intended return value.
Parameters:
sql - The SQL code for the prepared statement.
resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency - a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
action - The action to perform on a newly created prepared statement based on the given SQL code.
Returns:
The value returned by JdbcStatementAction.perform(Statement).

performWithPreparedStatement

protected <T> T performWithPreparedStatement(String sql,
                                             int autoGeneratedKeys,
                                             JdbcPreparedStatementAction<T> action)
Perform a JDBC action inside a context, where an JDBC statement has been set up and will be closed in a finally clause. This method allows server-generated keys to be retrieved via Statement.getGeneratedKeys() by passing Statement.RETURN_GENERATED_KEYS as the second argument.

Type Parameters:
T - The intended return value.
Parameters:
sql - The SQL code for the prepared statement.
autoGeneratedKeys - a constant indicating whether auto-generated keys should be made available for retrieval using the method getGeneratedKeys; one of the following constants: Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS.
action - The action to perform on a newly created prepared statement based on the given SQL code.
Returns:
The value returned by JdbcStatementAction.perform(Statement).

getThreadLocalKey

public String getThreadLocalKey()
Returns:
The key for retrieving the thread-bound connection. The default value is JdbcTransactionInterceptor.DEFAULT_THREAD_LOCAL_KEY.

setThreadLocalKey

public void setThreadLocalKey(String key)
Parameters:
key - the key for retrieving the connection to set. The default value is JdbcTransactionInterceptor.DEFAULT_THREAD_LOCAL_KEY. Configure this property, if you use multiple JDBC datasources. For clarity, it is recommended to use the JDBC URL of the underlying datasource for this value.


Copyright © 2013. All Rights Reserved.