KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > xact > XactContext


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.xact.XactContext
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.derby.impl.store.raw.xact;
23
24 import org.apache.derby.iapi.reference.SQLState;
25
26 // This is the recommended super-class for all contexts.
27
import org.apache.derby.iapi.services.context.ContextImpl;
28 import org.apache.derby.iapi.services.context.ContextManager;
29
30 import org.apache.derby.iapi.services.sanity.SanityManager;
31
32 import org.apache.derby.iapi.store.raw.RawStoreFactory;
33 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
34
35 import org.apache.derby.iapi.error.StandardException;
36
37 import org.apache.derby.iapi.error.ExceptionSeverity;
38
39 /**
40 The context associated with the transaction.
41
42 This object stores the context associated with the raw store transaction
43 on the stack. It stores info about the transaction opened within a
44 context manager (ie. typically a single user) for a single RawStoreFactory.
45
46 **/

47
48 final class XactContext extends ContextImpl {
49
50     private RawTransaction xact;
51     private RawStoreFactory factory;
52     private boolean abortAll; // true if any exception causes this transaction to be aborted.
53

54     XactContext(ContextManager cm, String JavaDoc name, Xact xact, boolean abortAll, RawStoreFactory factory) {
55         super(cm, name);
56
57         this.xact = xact;
58         this.abortAll = abortAll;
59         this.factory = factory;
60         xact.xc = this; // double link between transaction and myself
61
}
62
63
64     /*
65     ** Context methods (most are implemented by super-class)
66     */

67
68
69     /**
70         @exception StandardException Standard Cloudscape error policy
71     */

72     public void cleanupOnError(Throwable JavaDoc error) throws StandardException {
73
74         if (SanityManager.DEBUG)
75         {
76             SanityManager.ASSERT(getContextManager() != null);
77         }
78
79         boolean throwAway = false;
80
81         if (error instanceof StandardException) {
82             StandardException se = (StandardException) error;
83
84             if (abortAll) {
85                 // any error aborts an internal/nested xact and its transaction
86

87                 if (se.getSeverity() < ExceptionSeverity.TRANSACTION_SEVERITY)
88                 {
89                     throw StandardException.newException(
90                         SQLState.XACT_INTERNAL_TRANSACTION_EXCEPTION, error);
91                 }
92
93                 throwAway = true;
94
95
96             } else {
97
98                 // If the severity is lower than a transaction error then do nothing.
99
if (se.getSeverity() < ExceptionSeverity.TRANSACTION_SEVERITY)
100                 {
101                     return;
102                 }
103                  
104
105                 // If the session is going to disappear then we want to close this
106
// transaction, not just abort it.
107
if (se.getSeverity() >= ExceptionSeverity.SESSION_SEVERITY)
108                     throwAway = true;
109             }
110         } else {
111             // some java* error, throw away the transaction.
112
throwAway = true;
113         }
114
115         try {
116
117             if (xact != null) {
118                 // abort the transaction
119
xact.abort();
120             }
121
122         } catch (StandardException se) {
123             // if we get an error during abort then shut the system down
124
throwAway = true;
125
126             // if the system was being shut down anyway, do nothing
127
if ((se.getSeverity() <= ExceptionSeverity.SESSION_SEVERITY) &&
128                 (se.getSeverity() >= ((StandardException) error).getSeverity())) {
129
130                 throw factory.markCorrupt(
131                     StandardException.newException(
132                         SQLState.XACT_ABORT_EXCEPTION, se));
133             }
134
135         } finally {
136
137             if (throwAway) {
138                 // xact close will pop this context out of the context
139
// stack
140
xact.close();
141                 xact = null;
142             }
143         }
144
145     }
146
147     RawTransaction getTransaction() {
148         return xact;
149     }
150
151     RawStoreFactory getFactory() {
152         return factory;
153     }
154
155     void substituteTransaction(Xact newTran)
156     {
157         // disengage old tran from this xact context
158
Xact oldTran = (Xact)xact;
159         if (oldTran.xc == this)
160             oldTran.xc = null;
161
162         // set up double link between new transaction and myself
163
xact = newTran;
164         ((Xact)xact).xc = this;
165     }
166
167 }
168
Popular Tags