The Spring Framework

org.springframework.orm.toplink
Class TopLinkInterceptor

java.lang.Object
  extended by org.springframework.orm.toplink.TopLinkAccessor
      extended by org.springframework.orm.toplink.TopLinkInterceptor
All Implemented Interfaces:
Advice, Interceptor, MethodInterceptor, InitializingBean

public class TopLinkInterceptor
extends TopLinkAccessor
implements MethodInterceptor

This interceptor binds a new TopLink Session to the thread before a method call, closing and removing it afterwards in case of any method outcome. If there already is a pre-bound Session (e.g. from TopLinkTransactionManager, or from a surrounding TopLink-intercepted method), the interceptor simply takes part in it.

Application code must retrieve a TopLink Session via the SessionFactoryUtils.getSession method or - preferably - TopLink's own Session.getActiveSession() method, to be able to detect a thread-bound Session. Typically, the code will look like as follows:

 public void doSomeDataAccessAction() {
   Session session = this.serverSession.getActiveSession();
   ...
 }
Note that this interceptor automatically translates TopLinkExceptions, via delegating to the SessionFactoryUtils.convertTopLikAccessException method that converts them to exceptions that are compatible with the org.springframework.dao exception hierarchy (like TopLinkTemplate does). This can be turned off if the raw exceptions are preferred.

This class can be considered a declarative alternative to TopLinkTemplate's callback approach. The advantages are:

The drawback is the dependency on interceptor configuration. However, note that this interceptor is usually not necessary in scenarios where the data access code always executes within transactions. A transaction will always have a thread-bound Session in the first place, so adding this interceptor to the configuration just adds value when potentially executing outside of transactions and/or when relying on exception translation.

Since:
1.2
Author:
Juergen Hoeller

Field Summary
 
Fields inherited from class org.springframework.orm.toplink.TopLinkAccessor
logger
 
Constructor Summary
TopLinkInterceptor()
           
 
Method Summary
 Object invoke(MethodInvocation methodInvocation)
           
 void setExceptionConversionEnabled(boolean exceptionConversionEnabled)
          Set whether to convert any TopLinkException raised to a Spring DataAccessException, compatible with the org.springframework.dao exception hierarchy.
 
Methods inherited from class org.springframework.orm.toplink.TopLinkAccessor
afterPropertiesSet, convertTopLinkAccessException, getJdbcExceptionTranslator, getSessionFactory, setJdbcExceptionTranslator, setSessionFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TopLinkInterceptor

public TopLinkInterceptor()
Method Detail

setExceptionConversionEnabled

public void setExceptionConversionEnabled(boolean exceptionConversionEnabled)
Set whether to convert any TopLinkException raised to a Spring DataAccessException, compatible with the org.springframework.dao exception hierarchy.

Default is "true". Turn this flag off to let the caller receive raw exceptions as-is, without any wrapping.

See Also:
DataAccessException

invoke

public Object invoke(MethodInvocation methodInvocation)
              throws Throwable
Specified by:
invoke in interface MethodInterceptor
Throws:
Throwable

The Spring Framework

Copyright © 2002-2007 The Spring Framework.