1 16 17 package org.springframework.orm.jdo; 18 19 import javax.jdo.JDOException; 20 import javax.jdo.PersistenceManager; 21 22 import org.aopalliance.intercept.MethodInterceptor; 23 import org.aopalliance.intercept.MethodInvocation; 24 25 import org.springframework.transaction.support.TransactionSynchronizationManager; 26 27 72 public class JdoInterceptor extends JdoAccessor implements MethodInterceptor { 73 74 private boolean exceptionConversionEnabled = true; 75 76 77 84 public void setExceptionConversionEnabled(boolean exceptionConversionEnabled) { 85 this.exceptionConversionEnabled = exceptionConversionEnabled; 86 } 87 88 89 public Object invoke(MethodInvocation methodInvocation) throws Throwable { 90 boolean existingTransaction = false; 91 PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), true); 92 if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) { 93 logger.debug("Found thread-bound PersistenceManager for JDO interceptor"); 94 existingTransaction = true; 95 } 96 else { 97 logger.debug("Using new PersistenceManager for JDO interceptor"); 98 TransactionSynchronizationManager.bindResource(getPersistenceManagerFactory(), new PersistenceManagerHolder(pm)); 99 } 100 try { 101 Object retVal = methodInvocation.proceed(); 102 flushIfNecessary(pm, existingTransaction); 103 return retVal; 104 } 105 catch (JDOException ex) { 106 if (this.exceptionConversionEnabled) { 107 throw convertJdoAccessException(ex); 108 } 109 else { 110 throw ex; 111 } 112 } 113 finally { 114 if (existingTransaction) { 115 logger.debug("Not closing pre-bound JDO PersistenceManager after interceptor"); 116 } 117 else { 118 TransactionSynchronizationManager.unbindResource(getPersistenceManagerFactory()); 119 PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory()); 120 } 121 } 122 } 123 124 } 125 | Popular Tags |