org.springframework.orm.jdo
Class JdoInterceptor
java.lang.Object
org.springframework.orm.jdo.JdoAccessor
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:
- no anonymous classes necessary for callback implementations;
- the possibility to throw any application exceptions from within data access code.
The drawbacks are:
- the dependency on interceptor configuration;
- the delegating try/catch blocks.
- Since:
- 13.06.2003
- Author:
- Juergen Hoeller
- See Also:
PersistenceManagerFactoryUtils.getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean)
,
JdoTransactionManager
,
JdoTemplate
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
JdoInterceptor
public JdoInterceptor()
invoke
public Object invoke(MethodInvocation methodInvocation)
throws Throwable
- Specified by:
invoke
in interface MethodInterceptor
- Throws:
Throwable
Copyright (c) 2002-2007 The Spring Framework Project.