1 package org.apache.ojb.otm.transaction; 2 3 17 18 import java.util.HashMap ; 19 20 import javax.transaction.SystemException ; 21 import javax.transaction.TransactionManager ; 22 23 import org.apache.ojb.broker.PBKey; 24 import org.apache.ojb.broker.transaction.tm.TransactionManagerFactoryFactory; 25 import org.apache.ojb.broker.transaction.tm.TransactionManagerFactoryException; 26 import org.apache.ojb.otm.OTMConnection; 27 import org.apache.ojb.otm.core.BaseConnection; 28 import org.apache.ojb.otm.core.Transaction; 29 import org.apache.ojb.otm.core.TransactionException; 30 31 37 public abstract class ManagedTransactionFactory implements TransactionFactory 38 { 39 40 private HashMap _transactionMap; 41 private TransactionManager tm; 42 43 46 public ManagedTransactionFactory() 47 { 48 _transactionMap = new HashMap (); 49 } 50 51 52 56 59 public Transaction getTransactionForConnection(OTMConnection connection) 60 { 61 if (!(connection instanceof BaseConnection)) 62 { 63 throw new TransactionFactoryException("Unknown connection type"); 64 } 65 BaseConnection baseConnection = (BaseConnection) connection; 66 67 javax.transaction.Transaction jtaTx = getJTATransaction(); 68 if (jtaTx == null) 69 { 70 throw new TransactionFactoryException("Unable to get the JTA Transaction"); 71 } 72 73 Transaction tx = (Transaction) _transactionMap.get(jtaTx); 74 if (tx == null) 75 { 76 tx = new Transaction(); 77 _transactionMap.put(jtaTx, tx); 78 } 79 80 tx.registerConnection(baseConnection); 82 return tx; 83 } 84 85 88 public OTMConnection acquireConnection(PBKey pbKey) 89 { 90 return new ManagedConnection(pbKey); 91 } 92 93 94 98 public javax.transaction.Transaction getJTATransaction() 99 { 100 if(tm == null) 101 { 102 try 103 { 104 tm = TransactionManagerFactoryFactory.instance().getTransactionManager(); 105 } 106 catch (TransactionManagerFactoryException e) 107 { 108 throw new TransactionFactoryException("Can't instantiate TransactionManagerFactory", e); 109 } 110 } 111 try 112 { 113 return tm.getTransaction(); 114 } 115 catch(SystemException e) 116 { 117 throw new TransactionFactoryException("Error acquiring JTA Transaction", e); 118 } 119 } 120 121 private static class ManagedConnection extends BaseConnection 122 { 123 124 public ManagedConnection (PBKey pbKey) 125 { 126 super(pbKey); 127 } 128 129 132 public void transactionBegin() throws TransactionException 133 { 134 } 136 137 140 public void transactionPrepare() throws TransactionException 141 { 142 } 145 146 149 public void transactionCommit() throws TransactionException 150 { 151 } 154 155 158 public void transactionRollback() throws TransactionException 159 { 160 } 163 164 } 165 } 166 | Popular Tags |