1 19 20 package org.apache.cayenne.access; 21 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import org.apache.cayenne.CayenneException; 28 29 35 public abstract class Transaction { 36 37 42 static final ThreadLocal currentTransaction = new InheritableThreadLocal (); 43 44 private static final Transaction NO_TRANSACTION = new Transaction() { 45 46 public void begin() { 47 48 } 49 50 public void commit() { 51 52 } 53 54 public void rollback() { 55 56 } 57 }; 58 59 public static final int STATUS_ACTIVE = 1; 60 public static final int STATUS_COMMITTING = 2; 61 public static final int STATUS_COMMITTED = 3; 62 public static final int STATUS_ROLLEDBACK = 4; 63 public static final int STATUS_ROLLING_BACK = 5; 64 public static final int STATUS_NO_TRANSACTION = 6; 65 public static final int STATUS_MARKED_ROLLEDBACK = 7; 66 67 protected Map connections; 68 protected int status; 69 protected TransactionDelegate delegate; 70 71 static String decodeStatus(int status) { 72 switch (status) { 73 case STATUS_ACTIVE: 74 return "STATUS_ACTIVE"; 75 case STATUS_COMMITTING: 76 return "STATUS_COMMITTING"; 77 case STATUS_COMMITTED: 78 return "STATUS_COMMITTED"; 79 case STATUS_ROLLEDBACK: 80 return "STATUS_ROLLEDBACK"; 81 case STATUS_ROLLING_BACK: 82 return "STATUS_ROLLING_BACK"; 83 case STATUS_NO_TRANSACTION: 84 return "STATUS_NO_TRANSACTION"; 85 case STATUS_MARKED_ROLLEDBACK: 86 return "STATUS_MARKED_ROLLEDBACK"; 87 default: 88 return "Unknown Status - " + status; 89 } 90 } 91 92 97 public static void bindThreadTransaction(Transaction transaction) { 98 currentTransaction.set(transaction); 99 } 100 101 107 public static Transaction getThreadTransaction() { 108 return (Transaction) currentTransaction.get(); 109 } 110 111 116 public static Transaction internalTransaction(TransactionDelegate delegate) { 117 return new InternalTransaction(delegate); 118 } 119 120 125 public static Transaction externalTransaction(TransactionDelegate delegate) { 126 return new ExternalTransaction(delegate); 127 } 128 129 134 public static Transaction noTransaction() { 135 return NO_TRANSACTION; 136 } 137 138 141 protected Transaction() { 142 status = STATUS_NO_TRANSACTION; 143 } 144 145 public TransactionDelegate getDelegate() { 146 return delegate; 147 } 148 149 public void setDelegate(TransactionDelegate delegate) { 150 this.delegate = delegate; 151 } 152 153 public int getStatus() { 154 return status; 155 } 156 157 public synchronized void setRollbackOnly() { 158 setStatus(STATUS_MARKED_ROLLEDBACK); 159 } 160 161 public synchronized void setStatus(int status) { 162 if (delegate != null 163 && status == STATUS_MARKED_ROLLEDBACK 164 && !delegate.willMarkAsRollbackOnly(this)) { 165 return; 166 } 167 168 this.status = status; 169 } 170 171 175 public abstract void begin(); 176 177 public abstract void commit() throws IllegalStateException , SQLException , 178 CayenneException; 179 180 public abstract void rollback() throws IllegalStateException , SQLException , 181 CayenneException; 182 183 186 public Connection getConnection(String name) { 187 return (connections != null) ? (Connection ) connections.get(name) : null; 188 } 189 190 193 public boolean addConnection(String name, Connection connection) throws SQLException { 194 if (delegate != null && !delegate.willAddConnection(this, connection)) { 195 return false; 196 } 197 198 if (connections == null) { 199 connections = new HashMap (); 200 } 201 202 return connections.put(name, connection) != connection; 203 } 204 } 205 | Popular Tags |