org.springframework.orm.jdo
Class JdoInterceptor

java.lang.Object
  extended by org.springframework.orm.jdo.JdoAccessor
      extended by org.springframework.orm.jdo.JdoInterceptor
All Implemented Interfaces:
Advice, Interceptor, MethodInterceptor, InitializingBean

public class JdoInterceptor
extends JdoAccessor
implements MethodInterceptor

This interceptor binds a new JDO PersistenceManager 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 PersistenceManager (e.g. from JdoTransactionManager, or from a surrounding JDO-intercepted method), the interceptor simply participates in it.

Application code must retrieve a JDO PersistenceManager via the PersistenceManagerFactoryUtils.getPersistenceManager method, to be able to detect a thread-bound PersistenceManager. It is preferable to use getPersistenceManager with allowCreate=false, if the code relies on the interceptor to provide proper PersistenceManager handling. Typically, the code will look as follows:

 public void doJdoAction() {
   PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(this.pmf, false);
   try {
     ...
   }
   catch (JDOException ex) {
     throw PersistenceManagerFactoryUtils.convertJdoAccessException(ex);
   }
 }
Note that the application must care about handling JDOExceptions itself, preferably via delegating to the PersistenceManagerFactoryUtils.convertJdoAccessException method that converts them to exceptions that are compatible with the org.springframework.dao exception hierarchy (like JdoTemplate does).

This interceptor could convert unchecked JDOExceptions to unchecked dao ones on-the-fly. The intercepted method wouldn't have to throw any special checked exceptions to be able to achieve this. Nevertheless, such a mechanism would effectively break the contract of the intercepted method (runtime exceptions can be considered part of the contract too), therefore it isn't supported.

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

The drawbacks are:

Since:
13.06.2003
Author:
Juergen Hoeller
See Also:
PersistenceManagerFactoryUtils.getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean), JdoTransactionManager, JdoTemplate

Field Summary
 
Fields inherited from class org.springframework.orm.jdo.JdoAccessor
logger
 
Constructor Summary
JdoInterceptor()
           
 
Method Summary
 Object invoke(MethodInvocation methodInvocation)
           
 
Methods inherited from class org.springframework.orm.jdo.JdoAccessor
afterPropertiesSet, convertJdoAccessException, flushIfNecessary, getJdoDialect, getPersistenceManagerFactory, isFlushEager, setFlushEager, setJdoDialect, setPersistenceManagerFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdoInterceptor

public JdoInterceptor()
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.