01: /*
02: * Created on Nov 5, 2004
03: */
04: package uk.org.ponder.transaction;
05:
06: import java.util.ArrayList;
07:
08: import uk.org.ponder.util.UniversalRuntimeException;
09:
10: /**
11: * @author Antranig Basman (antranig@caret.cam.ac.uk)
12: *
13: */
14: public class TransactionThreadMap {
15: // largely for debugging purposes, this keeps a registry of all
16: // TransactionThreadMaps in the system for assertAllConcluded().
17: private static ArrayList allmaps = new ArrayList();
18:
19: public TransactionThreadMap() {
20: allmaps.add(this );
21: }
22:
23: private ThreadLocal transmap = new ThreadLocal() {
24: };
25:
26: // this is currently only called by NestedTransactionWrapper constructor
27: public void enterTransaction(Transaction tran) {
28: //Logger.log.info("*|*|*|*|*| Thread " + Thread.currentThread() + " entered transaction");
29: transmap.set(tran);
30: }
31:
32: public Transaction getTransaction() {
33: return (Transaction) transmap.get();
34: }
35:
36: public void endTransaction() {
37: //Logger.log.info("*|*|*|*|*| Thread " + Thread.currentThread() + " left transaction");
38: transmap.set(null);
39: }
40:
41: public void assertTransactionsConcluded() {
42: Transaction trans = getTransaction();
43: if (trans != null) {
44: try {
45: trans.rollback();
46: } catch (Exception e) {
47: }
48: throw new UniversalRuntimeException(
49: "Outstanding transaction " + trans
50: + " discovered on thread "
51: + Thread.currentThread());
52: }
53: }
54:
55: /** Throws an exception if this thread has an outstanding transaction on
56: * any transaction map in the system.
57: */
58: public static void assertAllTransactionsConcluded() {
59: for (int i = 0; i < allmaps.size(); ++i) {
60: ((TransactionThreadMap) allmaps.get(i))
61: .assertTransactionsConcluded();
62: }
63: }
64:
65: }
|