1 16 17 package org.springframework.transaction.jta; 18 19 import javax.transaction.Status ; 20 import javax.transaction.Synchronization ; 21 import javax.transaction.TransactionManager ; 22 import javax.transaction.UserTransaction ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import org.springframework.transaction.support.TransactionSynchronization; 28 import org.springframework.transaction.support.TransactionSynchronizationManager; 29 import org.springframework.util.Assert; 30 31 45 public class SpringJtaSynchronizationAdapter implements Synchronization { 46 47 protected static final Log logger = LogFactory.getLog(SpringJtaSynchronizationAdapter.class); 48 49 private final TransactionSynchronization springSynchronization; 50 51 private UserTransaction jtaTransaction; 52 53 private boolean beforeCompletionCalled = false; 54 55 56 61 public SpringJtaSynchronizationAdapter(TransactionSynchronization springSynchronization) { 62 Assert.notNull(springSynchronization, "TransactionSynchronization must not be null"); 63 this.springSynchronization = springSynchronization; 64 } 65 66 79 public SpringJtaSynchronizationAdapter( 80 TransactionSynchronization springSynchronization, UserTransaction jtaUserTransaction) { 81 82 this(springSynchronization); 83 if (jtaUserTransaction != null && !jtaUserTransaction.getClass().getName().startsWith("weblogic.")) { 84 this.jtaTransaction = jtaUserTransaction; 85 } 86 } 87 88 101 public SpringJtaSynchronizationAdapter( 102 TransactionSynchronization springSynchronization, TransactionManager jtaTransactionManager) { 103 104 this(springSynchronization); 105 if (jtaTransactionManager != null && !jtaTransactionManager.getClass().getName().startsWith("weblogic.")) { 106 this.jtaTransaction = new UserTransactionAdapter(jtaTransactionManager); 107 } 108 } 109 110 111 116 public void beforeCompletion() { 117 try { 118 boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); 119 this.springSynchronization.beforeCommit(readOnly); 120 } 121 catch (RuntimeException ex) { 122 setRollbackOnlyIfPossible(); 123 throw ex; 124 } 125 catch (Error err) { 126 setRollbackOnlyIfPossible(); 127 throw err; 128 } 129 finally { 130 this.beforeCompletionCalled = true; 134 this.springSynchronization.beforeCompletion(); 135 } 136 } 137 138 141 private void setRollbackOnlyIfPossible() { 142 if (this.jtaTransaction != null) { 143 try { 144 this.jtaTransaction.setRollbackOnly(); 145 } 146 catch (UnsupportedOperationException ex) { 147 logger.debug("JTA transaction handle does not support setRollbackOnly method - " + 149 "relying on JTA provider to mark the transaction as rollback-only based on " + 150 "the exception thrown from beforeCompletion", ex); 151 } 152 catch (Throwable ex) { 153 logger.error("Could not set JTA transaction rollback-only", ex); 154 } 155 } 156 else { 157 logger.debug("No JTA transaction handle available and/or running on WebLogic - " + 158 "relying on JTA provider to mark the transaction as rollback-only based on " + 159 "the exception thrown from beforeCompletion"); 160 } 161 } 162 163 171 public void afterCompletion(int status) { 172 if (!this.beforeCompletionCalled) { 173 this.springSynchronization.beforeCompletion(); 176 } 177 switch (status) { 179 case Status.STATUS_COMMITTED: 180 this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_COMMITTED); 181 break; 182 case Status.STATUS_ROLLEDBACK: 183 this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK); 184 break; 185 default: 186 this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN); 187 } 188 } 189 190 } 191 | Popular Tags |