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 InternalTransaction extends ExternalTransaction { 35 36 InternalTransaction(TransactionDelegate delegate) { 37 super(delegate); 38 } 39 40 public void begin() { 41 super.begin(); 42 QueryLogger.logBeginTransaction("transaction started."); 43 } 44 45 void fixConnectionState(Connection connection) throws SQLException { 46 if (connection.getAutoCommit()) { 47 50 try { 52 connection.setAutoCommit(false); 53 } 54 catch (SQLException cay179Ex) { 55 } 57 } 58 } 59 60 void processCommit() throws SQLException , CayenneException { 61 status = Transaction.STATUS_COMMITTING; 62 63 if (connections != null && connections.size() > 0) { 64 Throwable deferredException = null; 65 Iterator it = connections.values().iterator(); 66 while (it.hasNext()) { 67 Connection connection = (Connection ) it.next(); 68 try { 69 70 if (deferredException == null) { 71 connection.commit(); 72 } 73 else { 74 connection.rollback(); 78 } 79 80 } 81 catch (Throwable th) { 82 setRollbackOnly(); 86 87 deferredException = th; 90 } 91 } 92 93 if (deferredException != null) { 94 QueryLogger.logRollbackTransaction("transaction rolledback."); 95 if (deferredException instanceof SQLException ) { 96 throw (SQLException ) deferredException; 97 } 98 else { 99 throw new CayenneException(deferredException); 100 } 101 } 102 else { 103 QueryLogger.logCommitTransaction("transaction committed."); 104 } 105 } 106 } 107 108 void processRollback() throws SQLException , CayenneException { 109 status = Transaction.STATUS_ROLLING_BACK; 110 111 if (connections != null && connections.size() > 0) { 112 Throwable deferredException = null; 113 114 Iterator it = connections.values().iterator(); 115 while (it.hasNext()) { 116 Connection connection = (Connection ) it.next(); 117 118 try { 119 connection.rollback(); 121 } 122 catch (Throwable th) { 123 deferredException = th; 126 } 127 } 128 129 if (deferredException != null) { 130 if (deferredException instanceof SQLException ) { 131 throw (SQLException ) deferredException; 132 } 133 else { 134 throw new CayenneException(deferredException); 135 } 136 } 137 } 138 } 139 } 140 | Popular Tags |