1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.orm.jdo;
  17. import javax.jdo.PersistenceManager;
  18. import org.aopalliance.intercept.MethodInterceptor;
  19. import org.aopalliance.intercept.MethodInvocation;
  20. import org.springframework.transaction.support.TransactionSynchronizationManager;
  21. /**
  22. * This interceptor binds a new JDO PersistenceManager to the thread before a method
  23. * call, closing and removing it afterwards in case of any method outcome.
  24. * If there already was a pre-bound PersistenceManager (e.g. from JdoTransactionManager,
  25. * or from a surrounding JDO-intercepted method), the interceptor simply takes part in it.
  26. *
  27. * <p>Application code must retrieve a JDO PersistenceManager via
  28. * PersistenceManagerFactoryUtils' getPersistenceManager method, to be able to detect
  29. * a thread-bound PersistenceManager. It is preferable to use getPersistenceManager
  30. * with allowCreate=false, as the code relies on the interceptor to provide proper
  31. * PersistenceManager handling. Typically the code will look as follows:
  32. *
  33. * <pre>
  34. * public void doJdoAction() {
  35. * PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(this.pmf, false);
  36. * try {
  37. * ...
  38. * }
  39. * catch (JDOException ex) {
  40. * throw PersistenceManagerFactoryUtils.convertJdoAccessException(ex);
  41. * }
  42. * }</pre>
  43. *
  44. * Note that the application must care about handling JDOExceptions itself,
  45. * preferably via delegating to PersistenceManagerFactoryUtils' convertJdoAccessException
  46. * that converts them to ones that are compatible with the org.springframework.dao exception
  47. * hierarchy (jlike JdoTemplate does). As JDOExceptions are unchecked, they can simply
  48. * get thrown too, sacrificing generic DAO abstraction in terms of exceptions though.
  49. *
  50. * <p>This interceptor could convert unchecked JDOExceptions to unchecked dao ones
  51. * on-the-fly. The intercepted method wouldn't have to throw any special checked
  52. * exceptions to be able to achieve this. Nevertheless, such a mechanism would
  53. * effectively break the contract of the intercepted method (runtime exceptions
  54. * can be considered part of the contract too), therefore it isn't supported.
  55. *
  56. * <p>This class can be considered a declarative alternative to JdoTemplate's
  57. * callback approach. The advantages are:
  58. * <ul>
  59. * <li>no anonymous classes necessary for callback implementations;
  60. * <li>the possibility to throw any application exceptions from within data access code.
  61. * </ul>
  62. *
  63. * <p>The drawbacks are:
  64. * <ul>
  65. * <li>the dependency on interceptor configuration;
  66. * <li>the delegating try/catch blocks.
  67. * </ul>
  68. *
  69. * @author Juergen Hoeller
  70. * @since 13.06.2003
  71. */
  72. public class JdoInterceptor extends JdoAccessor implements MethodInterceptor {
  73. public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  74. boolean existingTransaction = false;
  75. PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), true);
  76. if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) {
  77. logger.debug("Found thread-bound PersistenceManager for JDO interceptor");
  78. existingTransaction = true;
  79. }
  80. else {
  81. logger.debug("Using new PersistenceManager for JDO interceptor");
  82. TransactionSynchronizationManager.bindResource(getPersistenceManagerFactory(), new PersistenceManagerHolder(pm));
  83. }
  84. try {
  85. Object retVal = methodInvocation.proceed();
  86. flushIfNecessary(pm, existingTransaction);
  87. return retVal;
  88. }
  89. finally {
  90. if (existingTransaction) {
  91. logger.debug("Not closing pre-bound JDO PersistenceManager after interceptor");
  92. }
  93. else {
  94. TransactionSynchronizationManager.unbindResource(getPersistenceManagerFactory());
  95. PersistenceManagerFactoryUtils.closePersistenceManagerIfNecessary(pm, getPersistenceManagerFactory());
  96. }
  97. }
  98. }
  99. }