KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > db > pse > util > TransactionMgr


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.db.pse.util;
15
16 import COM.odi.*;
17
18
19 /**
20  * ObjectStore PSE does not allow nested transactions, nor does it allow
21  * multiple active sessions, so we have to coordinate access to transactions
22  * to prevent multiple threads from entering a transaction.
23  */

24 public class TransactionMgr
25 {
26   private Thread JavaDoc m_mainThread;
27   private Thread JavaDoc m_owner = null;
28   private int m_refCount = 0;
29
30
31   public TransactionMgr()
32   {
33       // remember the main thread
34
m_mainThread = Thread.currentThread();
35   }
36
37
38   public synchronized void begin()
39   {
40       // initialize this thread with the database
41
ObjectStore.initialize(m_mainThread);
42
43     Thread JavaDoc currentThread = Thread.currentThread();
44
45       // allow the same thread to acquire more than once
46
if (currentThread != m_owner) {
47       while (m_refCount != 0) {
48         try {
49           wait();
50         }
51         catch (InterruptedException JavaDoc e) {
52         }
53       }
54     }
55
56       // since we allow the same thread to call begin() more than once,
57
// we don't want to worry about conflicting transaction modes
58
if (m_refCount == 0)
59       Transaction.begin(ObjectStore.UPDATE);
60
61     m_owner = currentThread;
62     m_refCount++;
63   }
64
65
66   public synchronized void commit(int retain)
67   {
68     Thread JavaDoc currentThread = Thread.currentThread();
69     if (currentThread != m_owner)
70       throw new RuntimeException JavaDoc("Thread not owner");
71
72     if (m_refCount > 0)
73       m_refCount--;
74     else if (m_refCount == 0)
75       throw new RuntimeException JavaDoc("refCount should not be 0");
76
77       // commit the transaction if this is the last release()
78
if (m_refCount == 0) {
79       Transaction.current().commit(retain);
80         // disconnect this thread from the database
81
ObjectStore.shutdown(false);
82       m_owner = null;
83       notifyAll();
84     }
85   }
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
Popular Tags