KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > store > T_TWC


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.store.T_TWC
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.store;
23
24 import org.apache.derby.iapi.store.raw.*;
25
26 import org.apache.derby.iapi.services.context.ContextManager;
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28 import org.apache.derby.iapi.services.context.ContextService;
29 import org.apache.derby.iapi.error.StandardException;
30 import org.apache.derby.iapi.services.locks.LockFactory;
31 import org.apache.derby.iapi.store.access.AccessFactoryGlobals;
32
33 /**
34     Transaction with context, a utility class for tests to create
35     multiple interleaving transactions.
36 */

37 public class T_TWC
38 {
39     protected Transaction tran;
40     protected ContextManager cm;
41     protected ContextService contextService;
42     protected LockFactory lf;
43     protected RawStoreFactory rawStore;
44
45     public T_TWC(ContextService contextService,
46                     LockFactory lockFactory,
47                     RawStoreFactory rawStoreFactory)
48     {
49         this.contextService = contextService;
50         this.lf = lockFactory;
51         this.rawStore = rawStoreFactory;
52         tran = null;
53         cm = null;
54     }
55
56     public T_TWC startUserTransaction()
57          throws StandardException
58     {
59         cm = contextService.newContextManager();
60         contextService.setCurrentContextManager(cm);
61         try {
62         tran =
63             rawStore.startTransaction(cm, AccessFactoryGlobals.USER_TRANS_NAME);
64
65         if (SanityManager.DEBUG)
66             SanityManager.ASSERT(tran != null);
67         checkNullLockCount();
68         }
69         finally {
70             contextService.resetCurrentContextManager(cm);
71         }
72         return this;
73     }
74
75     public void checkNullLockCount()
76     {
77         switchTransactionContext();
78         try {
79         if (SanityManager.DEBUG)
80             SanityManager.ASSERT(!lf.areLocksHeld(tran));
81         } finally {
82             resetContext();
83         }
84     }
85
86     public void setSavePoint(String JavaDoc sp, Object JavaDoc kindOfSavepoint)
87          throws StandardException
88     {
89         switchTransactionContext();
90         try {
91         tran.setSavePoint(sp, null);
92         } finally {
93             resetContext();
94         }
95     }
96
97     public void rollbackToSavePoint(String JavaDoc sp, Object JavaDoc kindOfSavepoint)
98          throws StandardException
99     {
100         switchTransactionContext();
101         try {
102             tran.rollbackToSavePoint(sp, null);
103         } finally {
104             resetContext();
105         }
106     }
107
108     public void switchTransactionContext()
109     {
110         contextService.setCurrentContextManager(cm);
111     }
112     public void resetContext() {
113         contextService.resetCurrentContextManager(cm);
114     }
115
116     public void logAndDo(Loggable l)
117          throws StandardException
118     {
119         switchTransactionContext();
120         try {
121             tran.logAndDo(l);
122         } finally {
123             resetContext();
124         }
125     }
126
127     public void commit()
128          throws StandardException
129     {
130         switchTransactionContext();
131         try {
132         tran.commit();
133         } finally {
134             resetContext();
135         }
136         checkNullLockCount();
137     }
138
139     public void abort()
140          throws StandardException
141     {
142         switchTransactionContext();
143         try {
144         tran.abort();
145         } finally {
146             resetContext();
147         }
148         checkNullLockCount();
149     }
150
151
152     public GlobalTransactionId getId()
153          throws StandardException
154     {
155         switchTransactionContext();
156         try {
157             return tran.getGlobalId();
158         } finally {
159             resetContext();
160         }
161
162     }
163 }
164
Popular Tags