001: /*
002:
003: Derby - Class org.apache.derby.impl.store.raw.xact.XactContext
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.derby.impl.store.raw.xact;
023:
024: import org.apache.derby.iapi.reference.SQLState;
025:
026: // This is the recommended super-class for all contexts.
027: import org.apache.derby.iapi.services.context.ContextImpl;
028: import org.apache.derby.iapi.services.context.ContextManager;
029:
030: import org.apache.derby.iapi.services.sanity.SanityManager;
031:
032: import org.apache.derby.iapi.store.raw.RawStoreFactory;
033: import org.apache.derby.iapi.store.raw.xact.RawTransaction;
034:
035: import org.apache.derby.iapi.error.StandardException;
036:
037: import org.apache.derby.iapi.error.ExceptionSeverity;
038:
039: /**
040: The context associated with the transaction.
041:
042: This object stores the context associated with the raw store transaction
043: on the stack. It stores info about the transaction opened within a
044: context manager (ie. typically a single user) for a single RawStoreFactory.
045:
046: **/
047:
048: final class XactContext extends ContextImpl {
049:
050: private RawTransaction xact;
051: private RawStoreFactory factory;
052: private boolean abortAll; // true if any exception causes this transaction to be aborted.
053:
054: XactContext(ContextManager cm, String name, Xact xact,
055: boolean abortAll, RawStoreFactory factory) {
056: super (cm, name);
057:
058: this .xact = xact;
059: this .abortAll = abortAll;
060: this .factory = factory;
061: xact.xc = this ; // double link between transaction and myself
062: }
063:
064: /*
065: ** Context methods (most are implemented by super-class)
066: */
067:
068: /**
069: @exception StandardException Standard Cloudscape error policy
070: */
071: public void cleanupOnError(Throwable error)
072: throws StandardException {
073:
074: if (SanityManager.DEBUG) {
075: SanityManager.ASSERT(getContextManager() != null);
076: }
077:
078: boolean throwAway = false;
079:
080: if (error instanceof StandardException) {
081: StandardException se = (StandardException) error;
082:
083: if (abortAll) {
084: // any error aborts an internal/nested xact and its transaction
085:
086: if (se.getSeverity() < ExceptionSeverity.TRANSACTION_SEVERITY) {
087: throw StandardException
088: .newException(
089: SQLState.XACT_INTERNAL_TRANSACTION_EXCEPTION,
090: error);
091: }
092:
093: throwAway = true;
094:
095: } else {
096:
097: // If the severity is lower than a transaction error then do nothing.
098: if (se.getSeverity() < ExceptionSeverity.TRANSACTION_SEVERITY) {
099: return;
100: }
101:
102: // If the session is going to disappear then we want to close this
103: // transaction, not just abort it.
104: if (se.getSeverity() >= ExceptionSeverity.SESSION_SEVERITY)
105: throwAway = true;
106: }
107: } else {
108: // some java* error, throw away the transaction.
109: throwAway = true;
110: }
111:
112: try {
113:
114: if (xact != null) {
115: // abort the transaction
116: xact.abort();
117: }
118:
119: } catch (StandardException se) {
120: // if we get an error during abort then shut the system down
121: throwAway = true;
122:
123: // if the system was being shut down anyway, do nothing
124: if ((se.getSeverity() <= ExceptionSeverity.SESSION_SEVERITY)
125: && (se.getSeverity() >= ((StandardException) error)
126: .getSeverity())) {
127:
128: throw factory
129: .markCorrupt(StandardException.newException(
130: SQLState.XACT_ABORT_EXCEPTION, se));
131: }
132:
133: } finally {
134:
135: if (throwAway) {
136: // xact close will pop this context out of the context
137: // stack
138: xact.close();
139: xact = null;
140: }
141: }
142:
143: }
144:
145: RawTransaction getTransaction() {
146: return xact;
147: }
148:
149: RawStoreFactory getFactory() {
150: return factory;
151: }
152:
153: void substituteTransaction(Xact newTran) {
154: // disengage old tran from this xact context
155: Xact oldTran = (Xact) xact;
156: if (oldTran.xc == this )
157: oldTran.xc = null;
158:
159: // set up double link between new transaction and myself
160: xact = newTran;
161: ((Xact) xact).xc = this;
162: }
163:
164: }
|