1 package org.hibernate.test.tm; 3 4 import java.sql.Connection ; 5 import java.sql.SQLException ; 6 import java.util.ArrayList ; 7 import java.util.List ; 8 9 import javax.transaction.HeuristicMixedException ; 10 import javax.transaction.HeuristicRollbackException ; 11 import javax.transaction.RollbackException ; 12 import javax.transaction.Status ; 13 import javax.transaction.Synchronization ; 14 import javax.transaction.SystemException ; 15 import javax.transaction.Transaction ; 16 import javax.transaction.xa.XAResource ; 17 18 21 public class DummyTransaction implements Transaction { 22 23 int status; 24 private Connection connection; 25 List synchronizations = new ArrayList (); 26 private DummyTransactionManager transactionManager; 27 28 DummyTransaction(DummyTransactionManager transactionManager) { 29 status = Status.STATUS_NO_TRANSACTION; 30 this.transactionManager = transactionManager; 31 } 32 33 public void begin() throws SystemException { 34 try { 35 connection = transactionManager.connections.getConnection(); 36 } 37 catch (SQLException sqle) { 38 throw new SystemException (); 39 } 40 status = Status.STATUS_ACTIVE; 41 } 42 43 public void commit() throws RollbackException , HeuristicMixedException , 44 HeuristicRollbackException , SecurityException , 45 IllegalStateException , SystemException { 46 47 if (status == Status.STATUS_MARKED_ROLLBACK) { 48 rollback(); 49 } 50 else { 51 status = Status.STATUS_PREPARING; 52 53 for ( int i=0; i<synchronizations.size(); i++ ) { 54 Synchronization s = (Synchronization ) synchronizations.get(i); 55 s.beforeCompletion(); 56 } 57 58 status = Status.STATUS_COMMITTING; 59 60 try { 61 connection.commit(); 62 connection.close(); 63 } 64 catch (SQLException sqle) { 65 status = Status.STATUS_UNKNOWN; 66 throw new SystemException (); 67 } 68 69 status = Status.STATUS_COMMITTED; 70 71 for ( int i=0; i<synchronizations.size(); i++ ) { 72 Synchronization s = (Synchronization ) synchronizations.get(i); 73 s.afterCompletion(status); 74 } 75 76 transactionManager.endCurrent(this); 78 } 79 80 } 81 82 public boolean delistResource(XAResource arg0, int arg1) 83 throws IllegalStateException , SystemException { 84 return false; 86 } 87 88 public boolean enlistResource(XAResource arg0) throws RollbackException , 89 IllegalStateException , SystemException { 90 return false; 92 } 93 94 public int getStatus() throws SystemException { 95 return status; 96 } 97 98 public void registerSynchronization(Synchronization sync) 99 throws RollbackException , IllegalStateException , SystemException { 100 synchronizations.add(sync); 101 } 102 103 public void rollback() throws IllegalStateException , SystemException { 104 105 status = Status.STATUS_ROLLING_BACK; 106 107 113 status = Status.STATUS_ROLLEDBACK; 114 115 try { 116 connection.rollback(); 117 connection.close(); 118 } 119 catch (SQLException sqle) { 120 status = Status.STATUS_UNKNOWN; 121 throw new SystemException (); 122 } 123 124 for ( int i=0; i<synchronizations.size(); i++ ) { 125 Synchronization s = (Synchronization ) synchronizations.get(i); 126 s.afterCompletion(status); 127 } 128 129 transactionManager.endCurrent(this); 131 } 132 133 public void setRollbackOnly() throws IllegalStateException , SystemException { 134 status = Status.STATUS_MARKED_ROLLBACK; 135 } 136 137 void setConnection(Connection connection) { 138 this.connection = connection; 139 } 140 141 public Connection getConnection() { 142 return connection; 143 } 144 } 145 | Popular Tags |