1 16 17 package org.springframework.orm.toplink; 18 19 import oracle.toplink.exceptions.TopLinkException; 20 import oracle.toplink.sessions.Session; 21 import org.aopalliance.intercept.MethodInterceptor; 22 import org.aopalliance.intercept.MethodInvocation; 23 24 import org.springframework.transaction.support.TransactionSynchronizationManager; 25 26 67 public class TopLinkInterceptor extends TopLinkAccessor implements MethodInterceptor { 68 69 private boolean exceptionConversionEnabled = true; 70 71 72 79 public void setExceptionConversionEnabled(boolean exceptionConversionEnabled) { 80 this.exceptionConversionEnabled = exceptionConversionEnabled; 81 } 82 83 84 public Object invoke(MethodInvocation methodInvocation) throws Throwable { 85 boolean existingTransaction = false; 86 Session session = SessionFactoryUtils.getSession(getSessionFactory(), true); 87 if (TransactionSynchronizationManager.hasResource(getSessionFactory())) { 88 logger.debug("Found thread-bound Session for TopLink interceptor"); 89 existingTransaction = true; 90 } 91 else { 92 logger.debug("Using new Session for TopLink interceptor"); 93 TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session)); 94 } 95 try { 96 return methodInvocation.proceed(); 97 } 98 catch (TopLinkException ex) { 99 if (this.exceptionConversionEnabled) { 100 throw convertTopLinkAccessException(ex); 101 } 102 else { 103 throw ex; 104 } 105 } 106 finally { 107 if (existingTransaction) { 108 logger.debug("Not closing pre-bound TopLink Session after interceptor"); 109 } 110 else { 111 TransactionSynchronizationManager.unbindResource(getSessionFactory()); 112 SessionFactoryUtils.releaseSession(session, getSessionFactory()); 113 } 114 } 115 } 116 117 } 118 | Popular Tags |