1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.store.raw.xact.EndXact |
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.services.io.FormatIdUtil; |
24 | import org.apache.derby.iapi.services.io.StoredFormatIds; |
25 | import org.apache.derby.iapi.services.sanity.SanityManager; |
26 | |
27 | import org.apache.derby.iapi.store.raw.Transaction; |
28 | import org.apache.derby.iapi.store.raw.Loggable; |
29 | import org.apache.derby.iapi.store.raw.GlobalTransactionId; |
30 | |
31 | import org.apache.derby.iapi.store.raw.log.LogInstant; |
32 | import org.apache.derby.iapi.store.raw.xact.RawTransaction; |
33 | |
34 | import org.apache.derby.iapi.services.io.CompressedNumber; |
35 | import org.apache.derby.iapi.util.ByteArray; |
36 | |
37 | import java.io.OutputStream; |
38 | import java.io.InputStream; |
39 | import java.io.ObjectOutput; |
40 | import java.io.ObjectInput; |
41 | import java.io.IOException; |
42 | import org.apache.derby.iapi.services.io.LimitObjectInput; |
43 | |
44 | /** |
45 | This operation indicates the End of a transaction. |
46 | @see Loggable |
47 | */ |
48 | |
49 | public class EndXact implements Loggable { |
50 | |
51 | private int transactionStatus; |
52 | private GlobalTransactionId xactId; |
53 | |
54 | public EndXact(GlobalTransactionId xid, int s) { |
55 | super(); |
56 | |
57 | xactId = xid; |
58 | transactionStatus = s; |
59 | } |
60 | |
61 | /* |
62 | * Formatable methods |
63 | */ |
64 | |
65 | // no-arg constructor, required by Formatable |
66 | public EndXact() |
67 | { super(); } |
68 | |
69 | public void writeExternal(ObjectOutput out) throws IOException |
70 | { |
71 | out.writeObject(xactId); |
72 | CompressedNumber.writeInt(out, transactionStatus); |
73 | } |
74 | |
75 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException |
76 | { |
77 | xactId = (GlobalTransactionId)in.readObject(); |
78 | transactionStatus = CompressedNumber.readInt(in); |
79 | } |
80 | |
81 | /** |
82 | Return my format identifier. |
83 | */ |
84 | public int getTypeFormatId() { |
85 | return StoredFormatIds.LOGOP_END_XACT; |
86 | } |
87 | |
88 | /** |
89 | Loggable methods |
90 | @see Loggable |
91 | */ |
92 | |
93 | /** |
94 | Apply the change indicated by this operation and optional data. |
95 | |
96 | @param xact the Transaction |
97 | @param instant the log instant of this operation |
98 | @param in optional data |
99 | |
100 | */ |
101 | public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in) |
102 | { |
103 | |
104 | if ((transactionStatus & Xact.END_PREPARED) == 0) |
105 | { |
106 | ((RawTransaction)xact).removeUpdateTransaction(); |
107 | } |
108 | else |
109 | { |
110 | ((RawTransaction)xact).prepareTransaction(); |
111 | } |
112 | } |
113 | |
114 | /** |
115 | the default for prepared log is always null for all the operations |
116 | that don't have optionalData. If an operation has optional data, |
117 | the operation need to prepare the optional data for this method. |
118 | |
119 | EndXact has no optional data to write out |
120 | |
121 | @see ObjectOutput |
122 | */ |
123 | public ByteArray getPreparedLog() |
124 | { |
125 | return (ByteArray) null; |
126 | } |
127 | |
128 | /** |
129 | Always redo an EndXact. |
130 | |
131 | @param xact The transaction trying to redo this operation |
132 | @return true if operation needs redoing, false if not. |
133 | */ |
134 | public boolean needsRedo(Transaction xact) |
135 | { |
136 | return true; // always redo this |
137 | } |
138 | |
139 | |
140 | /** |
141 | EndXact has no resource to release |
142 | */ |
143 | public void releaseResource(Transaction xact) |
144 | {} |
145 | |
146 | |
147 | /** |
148 | EndXact is a RAWSTORE log record. |
149 | */ |
150 | public int group() |
151 | { |
152 | int group = Loggable.RAWSTORE; |
153 | |
154 | if ((transactionStatus & Xact.END_COMMITTED) != 0) |
155 | group |= (Loggable.COMMIT | Loggable.LAST); |
156 | else if ((transactionStatus & Xact.END_ABORTED) != 0) |
157 | group |= (Loggable.ABORT | Loggable.LAST); |
158 | else if ((transactionStatus & Xact.END_PREPARED) != 0) |
159 | group |= Loggable.PREPARE; |
160 | |
161 | return group; |
162 | } |
163 | |
164 | |
165 | /** |
166 | DEBUG: Print self. |
167 | */ |
168 | public String toString() |
169 | { |
170 | if (SanityManager.DEBUG) |
171 | { |
172 | String endStatus; |
173 | switch(transactionStatus & |
174 | (Xact.END_ABORTED | Xact.END_PREPARED | Xact.END_COMMITTED)) |
175 | { |
176 | case Xact.END_ABORTED: |
177 | endStatus = " Aborted"; |
178 | break; |
179 | case Xact.END_PREPARED: |
180 | endStatus = " Prepared"; |
181 | break; |
182 | case Xact.END_COMMITTED: |
183 | endStatus = " Committed"; |
184 | break; |
185 | default: |
186 | endStatus = "Unknown"; |
187 | } |
188 | |
189 | return( |
190 | "EndXact " + xactId + endStatus + |
191 | " : transactionStatus = " + endStatus); |
192 | } |
193 | else |
194 | { |
195 | return null; |
196 | } |
197 | } |
198 | } |