1 package org.apache.ojb.otm.transaction; 2 3 17 18 import org.apache.ojb.broker.PBKey; 19 import org.apache.ojb.otm.OTMConnection; 20 import org.apache.ojb.otm.core.BaseConnection; 21 import org.apache.ojb.otm.core.Transaction; 22 import org.apache.ojb.otm.core.TransactionException; 23 24 import java.util.HashMap ; 25 26 33 public class LocalTransactionFactory implements TransactionFactory 34 { 35 36 private HashMap _transactionMap; 37 38 public LocalTransactionFactory () 39 { 40 _transactionMap = new HashMap (); 41 } 42 43 46 public Transaction getTransactionForConnection (OTMConnection connection) 47 { 48 if (!(connection instanceof BaseConnection)) 49 { 50 StringBuffer msg = new StringBuffer (); 51 msg.append("Unknown connection type: "); 52 if (connection != null) 53 msg.append(connection.getClass().getName()); 54 else 55 msg.append(" null. Make sure you pass a non-null OTMConnection to this method. An OTMConnection can be acquired by calling acquireConnection (PBKey pbKey)"); 56 throw new TransactionFactoryException(msg.toString()); 57 } 58 59 Transaction tx = (Transaction) _transactionMap.get(connection); 60 if (tx == null) 61 { 62 tx = new Transaction(); 63 _transactionMap.put(connection, tx); 64 } 65 tx.registerConnection(connection); 67 return tx; 68 } 69 70 71 74 public OTMConnection acquireConnection (PBKey pbKey) 75 { 76 OTMConnection newConnection = new LocalConnection(pbKey); 77 getTransactionForConnection(newConnection); 79 return newConnection; 80 } 81 82 90 private static class LocalConnection extends BaseConnection 91 { 92 public LocalConnection (PBKey pbKey) 93 { 94 super(pbKey); 95 } 96 97 98 101 public void transactionBegin() throws TransactionException 102 { 103 getKernelBroker().beginTransaction(); 104 } 105 106 109 public void transactionPrepare() throws TransactionException 110 { 111 } 113 114 117 public void transactionCommit() throws TransactionException 118 { 119 getKernelBroker().commitTransaction(); 120 } 121 122 125 public void transactionRollback() throws TransactionException 126 { 127 getKernelBroker().abortTransaction(); 128 } 129 130 } 131 } 132 | Popular Tags |