1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.store.raw.xact.BeginXact |
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.util.ByteArray; |
35 | |
36 | import java.io.OutputStream; |
37 | import java.io.InputStream; |
38 | import java.io.ObjectOutput; |
39 | import java.io.ObjectInput; |
40 | import java.io.IOException; |
41 | import org.apache.derby.iapi.services.io.LimitObjectInput; |
42 | |
43 | /** |
44 | This operation indicates the beginning of a transaction. |
45 | @see Loggable |
46 | */ |
47 | |
48 | public class BeginXact implements Loggable { |
49 | |
50 | protected int transactionStatus; |
51 | protected GlobalTransactionId xactId; |
52 | |
53 | |
54 | public BeginXact(GlobalTransactionId xid, int s) |
55 | { |
56 | xactId = xid; |
57 | transactionStatus = s; |
58 | } |
59 | |
60 | /* |
61 | * Formatable methods |
62 | */ |
63 | public BeginXact() |
64 | { super() ; } |
65 | |
66 | public void writeExternal(ObjectOutput out) throws IOException |
67 | { |
68 | out.writeInt(transactionStatus); |
69 | out.writeObject(xactId); |
70 | } |
71 | |
72 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException |
73 | { |
74 | transactionStatus = in.readInt(); |
75 | xactId = (GlobalTransactionId)in.readObject(); |
76 | } |
77 | |
78 | /** |
79 | Return my format identifier. |
80 | */ |
81 | public int getTypeFormatId() { |
82 | return StoredFormatIds.LOGOP_BEGIN_XACT; |
83 | } |
84 | |
85 | /** |
86 | Loggable methods |
87 | @see Loggable |
88 | */ |
89 | |
90 | /** |
91 | Apply the change indicated by this operation and optional data. |
92 | |
93 | @param xact the Transaction |
94 | @param instant the log instant of this operation |
95 | @param in optional data |
96 | |
97 | */ |
98 | public void doMe(Transaction xact, LogInstant instant, LimitObjectInput in) |
99 | { |
100 | RawTransaction rt = (RawTransaction)xact; |
101 | |
102 | // If we are not doing fake logging for in memory database |
103 | if (instant != null) |
104 | { |
105 | rt.setFirstLogInstant(instant); |
106 | |
107 | // need to do this here rather than in the transaction object for |
108 | // recovery. |
109 | rt.addUpdateTransaction(transactionStatus); |
110 | } |
111 | } |
112 | |
113 | /** |
114 | the default for prepared log is always null for all the operations |
115 | that don't have optionalData. If an operation has optional data, |
116 | the operation need to prepare the optional data for this method. |
117 | |
118 | BeginXact has no optional data to write out |
119 | |
120 | @see ObjectOutput |
121 | */ |
122 | public ByteArray getPreparedLog() |
123 | { |
124 | return (ByteArray) null; |
125 | } |
126 | |
127 | /** |
128 | Always redo a BeginXact. |
129 | |
130 | @param xact The transaction trying to redo this operation |
131 | @return true if operation needs redoing, false if not. |
132 | */ |
133 | public boolean needsRedo(Transaction xact) |
134 | { |
135 | return true; // always redo this |
136 | } |
137 | |
138 | |
139 | /** |
140 | BeginXact has no resource to release |
141 | */ |
142 | public void releaseResource(Transaction xact) |
143 | {} |
144 | |
145 | |
146 | /** |
147 | BeginXact is both a FIRST and a RAWSTORE log record |
148 | */ |
149 | public int group() |
150 | { |
151 | int group = Loggable.FIRST | Loggable.RAWSTORE; |
152 | return group; |
153 | } |
154 | |
155 | /** |
156 | DEBUG: Print self. |
157 | */ |
158 | public String toString() |
159 | { |
160 | if (SanityManager.DEBUG) |
161 | return "BeginXact " + xactId + " transactionStatus " + Integer.toHexString(transactionStatus); |
162 | else |
163 | return null; |
164 | |
165 | } |
166 | |
167 | /** |
168 | BeginXact method |
169 | */ |
170 | public GlobalTransactionId getGlobalId() |
171 | { |
172 | return xactId; |
173 | } |
174 | |
175 | |
176 | } |
177 | |