1 3 package jodd.jtx; 4 5 import java.util.Map ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 9 30 public class JtxTransaction { 31 32 34 38 public static final int STATUS_AUTOCOMMIT = -1; 39 40 43 public static final int STATUS_ACTIVE = 0; 44 45 50 public static final int STATUS_COMMITTED = 3; 51 52 55 public static final int STATUS_ROLLEDBACK = 4; 56 57 60 public static final int STATUS_NO_TRANSACTION = 6; 61 62 66 public static final int STATUS_COMMITTING = 8; 67 68 72 public static final int STATUS_ROLLING_BACK = 9; 73 74 75 77 protected JtxTransactionManager manager; 78 protected JtxTransactionMode mode; 79 protected Map resources; 80 protected int status; 81 82 88 JtxTransaction(JtxTransactionManager manager, JtxTransactionMode mode) { 89 this.manager = manager; 90 this.mode = mode; 91 this.resources = new HashMap (); 92 if ((mode.propagation == JtxTransactionMode.PROPAGATION_NOT_SUPPORTED) || 93 (mode.propagation == JtxTransactionMode.PROPAGATION_NEVER)) { 94 this.status = STATUS_AUTOCOMMIT; 95 } else { 96 this.status = STATUS_NO_TRANSACTION; 97 } 98 } 99 100 102 105 public int getStatus() { 106 return status; 107 } 108 109 112 public boolean isActive() { 113 return status == STATUS_ACTIVE; 114 } 115 116 120 public boolean isAutocommit() { 121 return status == STATUS_AUTOCOMMIT; 122 } 123 124 127 public boolean isCommitted() { 128 return status == STATUS_COMMITTED; 129 } 130 131 134 public boolean isRolledback() { 135 return status == STATUS_ROLLEDBACK; 136 } 137 138 139 141 144 public void begin() { 145 if (status == STATUS_AUTOCOMMIT) { 146 throw new JtxException("Transaction is not availiable in autocommit mode."); 147 } 148 if (status != STATUS_NO_TRANSACTION) { 149 throw new JtxException("Transaction already started."); 150 } 151 status = STATUS_ACTIVE; 152 } 153 154 160 public void commit() { 161 commitOrRollback(true); 162 } 163 164 171 public void rollback() { 172 commitOrRollback(false); 173 } 174 175 protected void commitOrRollback(boolean commit) { 176 if (status != STATUS_AUTOCOMMIT) { 177 if (status != STATUS_ACTIVE) { 178 throw new JtxException(commit ? "No active transaction to commit." : "No active transaction to roll back."); 179 } 180 status = commit ? STATUS_COMMITTING : STATUS_ROLLING_BACK; 181 Iterator it = resources.values().iterator(); 182 while (it.hasNext()) { 183 JtxResource resource = (JtxResource) it.next(); 184 if (resource.isTransactionActive() == true) { 185 if (commit) { 186 resource.commitTransaction(); 187 } else { 188 resource.rollbackTransaction(); 189 } 190 } 191 } 192 } 193 closeResources(); 194 status = commit ? STATUS_COMMITTED : STATUS_ROLLEDBACK; 195 manager.removeTransaction(this); 196 } 197 198 protected void closeResources() { 199 Iterator it = resources.values().iterator(); 200 while (it.hasNext()) { 201 JtxResource resource = (JtxResource) it.next(); 202 resource.closeResource(); 203 } 204 resources.clear(); 205 } 206 207 208 210 215 protected void attachResource(String resourceType, JtxResource resource) { 216 if (resources.put(resourceType, resource) != null) { 217 throw new JtxException("Resource of the same type already attached to this transaction."); 218 } 219 if (manager.isSingleResourceTransaction() == true) { 220 if (resources.size() != 1) { 221 throw new JtxException("Only one resource may be attached to transactions created by this transaction manager."); 222 } 223 } 224 if (status != STATUS_AUTOCOMMIT) { 225 resource.beginTransaction(mode); 226 } 227 } 228 229 237 public JtxResource getResource(String resourceType) { 238 JtxResource resource = (JtxResource) resources.get(resourceType); 239 if (resource == null) { 240 JtxResourceManager resourceManager = manager.lookupResourceManager(resourceType); 241 resource = resourceManager.createResource(); 242 attachResource(resourceType, resource); 243 } 244 return resource; 245 } 246 } 247 | Popular Tags |