org.clazzes.util.aop.jdbc
Class JdbcTransactionInterceptor

java.lang.Object
  extended by org.clazzes.util.aop.jdbc.JdbcTransactionInterceptor
All Implemented Interfaces:
org.aopalliance.aop.Advice, org.aopalliance.intercept.Interceptor, org.aopalliance.intercept.MethodInterceptor

public class JdbcTransactionInterceptor
extends Object
implements org.aopalliance.intercept.MethodInterceptor

This method interceptor fetches a JDBC transaction from a JDBC Datasource, opens a transaction and binds the JDBC connection to the current thread using ThreadLocalManager.bindResource(String, Object) with the given datasource as a key.

When the method finishes, the transaction is committed or rolled back and the JDBC connection is unbound from the current thread.


Field Summary
static String DEFAULT_THREAD_LOCAL_KEY
          The default thread local key, which is used if no other key is configured.
 
Constructor Summary
JdbcTransactionInterceptor()
           
 
Method Summary
 String getCatalog()
           
 DataSource getDataSource()
           
 String getThreadLocalKey()
           
 Integer getTransactionIsolation()
           
 Object invoke(org.aopalliance.intercept.MethodInvocation invocation)
           
 boolean isPedanticProbe()
           
 void probeDataBase()
          This method may be used as an initialization method in order to resolve the database driver and check the database connection.
 void setCatalog(String catalog)
           
 void setDataSource(DataSource dataSource)
           
 void setPedanticProbe(boolean pedanticProbe)
           
 void setThreadLocalKey(String key)
           
 void setTransactionIsolation(Integer transactionIsolation)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_THREAD_LOCAL_KEY

public static final String DEFAULT_THREAD_LOCAL_KEY
The default thread local key, which is used if no other key is configured.

See Also:
Constant Field Values
Constructor Detail

JdbcTransactionInterceptor

public JdbcTransactionInterceptor()
Method Detail

probeDataBase

public void probeDataBase()
This method may be used as an initialization method in order to resolve the database driver and check the database connection. If isPedanticProbe() is true, this method will fail with a DAOException, when the database connection is not ready.


invoke

public Object invoke(org.aopalliance.intercept.MethodInvocation invocation)
              throws Throwable
Specified by:
invoke in interface org.aopalliance.intercept.MethodInterceptor
Throws:
Throwable

getDataSource

public DataSource getDataSource()
Returns:
The JDBC datasource for retrieving the connection.

setDataSource

public void setDataSource(DataSource dataSource)
Parameters:
dataSource - the JDBC datasource for retrieving the connection to set.

getThreadLocalKey

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

setThreadLocalKey

public void setThreadLocalKey(String key)
Parameters:
key - the key for retrieving the connection to set. The default value is 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.

getTransactionIsolation

public Integer getTransactionIsolation()
Returns:
An optional transaction isolation level to set.
See Also:
Connection.setTransactionIsolation(int)

setTransactionIsolation

public void setTransactionIsolation(Integer transactionIsolation)
Parameters:
transactionIsolation - the optional transaction isloation level to set.

getCatalog

public String getCatalog()
Returns:
An optional catalog to set to newly created connection.
See Also:
Connection.setCatalog(String)

setCatalog

public void setCatalog(String catalog)
Parameters:
catalog - the optional catalog to set to newly created connection to set

isPedanticProbe

public boolean isPedanticProbe()
Returns:
Whether probeDataBase() should fail on connection errors.

setPedanticProbe

public void setPedanticProbe(boolean pedanticProbe)
Parameters:
pedanticProbe - Whether probeDataBase() should fail on connection errors.


Copyright © 2012. All Rights Reserved.