1 package org.hibernate.transaction; 3 4 import javax.transaction.Status ; 5 import javax.transaction.Synchronization ; 6 import javax.transaction.SystemException ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.hibernate.HibernateException; 11 import org.hibernate.Transaction; 12 import org.hibernate.TransactionException; 13 import org.hibernate.jdbc.JDBCContext; 14 import org.hibernate.util.JTAHelper; 15 16 21 public class CMTTransaction implements Transaction { 22 23 private static final Log log = LogFactory.getLog(CMTTransaction.class); 24 25 private final JDBCContext jdbcContext; 26 private final TransactionFactory.Context transactionContext; 27 28 private boolean begun; 29 private javax.transaction.Transaction transaction; 30 31 public CMTTransaction(JDBCContext jdbcContext, TransactionFactory.Context transactionContext) { 32 this.jdbcContext = jdbcContext; 33 this.transactionContext = transactionContext; 34 } 35 36 public void begin() throws HibernateException { 37 log.debug("begin"); 38 39 boolean synchronization = jdbcContext.registerSynchronizationIfPossible(); 40 41 if ( !synchronization ) { 42 throw new TransactionException("Could not register synchronization for container transaction"); 43 } 44 45 begun = true; 46 } 47 48 public void commit() throws HibernateException { 49 if (!begun) { 50 throw new TransactionException("Transaction not successfully started"); 51 } 52 53 log.debug("commit"); 54 55 boolean flush = !transactionContext.isFlushModeNever() && 56 !transactionContext.isFlushBeforeCompletionEnabled(); 57 58 if (flush) { 59 transactionContext.managedFlush(); } 61 62 } 63 64 public void rollback() throws HibernateException { 65 if (!begun) { 66 throw new TransactionException("Transaction not successfully started"); 67 } 68 69 log.debug("rollback"); 70 71 try { 72 transaction.setRollbackOnly(); 73 } 74 catch (SystemException se) { 75 log.error("Could not set transaction to rollback only", se); 76 throw new TransactionException("Could not set transaction to rollback only", se); 77 } 78 79 } 80 81 public boolean isActive() throws TransactionException { 82 83 if (!begun) return false; 84 85 final int status; 86 try { 87 status = transaction.getStatus(); 88 } 89 catch (SystemException se) { 90 log.error("Could not determine transaction status", se); 91 throw new TransactionException("Could not determine transaction status: ", se); 92 } 93 if (status==Status.STATUS_UNKNOWN) { 94 throw new TransactionException("Could not determine transaction status"); 95 } 96 else { 97 return status==Status.STATUS_ACTIVE; 98 } 99 } 100 101 public boolean wasRolledBack() throws TransactionException { 102 103 if (!begun) return false; 104 105 final int status; 106 try { 107 status = transaction.getStatus(); 108 } 109 catch (SystemException se) { 110 log.error("Could not determine transaction status", se); 111 throw new TransactionException("Could not determine transaction status", se); 112 } 113 if (status==Status.STATUS_UNKNOWN) { 114 throw new TransactionException("Could not determine transaction status"); 115 } 116 else { 117 return JTAHelper.isRollback(status); 118 } 119 } 120 121 public boolean wasCommitted() throws TransactionException { 122 123 if ( !begun ) return false; 124 125 final int status; 126 try { 127 status = transaction.getStatus(); 128 } 129 catch (SystemException se) { 130 log.error("Could not determine transaction status", se); 131 throw new TransactionException("Could not determine transaction status: ", se); 132 } 133 if (status==Status.STATUS_UNKNOWN) { 134 throw new TransactionException("Could not determine transaction status"); 135 } 136 else { 137 return status==Status.STATUS_COMMITTED; 138 } 139 } 140 141 public void registerSynchronization(Synchronization sync) throws HibernateException { 142 if (transaction!=null) { 143 try { 144 transaction.registerSynchronization(sync); 145 } 146 catch (Exception e) { 147 throw new TransactionException("Could not register synchronization", e); 148 } 149 } 150 else { 151 throw new IllegalStateException ("JTA TransactionManager not available"); 152 } 153 } 154 155 } 156 | Popular Tags |