1 19 20 package org.apache.cayenne.access; 21 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 import java.util.Iterator ; 25 26 import org.apache.cayenne.CayenneException; 27 28 34 class ExternalTransaction extends Transaction { 35 36 ExternalTransaction() { 37 } 38 39 ExternalTransaction(TransactionDelegate delegate) { 40 setDelegate(delegate); 41 } 42 43 public synchronized void begin() { 44 if (status != Transaction.STATUS_NO_TRANSACTION) { 45 throw new IllegalStateException ( 46 "Transaction must have 'STATUS_NO_TRANSACTION' to begin. " 47 + "Current status: " 48 + Transaction.decodeStatus(status)); 49 } 50 51 status = Transaction.STATUS_ACTIVE; 52 } 53 54 public boolean addConnection(String name, Connection connection) throws SQLException { 55 if (super.addConnection(name, connection)) { 56 57 if (status == Transaction.STATUS_NO_TRANSACTION) { 59 begin(); 60 } 61 62 if (status != Transaction.STATUS_ACTIVE) { 63 throw new IllegalStateException ( 64 "Transaction must have 'STATUS_ACTIVE' to add a connection. " 65 + "Current status: " 66 + Transaction.decodeStatus(status)); 67 } 68 69 fixConnectionState(connection); 70 return true; 71 } 72 else { 73 return false; 74 } 75 76 } 77 78 public void commit() throws IllegalStateException , SQLException , CayenneException { 79 try { 80 if (status == Transaction.STATUS_NO_TRANSACTION) { 81 return; 82 } 83 84 if (delegate != null && !delegate.willCommit(this)) { 85 return; 86 } 87 88 if (status != Transaction.STATUS_ACTIVE) { 89 throw new IllegalStateException ( 90 "Transaction must have 'STATUS_ACTIVE' to be committed. " 91 + "Current status: " 92 + Transaction.decodeStatus(status)); 93 } 94 95 processCommit(); 96 97 status = Transaction.STATUS_COMMITTED; 98 if (delegate != null) { 99 delegate.didCommit(this); 100 } 101 } 102 finally { 103 close(); 104 } 105 } 106 107 public void rollback() throws IllegalStateException , SQLException , CayenneException { 108 109 try { 110 if (status == Transaction.STATUS_NO_TRANSACTION 111 || status == Transaction.STATUS_ROLLEDBACK 112 || status == Transaction.STATUS_ROLLING_BACK) { 113 return; 114 } 115 116 if (delegate != null && !delegate.willRollback(this)) { 117 return; 118 } 119 120 if (status != Transaction.STATUS_ACTIVE 121 && status != Transaction.STATUS_MARKED_ROLLEDBACK) { 122 throw new IllegalStateException ( 123 "Transaction must have 'STATUS_ACTIVE' or 'STATUS_MARKED_ROLLEDBACK' to be rolled back. " 124 + "Current status: " 125 + Transaction.decodeStatus(status)); 126 } 127 128 processRollback(); 129 130 status = Transaction.STATUS_ROLLEDBACK; 131 if (delegate != null) { 132 delegate.didRollback(this); 133 } 134 } 135 finally { 136 close(); 137 } 138 } 139 140 void fixConnectionState(Connection connection) throws SQLException { 141 } 143 144 void processCommit() throws SQLException , CayenneException { 145 QueryLogger 146 .logCommitTransaction("no commit - transaction controlled externally."); 147 } 148 149 void processRollback() throws SQLException , CayenneException { 150 QueryLogger 151 .logRollbackTransaction("no rollback - transaction controlled externally."); 152 } 153 154 157 void close() { 158 if (connections == null || connections.isEmpty()) { 159 return; 160 } 161 162 Iterator it = connections.values().iterator(); 163 while (it.hasNext()) { 164 try { 165 166 ((Connection ) it.next()).close(); 167 } 168 catch (Throwable th) { 169 } 172 } 173 } 174 } 175 | Popular Tags |