com.interface21.orm.jdo
Class JdoInterceptor

java.lang.Object
  |
  +--com.interface21.orm.jdo.JdoInterceptor
All Implemented Interfaces:
org.aopalliance.Interceptor, org.aopalliance.MethodInterceptor

public class JdoInterceptor
extends java.lang.Object
implements org.aopalliance.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 was a pre-bound PersistenceManager (e.g. from JdoTransactionManager, or from a surrounding JDO-intercepted method), the interceptor simply takes part in it.

Application code must retrieve a JDO PersistenceManager via PersistenceManagerFactoryUtils' getPersistenceManager method, to be able to detect a thread-bound PersistenceManager. It is preferable to use getPersistenceManager with allowCreate=false, as 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 PersistenceManagerFactoryUtils' convertJdoAccessException that converts them to ones that are compatible with the com.interface21.dao exception hierarchy (jlike JdoTemplate does). As JDOExceptions are unchecked, they can simply get thrown too, sacrificing generic DAO abstraction in terms of exceptions though.

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

Constructor Summary
JdoInterceptor()
           
 
Method Summary
 java.lang.Object invoke(org.aopalliance.MethodInvocation methodInvocation)
           
 void setPersistenceManagerFactory(javax.jdo.PersistenceManagerFactory persistenceManagerFactory)
           
 
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

setPersistenceManagerFactory

public void setPersistenceManagerFactory(javax.jdo.PersistenceManagerFactory persistenceManagerFactory)

invoke

public java.lang.Object invoke(org.aopalliance.MethodInvocation methodInvocation)
                        throws java.lang.Throwable
Specified by:
invoke in interface org.aopalliance.MethodInterceptor


Rod Johnson and Spring contributors 2001-2003.