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 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 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public abstract class ManagedTransactionFactory implements TransactionFactory |
38 | |
{ |
39 | |
|
40 | |
private HashMap _transactionMap; |
41 | |
private TransactionManager tm; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public ManagedTransactionFactory() |
47 | |
{ |
48 | |
_transactionMap = new HashMap(); |
49 | |
} |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
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 | |
|
81 | |
tx.registerConnection(baseConnection); |
82 | |
return tx; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public OTMConnection acquireConnection(PBKey pbKey) |
89 | |
{ |
90 | |
return new ManagedConnection(pbKey); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
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 | |
|
130 | |
|
131 | |
|
132 | |
public void transactionBegin() throws TransactionException |
133 | |
{ |
134 | |
|
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public void transactionPrepare() throws TransactionException |
141 | |
{ |
142 | |
|
143 | |
|
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public void transactionCommit() throws TransactionException |
150 | |
{ |
151 | |
|
152 | |
|
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public void transactionRollback() throws TransactionException |
159 | |
{ |
160 | |
|
161 | |
|
162 | |
} |
163 | |
|
164 | |
} |
165 | |
} |