Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LocalTxManager |
|
| 1.1818181818181819;1.182 | ||||
LocalTxManager$TransactionTable |
|
| 1.1818181818181819;1.182 |
1 | package org.apache.ojb.odmg; | |
2 | ||
3 | /* Copyright 2002-2005 The Apache Software Foundation | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | import java.util.Hashtable; | |
19 | ||
20 | import org.apache.ojb.broker.util.configuration.Configuration; | |
21 | import org.odmg.TransactionNotInProgressException; | |
22 | ||
23 | /** | |
24 | * In a non-appserver environment, without a transaction manager, we can | |
25 | * safely associate the current ODMG transaction with the calling thread. | |
26 | * | |
27 | * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a> | |
28 | * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a> | |
29 | * @version $Id: LocalTxManager.java,v 1.1 2007-08-24 22:17:37 ewestfal Exp $ | |
30 | */ | |
31 | public class LocalTxManager implements OJBTxManager | |
32 | { | |
33 | /** | |
34 | * Internal table which provides mapping between threads and transactions. | |
35 | * This is required because a Thread can join a Transaction already in | |
36 | * progress. | |
37 | * If the thread joins a transaction, then "getTransaction()" should return | |
38 | * the apropriate one. The only way we can ensure that is by keeping hold | |
39 | * of the txTable. | |
40 | */ | |
41 | private static TransactionTable tx_table = new TransactionTable(); | |
42 | ||
43 | public LocalTxManager() | |
44 | { | |
45 | ||
46 | } | |
47 | ||
48 | /** | |
49 | * Returns the current transaction for the calling thread. | |
50 | * | |
51 | * @throws org.odmg.TransactionNotInProgressException | |
52 | * {@link org.odmg.TransactionNotInProgressException} if no transaction was found. | |
53 | */ | |
54 | public TransactionImpl getCurrentTransaction() | |
55 | { | |
56 | TransactionImpl tx = tx_table.get(Thread.currentThread()); | |
57 | if(tx == null) | |
58 | { | |
59 | throw new TransactionNotInProgressException("Calling method needed transaction, but no transaction found for current thread :-("); | |
60 | } | |
61 | return tx; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Returns the current transaction for the calling thread or <code>null</code> | |
66 | * if no transaction was found. | |
67 | */ | |
68 | public TransactionImpl getTransaction() | |
69 | { | |
70 | return tx_table.get(Thread.currentThread()); | |
71 | } | |
72 | ||
73 | /** | |
74 | * add the current transaction to the map key'd by the calling thread. | |
75 | */ | |
76 | public void registerTx(TransactionImpl tx) | |
77 | { | |
78 | tx_table.put(Thread.currentThread(), tx); | |
79 | } | |
80 | ||
81 | /** | |
82 | * remove the current transaction from the map key'd by the calling thread. | |
83 | */ | |
84 | public void deregisterTx(Object token) | |
85 | { | |
86 | tx_table.remove(Thread.currentThread()); | |
87 | } | |
88 | ||
89 | /** | |
90 | * included to keep interface contract consistent. | |
91 | */ | |
92 | public void abortExternalTx(TransactionImpl odmgTrans) | |
93 | { | |
94 | /** | |
95 | * no op | |
96 | */ | |
97 | } | |
98 | ||
99 | public void configure(Configuration config) | |
100 | { | |
101 | ||
102 | } | |
103 | ||
104 | ||
105 | //======================================================= | |
106 | // inner class | |
107 | //======================================================= | |
108 | /** | |
109 | * TransactionTable provides a mapping between the calling | |
110 | * thread and the Transaction it is currently using. | |
111 | * One thread can be joined with one transaction at | |
112 | * a certain point in time. But a thread can join with | |
113 | * different transactions subsequently. | |
114 | * This mapping from threads to Transactions is based on ODMG. | |
115 | * | |
116 | * @author Thomas Mahler & David Dixon-Peugh | |
117 | */ | |
118 | static final class TransactionTable | |
119 | { | |
120 | /** | |
121 | * the internal Hashtable mapping Transactions to threads | |
122 | */ | |
123 | private Hashtable m_table = new Hashtable(); | |
124 | ||
125 | /** | |
126 | * Creates new TransactionTable | |
127 | */ | |
128 | public TransactionTable() | |
129 | { | |
130 | } | |
131 | ||
132 | /** | |
133 | * Retreive a Transaction associated with a thread. | |
134 | * | |
135 | * @param key_thread The thread to lookup. | |
136 | * @return The transaction associated with the thread. | |
137 | */ | |
138 | public TransactionImpl get(Thread key_thread) | |
139 | { | |
140 | return (TransactionImpl) m_table.get(key_thread); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Store the Thread/Transaction pair in the TransactionTable | |
145 | * | |
146 | * @param key_thread Thread that the transaction will be associated to | |
147 | * @param value_tx Transaction to be associated with the thread | |
148 | */ | |
149 | public void put(Thread key_thread, TransactionImpl value_tx) | |
150 | { | |
151 | m_table.put(key_thread, value_tx); | |
152 | } | |
153 | ||
154 | /** | |
155 | * Remove the entry for the thread | |
156 | * | |
157 | * @param key_thread Thread to be removed. | |
158 | */ | |
159 | public void remove(Thread key_thread) | |
160 | { | |
161 | m_table.remove(key_thread); | |
162 | } | |
163 | } | |
164 | } |