|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.clazzes.util.aop.jdbc.JdbcDAOSupport
public class JdbcDAOSupport
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
|
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
|
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
|
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 |
---|
protected JdbcDAOSupport()
Method Detail |
---|
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); } }
ThreadLocalManager.bindResource(String, Object)
.protected static void closeStatement(Statement statement)
performWithPreparedStatement(String, JdbcPreparedStatementAction)
or
performWithStatement(JdbcStatementAction)
.
statement
- The statement to close. If null
is passed,
this method silently exits.protected <T> T performWithStatement(JdbcStatementAction<T> action)
T
- The intended return value.action
- The action to perform on a newly created statement.
JdbcStatementAction.perform(Statement)
.protected <T> T performWithPreparedStatement(String sql, JdbcPreparedStatementAction<T> action)
T
- The intended return value.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.
JdbcStatementAction.perform(Statement)
.protected <T> T performWithPreparedStatement(String sql, int autoGeneratedKeys, JdbcPreparedStatementAction<T> action)
Statement.getGeneratedKeys()
by passing
Statement.RETURN_GENERATED_KEYS
as the second argument.
T
- The intended return value.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.
JdbcStatementAction.perform(Statement)
.public String getThreadLocalKey()
JdbcTransactionInterceptor.DEFAULT_THREAD_LOCAL_KEY
.public void setThreadLocalKey(String key)
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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |