1 25 package org.ofbiz.shark.transaction; 26 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.entity.transaction.GenericTransactionException; 32 import org.ofbiz.entity.transaction.TransactionUtil; 33 34 import org.enhydra.shark.api.ApplicationMappingTransaction; 35 import org.enhydra.shark.api.ParticipantMappingTransaction; 36 import org.enhydra.shark.api.TransactionException; 37 import org.enhydra.shark.api.UserTransaction; 38 import org.enhydra.shark.api.RepositoryTransaction; 39 import org.enhydra.shark.api.RootException; 40 import org.enhydra.shark.api.internal.transaction.SharkInternalTransaction; 41 import org.enhydra.shark.api.internal.working.WfProcessInternal; 42 import org.enhydra.shark.api.internal.working.WfResourceInternal; 43 44 51 public class JtaTransaction implements SharkInternalTransaction, UserTransaction, 52 ApplicationMappingTransaction, ParticipantMappingTransaction, RepositoryTransaction { 53 54 public static final String module = JtaTransaction.class.getName(); 55 public static final int transactionTimeout = 120; 56 57 protected Map resourceMap = new HashMap (); 58 protected Map processMap = new HashMap (); 59 60 protected boolean beganTransaction = false; 61 protected boolean active = false; 62 protected boolean enabled = true; 63 64 public JtaTransaction() { 65 if (enabled) { 66 try { 67 this.beganTransaction = TransactionUtil.begin(transactionTimeout); 68 active = true; 69 } catch (GenericTransactionException e) { 70 Debug.logError(e, module); 71 } 72 } 73 } 74 75 public void commit() throws TransactionException { 76 if (active) { 77 try { 78 TransactionUtil.commit(beganTransaction); 79 } catch (GenericTransactionException e) { 80 Debug.logError(e, module); 81 throw new TransactionException(e); 82 } 83 active = false; 84 } else { 85 Debug.logError(new Exception (), "No active transaction; unable to commit", module); 86 } 88 } 89 90 public void rollback() throws TransactionException { 91 if (active) { 92 try { 93 TransactionUtil.rollback(beganTransaction, "Transaction rollback from Shark", null); 94 } catch (GenericTransactionException e) { 95 Debug.logError(e, module); 96 throw new TransactionException(e); 97 } 98 active = false; 99 } else { 100 Debug.logError(new Exception (), "No active transaction; unable to rollback", module); 101 } 103 } 104 105 public void release() throws TransactionException { 106 if (active) { 107 this.commit(); 108 } 109 } 110 111 public void addToTransaction(String procId, WfProcessInternal proc) throws RootException { 112 this.processMap.put(procId, proc); 113 } 114 115 public void addToTransaction(String resId, WfResourceInternal res) throws RootException { 116 this.resourceMap.put(resId, res); 117 } 118 119 public void removeProcess(String procId) throws RootException { 120 this.processMap.remove(procId); 121 } 122 123 public void removeResource(String resId) throws RootException { 124 this.resourceMap.remove(resId); 125 } 126 127 public WfProcessInternal getProcess(String procId) throws RootException { 128 return (WfProcessInternal) this.processMap.get(procId); 129 } 130 131 public WfResourceInternal getResource(String resId) throws RootException { 132 return (WfResourceInternal) this.resourceMap.get(resId); 133 } 134 } 135 | Popular Tags |