1 17 18 package org.apache.geronimo.connector.outbound; 19 20 import javax.resource.ResourceException ; 21 import javax.transaction.RollbackException ; 22 import javax.transaction.SystemException ; 23 import javax.transaction.Transaction ; 24 import javax.transaction.TransactionManager ; 25 import javax.transaction.xa.XAResource ; 26 27 35 public class TransactionEnlistingInterceptor implements ConnectionInterceptor { 36 37 private final ConnectionInterceptor next; 38 private final TransactionManager transactionManager; 39 40 public TransactionEnlistingInterceptor(ConnectionInterceptor next, TransactionManager transactionManager) { 41 this.next = next; 42 this.transactionManager = transactionManager; 43 } 44 45 public void getConnection(ConnectionInfo connectionInfo) throws ResourceException { 46 next.getConnection(connectionInfo); 47 try { 48 ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo(); 49 50 Transaction transaction = TxUtil.getTransactionIfActive(transactionManager); 52 if (transaction != null) { 53 XAResource xares = mci.getXAResource(); 54 transaction.enlistResource(xares); 55 } 56 } catch (SystemException e) { 57 returnConnection(connectionInfo, ConnectionReturnAction.DESTROY); 58 throw new ResourceException ("Could not get transaction", e); 59 } catch (RollbackException e) { 60 next.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE); 62 throw new ResourceException ("Could not enlist resource in rolled back transaction", e); 63 } catch (Throwable t) { 64 returnConnection(connectionInfo, ConnectionReturnAction.DESTROY); 65 throw new ResourceException ("Unknown throwable when trying to enlist connection in tx", t); 66 } 67 } 68 69 78 public void returnConnection(ConnectionInfo connectionInfo, 79 ConnectionReturnAction connectionReturnAction) { 80 try { 81 ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo(); 82 Transaction transaction = TxUtil.getTransactionIfActive(transactionManager); 83 if (transaction != null) { 84 XAResource xares = mci.getXAResource(); 85 transaction.delistResource(xares, XAResource.TMSUSPEND); 86 } 87 88 } catch (SystemException e) { 89 connectionReturnAction = ConnectionReturnAction.DESTROY; 91 } catch (IllegalStateException e) { 92 connectionReturnAction = ConnectionReturnAction.DESTROY; 93 } 94 95 next.returnConnection(connectionInfo, connectionReturnAction); 96 } 97 98 public void destroy() { 99 next.destroy(); 100 } 101 102 } 103 | Popular Tags |