1 package org.apache.ojb.otm.transaction;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
27
28
29
30
31
32
33 public class LocalTransactionFactory implements TransactionFactory
34 {
35
36 private HashMap _transactionMap;
37
38 public LocalTransactionFactory ()
39 {
40 _transactionMap = new HashMap();
41 }
42
43
44
45
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
66 tx.registerConnection(connection);
67 return tx;
68 }
69
70
71
72
73
74 public OTMConnection acquireConnection (PBKey pbKey)
75 {
76 OTMConnection newConnection = new LocalConnection(pbKey);
77
78 getTransactionForConnection(newConnection);
79 return newConnection;
80 }
81
82
83
84
85
86
87
88
89
90 private static class LocalConnection extends BaseConnection
91 {
92 public LocalConnection (PBKey pbKey)
93 {
94 super(pbKey);
95 }
96
97
98
99
100
101 public void transactionBegin() throws TransactionException
102 {
103 getKernelBroker().beginTransaction();
104 }
105
106
107
108
109 public void transactionPrepare() throws TransactionException
110 {
111
112 }
113
114
115
116
117 public void transactionCommit() throws TransactionException
118 {
119 getKernelBroker().commitTransaction();
120 }
121
122
123
124
125 public void transactionRollback() throws TransactionException
126 {
127 getKernelBroker().abortTransaction();
128 }
129
130 }
131 }