1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.store.raw.xact.XactContext |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.store.raw.xact; |
22 | |
23 | import org.apache.derby.iapi.reference.SQLState; |
24 | |
25 | // This is the recommended super-class for all contexts. |
26 | import org.apache.derby.iapi.services.context.ContextImpl; |
27 | import org.apache.derby.iapi.services.context.ContextManager; |
28 | |
29 | import org.apache.derby.iapi.services.sanity.SanityManager; |
30 | |
31 | import org.apache.derby.iapi.store.raw.RawStoreFactory; |
32 | import org.apache.derby.iapi.store.raw.xact.RawTransaction; |
33 | |
34 | import org.apache.derby.iapi.error.StandardException; |
35 | |
36 | import org.apache.derby.iapi.error.ExceptionSeverity; |
37 | |
38 | /** |
39 | The context associated with the transaction. |
40 | |
41 | This object stores the context associated with the raw store transaction |
42 | on the stack. It stores info about the transaction opened within a |
43 | context manager (ie. typically a single user) for a single RawStoreFactory. |
44 | |
45 | **/ |
46 | |
47 | final class XactContext extends ContextImpl { |
48 | |
49 | private RawTransaction xact; |
50 | private RawStoreFactory factory; |
51 | private boolean abortAll; // true if any exception causes this transaction to be aborted. |
52 | |
53 | XactContext(ContextManager cm, String name, Xact xact, boolean abortAll, RawStoreFactory factory) { |
54 | super(cm, name); |
55 | |
56 | this.xact = xact; |
57 | this.abortAll = abortAll; |
58 | this.factory = factory; |
59 | xact.xc = this; // double link between transaction and myself |
60 | } |
61 | |
62 | |
63 | /* |
64 | ** Context methods (most are implemented by super-class) |
65 | */ |
66 | |
67 | |
68 | /** |
69 | @exception StandardException Standard Cloudscape error policy |
70 | */ |
71 | public void cleanupOnError(Throwable error) throws StandardException { |
72 | |
73 | if (SanityManager.DEBUG) |
74 | { |
75 | SanityManager.ASSERT(getContextManager() != null); |
76 | } |
77 | |
78 | boolean throwAway = false; |
79 | |
80 | if (error instanceof StandardException) { |
81 | StandardException se = (StandardException) error; |
82 | |
83 | if (abortAll) { |
84 | // any error aborts an internal/nested xact and its transaction |
85 | |
86 | if (se.getSeverity() < ExceptionSeverity.TRANSACTION_SEVERITY) |
87 | { |
88 | throw StandardException.newException( |
89 | SQLState.XACT_INTERNAL_TRANSACTION_EXCEPTION, error); |
90 | } |
91 | |
92 | throwAway = true; |
93 | |
94 | |
95 | } else { |
96 | |
97 | // If the severity is lower than a transaction error then do nothing. |
98 | if (se.getSeverity() < ExceptionSeverity.TRANSACTION_SEVERITY) |
99 | { |
100 | return; |
101 | } |
102 | |
103 | |
104 | // If the session is going to disappear then we want to close this |
105 | // transaction, not just abort it. |
106 | if (se.getSeverity() >= ExceptionSeverity.SESSION_SEVERITY) |
107 | throwAway = true; |
108 | } |
109 | } else { |
110 | // some java* error, throw away the transaction. |
111 | throwAway = true; |
112 | } |
113 | |
114 | try { |
115 | |
116 | if (xact != null) { |
117 | // abort the transaction |
118 | xact.abort(); |
119 | } |
120 | |
121 | } catch (StandardException se) { |
122 | // if we get an error during abort then shut the system down |
123 | throwAway = true; |
124 | |
125 | // if the system was being shut down anyway, do nothing |
126 | if ((se.getSeverity() <= ExceptionSeverity.SESSION_SEVERITY) && |
127 | (se.getSeverity() >= ((StandardException) error).getSeverity())) { |
128 | |
129 | throw factory.markCorrupt( |
130 | StandardException.newException( |
131 | SQLState.XACT_ABORT_EXCEPTION, se)); |
132 | } |
133 | |
134 | } finally { |
135 | |
136 | if (throwAway) { |
137 | // xact close will pop this context out of the context |
138 | // stack |
139 | xact.close(); |
140 | xact = null; |
141 | } |
142 | } |
143 | |
144 | } |
145 | |
146 | RawTransaction getTransaction() { |
147 | return xact; |
148 | } |
149 | |
150 | RawStoreFactory getFactory() { |
151 | return factory; |
152 | } |
153 | |
154 | void substituteTransaction(Xact newTran) |
155 | { |
156 | // disengage old tran from this xact context |
157 | Xact oldTran = (Xact)xact; |
158 | if (oldTran.xc == this) |
159 | oldTran.xc = null; |
160 | |
161 | // set up double link between new transaction and myself |
162 | xact = newTran; |
163 | ((Xact)xact).xc = this; |
164 | } |
165 | |
166 | } |