|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DummyBaseTransactionManager.java | 50% | 81.2% | 80% | 74.1% |
|
1 | package org.jboss.cache.transaction; | |
2 | ||
3 | import javax.transaction.HeuristicMixedException; | |
4 | import javax.transaction.HeuristicRollbackException; | |
5 | import javax.transaction.InvalidTransactionException; | |
6 | import javax.transaction.NotSupportedException; | |
7 | import javax.transaction.RollbackException; | |
8 | import javax.transaction.Status; | |
9 | import javax.transaction.SystemException; | |
10 | import javax.transaction.Transaction; | |
11 | import javax.transaction.TransactionManager; | |
12 | ||
13 | /** | |
14 | * @author bela | |
15 | * @version $Revision: 1.4 $ | |
16 | * Date: May 15, 2003 | |
17 | * Time: 4:11:37 PM | |
18 | */ | |
19 | public class DummyBaseTransactionManager implements TransactionManager, java.io.Serializable | |
20 | { | |
21 | static ThreadLocal<Transaction> thread_local = new ThreadLocal<Transaction>(); | |
22 | private static final long serialVersionUID = -6716097342564237376l; | |
23 | ||
24 | /** | |
25 | * Starts a new transaction, and associate it with the calling thread. | |
26 | * | |
27 | * @throws javax.transaction.NotSupportedException | |
28 | * If the calling thread is already | |
29 | * associated with a transaction, and nested transactions are | |
30 | * not supported. | |
31 | * @throws javax.transaction.SystemException | |
32 | * If the transaction service fails in an | |
33 | * unexpected way. | |
34 | */ | |
35 | 1121975 | public void begin() throws NotSupportedException, SystemException |
36 | { | |
37 | 1121975 | Transaction currentTx; |
38 | ? | if ((currentTx = getTransaction()) != null) |
39 | 0 | throw new NotSupportedException(Thread.currentThread() + |
40 | " is already associated with a transaction (" + currentTx + ")"); | |
41 | 1121975 | DummyTransaction tx = new DummyTransaction(this); |
42 | 1121975 | setTransaction(tx); |
43 | } | |
44 | ||
45 | /** | |
46 | * Commit the transaction associated with the calling thread. | |
47 | * | |
48 | * @throws javax.transaction.RollbackException | |
49 | * If the transaction was marked for rollback | |
50 | * only, the transaction is rolled back and this exception is | |
51 | * thrown. | |
52 | * @throws IllegalStateException If the calling thread is not associated | |
53 | * with a transaction. | |
54 | * @throws javax.transaction.SystemException | |
55 | * If the transaction service fails in an | |
56 | * unexpected way. | |
57 | * @throws javax.transaction.HeuristicMixedException | |
58 | * If a heuristic decision was made and | |
59 | * some some parts of the transaction have been committed while | |
60 | * other parts have been rolled back. | |
61 | * @throws javax.transaction.HeuristicRollbackException | |
62 | * If a heuristic decision to roll | |
63 | * back the transaction was made. | |
64 | * @throws SecurityException If the caller is not allowed to commit this | |
65 | * transaction. | |
66 | */ | |
67 | 1120594 | public void commit() throws RollbackException, HeuristicMixedException, |
68 | HeuristicRollbackException, SecurityException, | |
69 | IllegalStateException, SystemException | |
70 | { | |
71 | 1120594 | int status; |
72 | 1120594 | Transaction tx = getTransaction(); |
73 | 1120594 | if (tx == null) |
74 | 2 | throw new IllegalStateException("thread not associated with transaction"); |
75 | 1120592 | status = tx.getStatus(); |
76 | 1120592 | if (status == Status.STATUS_MARKED_ROLLBACK) |
77 | 0 | throw new RollbackException(); |
78 | 1120592 | tx.commit(); |
79 | ||
80 | // Disassociate tx from thread. | |
81 | 1120545 | setTransaction(null); |
82 | } | |
83 | ||
84 | /** | |
85 | * Rolls back the transaction associated with the calling thread. | |
86 | * | |
87 | * @throws IllegalStateException If the transaction is in a state | |
88 | * where it cannot be rolled back. This could be because the | |
89 | * calling thread is not associated with a transaction, or | |
90 | * because it is in the | |
91 | * {@link javax.transaction.Status#STATUS_PREPARED prepared state}. | |
92 | * @throws SecurityException If the caller is not allowed to roll back | |
93 | * this transaction. | |
94 | * @throws javax.transaction.SystemException | |
95 | * If the transaction service fails in an | |
96 | * unexpected way. | |
97 | */ | |
98 | 351 | public void rollback() throws IllegalStateException, SecurityException, |
99 | SystemException | |
100 | { | |
101 | 351 | Transaction tx = getTransaction(); |
102 | 351 | if (tx == null) |
103 | 154 | throw new IllegalStateException("no transaction associated with thread"); |
104 | 197 | tx.rollback(); |
105 | ||
106 | // Disassociate tx from thread. | |
107 | 197 | setTransaction(null); |
108 | } | |
109 | ||
110 | /** | |
111 | * Mark the transaction associated with the calling thread for rollback | |
112 | * only. | |
113 | * | |
114 | * @throws IllegalStateException If the transaction is in a state | |
115 | * where it cannot be rolled back. This could be because the | |
116 | * calling thread is not associated with a transaction, or | |
117 | * because it is in the | |
118 | * {@link javax.transaction.Status#STATUS_PREPARED prepared state}. | |
119 | * @throws javax.transaction.SystemException | |
120 | * If the transaction service fails in an | |
121 | * unexpected way. | |
122 | */ | |
123 | 16 | public void setRollbackOnly() throws IllegalStateException, SystemException |
124 | { | |
125 | 16 | Transaction tx = getTransaction(); |
126 | 16 | if (tx == null) |
127 | 0 | throw new IllegalStateException("no transaction associated with calling thread"); |
128 | 16 | tx.setRollbackOnly(); |
129 | } | |
130 | ||
131 | /** | |
132 | * Get the status of the transaction associated with the calling thread. | |
133 | * | |
134 | * @return The status of the transaction. This is one of the | |
135 | * {@link javax.transaction.Status} constants. If no transaction is associated | |
136 | * with the calling thread, | |
137 | * {@link javax.transaction.Status#STATUS_NO_TRANSACTION} is returned. | |
138 | * @throws javax.transaction.SystemException | |
139 | * If the transaction service fails in an | |
140 | * unexpected way. | |
141 | */ | |
142 | 0 | public int getStatus() throws SystemException |
143 | { | |
144 | 0 | Transaction tx = getTransaction(); |
145 | 0 | return tx != null ? tx.getStatus() : Status.STATUS_NO_TRANSACTION; |
146 | } | |
147 | ||
148 | /** | |
149 | * Get the transaction associated with the calling thread. | |
150 | * | |
151 | * @return The transaction associated with the calling thread, or | |
152 | * <code>null</code> if the calling thread is not associated | |
153 | * with a transaction. | |
154 | * @throws javax.transaction.SystemException | |
155 | * If the transaction service fails in an | |
156 | * unexpected way. | |
157 | */ | |
158 | 7827519 | public Transaction getTransaction() throws SystemException |
159 | { | |
160 | 7827524 | return thread_local.get(); |
161 | } | |
162 | ||
163 | /** | |
164 | * Change the transaction timeout for transactions started by the calling | |
165 | * thread with the {@link #begin()} method. | |
166 | * | |
167 | * @param seconds The new timeout value, in seconds. If this parameter | |
168 | * is <code>0</code>, the timeout value is reset to the default | |
169 | * value. | |
170 | * @throws javax.transaction.SystemException | |
171 | * If the transaction service fails in an | |
172 | * unexpected way. | |
173 | */ | |
174 | 0 | public void setTransactionTimeout(int seconds) throws SystemException |
175 | { | |
176 | 0 | throw new SystemException("not supported"); |
177 | } | |
178 | ||
179 | /** | |
180 | * Suspend the association the calling thread has to a transaction, | |
181 | * and return the suspended transaction. | |
182 | * When returning from this method, the calling thread is no longer | |
183 | * associated with a transaction. | |
184 | * | |
185 | * @return The transaction that the calling thread was associated with, | |
186 | * or <code>null</code> if the calling thread was not associated | |
187 | * with a transaction. | |
188 | * @throws javax.transaction.SystemException | |
189 | * If the transaction service fails in an | |
190 | * unexpected way. | |
191 | */ | |
192 | 2300 | public Transaction suspend() throws SystemException |
193 | { | |
194 | 2300 | Transaction retval = getTransaction(); |
195 | 2300 | setTransaction(null); |
196 | 2300 | return retval; |
197 | } | |
198 | ||
199 | /** | |
200 | * Resume the association of the calling thread with the given | |
201 | * transaction. | |
202 | * | |
203 | * @param tx The transaction to be associated with the calling thread. | |
204 | * @throws javax.transaction.InvalidTransactionException | |
205 | * If the argument does not represent | |
206 | * a valid transaction. | |
207 | * @throws IllegalStateException If the calling thread is already | |
208 | * associated with a transaction. | |
209 | * @throws javax.transaction.SystemException | |
210 | * If the transaction service fails in an | |
211 | * unexpected way. | |
212 | */ | |
213 | 833 | public void resume(Transaction tx) throws InvalidTransactionException, IllegalStateException, SystemException |
214 | { | |
215 | 833 | setTransaction(tx); |
216 | } | |
217 | ||
218 | /** | |
219 | * Just used for unit tests | |
220 | * | |
221 | * @param tx | |
222 | */ | |
223 | 3368114 | public void setTransaction(Transaction tx) |
224 | { | |
225 | 3368114 | thread_local.set(tx); |
226 | } | |
227 | ||
228 | } |
|