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, to be able to detect a thread-bound Session. It is preferable to use getSession with allowCreate=false, if the code relies on the interceptor to provide proper Session handling. Typically, the code will look as follows:

 public void doTopLinkAction() {
   Session session = SessionFactoryUtils.getSession(this.sessionFactory, false);
   try {
     ...
   }
   catch (TopLinkException ex) {
     throw SessionFactoryUtils.convertTopLinkAccessException(ex);
   }
 }
Note that the application must care about handling TopLinkExceptions itself, preferably via delegating to the SessionFactoryUtils.convertTopLinkAccessException method that converts them to exceptions that are compatible with the org.springframework.dao exception hierarchy (like TopLinkTemplate does).

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

The drawbacks are:

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)
           
 
Methods inherited from class org.springframework.orm.toplink.TopLinkAccessor
afterPropertiesSet, convertJdbcAccessException, 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

invoke

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


Copyright (c) 2002-2007 The Spring Framework Project.