001: /*
002:
003: Derby - Class org.apache.derbyTesting.unitTests.store.T_TWC
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.unitTests.store;
023:
024: import org.apache.derby.iapi.store.raw.*;
025:
026: import org.apache.derby.iapi.services.context.ContextManager;
027: import org.apache.derby.iapi.services.sanity.SanityManager;
028: import org.apache.derby.iapi.services.context.ContextService;
029: import org.apache.derby.iapi.error.StandardException;
030: import org.apache.derby.iapi.services.locks.LockFactory;
031: import org.apache.derby.iapi.store.access.AccessFactoryGlobals;
032:
033: /**
034: Transaction with context, a utility class for tests to create
035: multiple interleaving transactions.
036: */
037: public class T_TWC {
038: protected Transaction tran;
039: protected ContextManager cm;
040: protected ContextService contextService;
041: protected LockFactory lf;
042: protected RawStoreFactory rawStore;
043:
044: public T_TWC(ContextService contextService,
045: LockFactory lockFactory, RawStoreFactory rawStoreFactory) {
046: this .contextService = contextService;
047: this .lf = lockFactory;
048: this .rawStore = rawStoreFactory;
049: tran = null;
050: cm = null;
051: }
052:
053: public T_TWC startUserTransaction() throws StandardException {
054: cm = contextService.newContextManager();
055: contextService.setCurrentContextManager(cm);
056: try {
057: tran = rawStore.startTransaction(cm,
058: AccessFactoryGlobals.USER_TRANS_NAME);
059:
060: if (SanityManager.DEBUG)
061: SanityManager.ASSERT(tran != null);
062: checkNullLockCount();
063: } finally {
064: contextService.resetCurrentContextManager(cm);
065: }
066: return this ;
067: }
068:
069: public void checkNullLockCount() {
070: switchTransactionContext();
071: try {
072: if (SanityManager.DEBUG)
073: SanityManager.ASSERT(!lf.areLocksHeld(tran));
074: } finally {
075: resetContext();
076: }
077: }
078:
079: public void setSavePoint(String sp, Object kindOfSavepoint)
080: throws StandardException {
081: switchTransactionContext();
082: try {
083: tran.setSavePoint(sp, null);
084: } finally {
085: resetContext();
086: }
087: }
088:
089: public void rollbackToSavePoint(String sp, Object kindOfSavepoint)
090: throws StandardException {
091: switchTransactionContext();
092: try {
093: tran.rollbackToSavePoint(sp, null);
094: } finally {
095: resetContext();
096: }
097: }
098:
099: public void switchTransactionContext() {
100: contextService.setCurrentContextManager(cm);
101: }
102:
103: public void resetContext() {
104: contextService.resetCurrentContextManager(cm);
105: }
106:
107: public void logAndDo(Loggable l) throws StandardException {
108: switchTransactionContext();
109: try {
110: tran.logAndDo(l);
111: } finally {
112: resetContext();
113: }
114: }
115:
116: public void commit() throws StandardException {
117: switchTransactionContext();
118: try {
119: tran.commit();
120: } finally {
121: resetContext();
122: }
123: checkNullLockCount();
124: }
125:
126: public void abort() throws StandardException {
127: switchTransactionContext();
128: try {
129: tran.abort();
130: } finally {
131: resetContext();
132: }
133: checkNullLockCount();
134: }
135:
136: public GlobalTransactionId getId() throws StandardException {
137: switchTransactionContext();
138: try {
139: return tran.getGlobalId();
140: } finally {
141: resetContext();
142: }
143:
144: }
145: }
|