KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.xact.EndXact
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.services.io.FormatIdUtil;
25 import org.apache.derby.iapi.services.io.StoredFormatIds;
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27
28 import org.apache.derby.iapi.store.raw.Transaction;
29 import org.apache.derby.iapi.store.raw.Loggable;
30 import org.apache.derby.iapi.store.raw.GlobalTransactionId;
31
32 import org.apache.derby.iapi.store.raw.log.LogInstant;
33 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
34
35 import org.apache.derby.iapi.services.io.CompressedNumber;
36 import org.apache.derby.iapi.util.ByteArray;
37
38 import java.io.OutputStream JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.ObjectOutput JavaDoc;
41 import java.io.ObjectInput JavaDoc;
42 import java.io.IOException JavaDoc;
43 import org.apache.derby.iapi.services.io.LimitObjectInput;
44
45 /**
46     This operation indicates the End of a transaction.
47     @see Loggable
48 */

49
50 public class EndXact implements Loggable {
51
52     private int transactionStatus;
53     private GlobalTransactionId xactId;
54
55     public EndXact(GlobalTransactionId xid, int s) {
56         super();
57
58         xactId = xid;
59         transactionStatus = s;
60     }
61
62     /*
63      * Formatable methods
64      */

65
66     // no-arg constructor, required by Formatable
67
public EndXact()
68     { super(); }
69
70     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
71     {
72         out.writeObject(xactId);
73         CompressedNumber.writeInt(out, transactionStatus);
74     }
75
76     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
77     {
78         xactId = (GlobalTransactionId)in.readObject();
79         transactionStatus = CompressedNumber.readInt(in);
80     }
81
82     /**
83         Return my format identifier.
84     */

85     public int getTypeFormatId() {
86         return StoredFormatIds.LOGOP_END_XACT;
87     }
88
89     /**
90         Loggable methods
91         @see Loggable
92     */

93
94     /**
95         Apply the change indicated by this operation and optional data.
96
97         @param xact the Transaction
98         @param instant the log instant of this operation
99         @param in optional data
100
101     */

102     public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in)
103     {
104
105         if ((transactionStatus & Xact.END_PREPARED) == 0)
106         {
107             ((RawTransaction)xact).removeUpdateTransaction();
108         }
109         else
110         {
111             ((RawTransaction)xact).prepareTransaction();
112         }
113     }
114
115     /**
116         the default for prepared log is always null for all the operations
117         that don't have optionalData. If an operation has optional data,
118         the operation need to prepare the optional data for this method.
119
120         EndXact has no optional data to write out
121
122         @see ObjectOutput
123     */

124     public ByteArray getPreparedLog()
125     {
126         return (ByteArray) null;
127     }
128
129     /**
130         Always redo an EndXact.
131
132         @param xact The transaction trying to redo this operation
133         @return true if operation needs redoing, false if not.
134     */

135     public boolean needsRedo(Transaction xact)
136     {
137         return true; // always redo this
138
}
139
140
141     /**
142         EndXact has no resource to release
143     */

144     public void releaseResource(Transaction xact)
145     {}
146
147
148     /**
149         EndXact is a RAWSTORE log record.
150     */

151     public int group()
152     {
153         int group = Loggable.RAWSTORE;
154
155         if ((transactionStatus & Xact.END_COMMITTED) != 0)
156             group |= (Loggable.COMMIT | Loggable.LAST);
157         else if ((transactionStatus & Xact.END_ABORTED) != 0)
158             group |= (Loggable.ABORT | Loggable.LAST);
159         else if ((transactionStatus & Xact.END_PREPARED) != 0)
160             group |= Loggable.PREPARE;
161
162         return group;
163     }
164           
165
166     /**
167       DEBUG: Print self.
168     */

169     public String JavaDoc toString()
170     {
171         if (SanityManager.DEBUG)
172         {
173             String JavaDoc endStatus;
174             switch(transactionStatus &
175                    (Xact.END_ABORTED | Xact.END_PREPARED | Xact.END_COMMITTED))
176             {
177                 case Xact.END_ABORTED:
178                     endStatus = " Aborted";
179                     break;
180                 case Xact.END_PREPARED:
181                     endStatus = " Prepared";
182                     break;
183                 case Xact.END_COMMITTED:
184                     endStatus = " Committed";
185                     break;
186                 default:
187                     endStatus = "Unknown";
188             }
189                 
190             return(
191                 "EndXact " + xactId + endStatus +
192                 " : transactionStatus = " + endStatus);
193         }
194         else
195         {
196             return null;
197         }
198     }
199 }
200
Popular Tags