1 | /* |
2 | |
3 | Derby - Class org.apache.derby.client.net.NetXAConnectionRequest |
4 | |
5 | Copyright (c) 2001, 2005 The Apache Software Foundation or its licensors, where 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.client.net; |
22 | |
23 | import javax.transaction.xa.XAResource; |
24 | import javax.transaction.xa.Xid; |
25 | |
26 | import org.apache.derby.client.am.SqlException; |
27 | |
28 | public class NetXAConnectionRequest extends NetResultSetRequest { |
29 | NetXAConnectionRequest(NetAgent netAgent, CcsidManager ccsidManager, int bufferSize) { |
30 | super(netAgent, ccsidManager, bufferSize); |
31 | } |
32 | |
33 | //----------------------------- entry points --------------------------------- |
34 | |
35 | |
36 | //Build the SYNNCTL commit command |
37 | public void writeLocalXACommit(NetConnection conn) throws SqlException { |
38 | NetXACallInfo callInfo = |
39 | conn.xares_.callInfoArray_[conn.currXACallInfoOffset_]; |
40 | Xid xid = callInfo.xid_; |
41 | buildSYNCCTLMigrate(); // xa migrate to resync server |
42 | buildSYNCCTLCommit(CodePoint.TMLOCAL, xid); // xa local commit |
43 | } |
44 | |
45 | //Build the SYNNCTL rollback command |
46 | public void writeLocalXARollback(NetConnection conn) throws SqlException { |
47 | NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_]; |
48 | buildSYNCCTLRollback(CodePoint.TMLOCAL); // xa local rollback |
49 | } |
50 | |
51 | public void writeXaStartUnitOfWork(NetConnection conn) throws SqlException { |
52 | NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_]; |
53 | Xid xid = callInfo.xid_; |
54 | int xaFlags = callInfo.xaFlags_; |
55 | |
56 | // create DSS command with reply. |
57 | createCommand(); |
58 | |
59 | // save the length bytes for later update |
60 | markLengthBytes(CodePoint.SYNCCTL); |
61 | |
62 | // SYNCTYPE |
63 | writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_NEW_UOW); |
64 | |
65 | if (xid.getFormatId() != -1) { |
66 | writeXID(CodePoint.XID, xid); |
67 | } else |
68 | // write the null XID for local transaction on XA connection |
69 | { |
70 | writeNullXID(CodePoint.XID); |
71 | } |
72 | |
73 | writeXAFlags(CodePoint.XAFLAGS, xaFlags); |
74 | updateLengthBytes(); |
75 | } |
76 | |
77 | public void writeXaEndUnitOfWork(NetConnection conn) throws SqlException { |
78 | NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_]; |
79 | Xid xid = callInfo.xid_; |
80 | int xaFlags = callInfo.xaFlags_; |
81 | |
82 | createCommand(); |
83 | |
84 | // save the length bytes for later update |
85 | markLengthBytes(CodePoint.SYNCCTL); |
86 | |
87 | // SYNCTYPE |
88 | writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_END_UOW); |
89 | |
90 | if (xid.getFormatId() != -1) { |
91 | writeXID(CodePoint.XID, xid); |
92 | } else |
93 | // write the null XID for local transaction on XA connection |
94 | { |
95 | writeNullXID(CodePoint.XID); |
96 | } |
97 | |
98 | writeXAFlags(CodePoint.XAFLAGS, xaFlags); |
99 | |
100 | updateLengthBytes(); |
101 | } |
102 | |
103 | protected void writeXaPrepare(NetConnection conn) throws SqlException { |
104 | NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_]; |
105 | Xid xid = callInfo.xid_; |
106 | // don't forget that xars.prepare() does not have flags, assume TMNOFLAGS |
107 | int xaFlags = XAResource.TMNOFLAGS; |
108 | |
109 | createCommand(); |
110 | |
111 | // save the length bytes for later update |
112 | markLengthBytes(CodePoint.SYNCCTL); |
113 | |
114 | // SYNCTYPE |
115 | writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_PREPARE); |
116 | |
117 | if (xid.getFormatId() != -1) { |
118 | writeXID(CodePoint.XID, xid); |
119 | } else |
120 | // write the null XID for local transaction on XA connection |
121 | { |
122 | writeNullXID(CodePoint.XID); |
123 | } |
124 | |
125 | writeXAFlags(CodePoint.XAFLAGS, xaFlags); |
126 | updateLengthBytes(); |
127 | } |
128 | |
129 | protected void writeXaCommit(NetConnection conn, Xid xid) throws SqlException { |
130 | NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_]; |
131 | int xaFlags = callInfo.xaFlags_; |
132 | |
133 | // create DSS command with no reply. |
134 | createCommand(); |
135 | |
136 | // save the length bytes for later update |
137 | markLengthBytes(CodePoint.SYNCCTL); |
138 | |
139 | // SYNCTYPE |
140 | writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_COMMITTED); |
141 | |
142 | if (xid.getFormatId() != -1) { |
143 | writeXID(CodePoint.XID, xid); |
144 | } else |
145 | // write the null XID for local transaction on XA connection |
146 | { |
147 | writeNullXID(CodePoint.XID); |
148 | } |
149 | |
150 | writeXAFlags(CodePoint.XAFLAGS, xaFlags); |
151 | updateLengthBytes(); |
152 | } |
153 | |
154 | protected void writeXaRollback(NetConnection conn, Xid xid) throws SqlException { |
155 | int xaFlags = XAResource.TMNOFLAGS; |
156 | |
157 | // create DSS command with no reply. |
158 | createCommand(); |
159 | |
160 | // save the length bytes for later update |
161 | markLengthBytes(CodePoint.SYNCCTL); |
162 | |
163 | // SYNCTYPE |
164 | writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_ROLLBACK); |
165 | |
166 | if (xid.getFormatId() != -1) { |
167 | writeXID(CodePoint.XID, xid); |
168 | } else |
169 | // write the null XID for local transaction on XA connection |
170 | { |
171 | writeNullXID(CodePoint.XID); |
172 | } |
173 | |
174 | writeXAFlags(CodePoint.XAFLAGS, xaFlags); |
175 | updateLengthBytes(); |
176 | } |
177 | |
178 | protected void writeXaRecover(NetConnection conn, int flag) throws SqlException { |
179 | // create DSS command with no reply. |
180 | createCommand(); |
181 | |
182 | // save the length bytes for later update |
183 | markLengthBytes(CodePoint.SYNCCTL); |
184 | |
185 | // SYNCTYPE |
186 | writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_INDOUBT); |
187 | writeXAFlags(CodePoint.XAFLAGS, flag); |
188 | updateLengthBytes(); |
189 | } |
190 | |
191 | protected void writeXaForget(NetConnection conn, Xid xid) throws SqlException { |
192 | |
193 | // create DSS command with no reply. |
194 | createCommand(); |
195 | |
196 | // save the length bytes for later update |
197 | markLengthBytes(CodePoint.SYNCCTL); |
198 | |
199 | // SYNCTYPE |
200 | writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_REQ_FORGET); |
201 | |
202 | writeXID(CodePoint.XID, xid); |
203 | |
204 | updateLengthBytes(); |
205 | } |
206 | |
207 | public void writeSYNCType(int codepoint, int syncType) { |
208 | writeScalar1Byte(codepoint, syncType); |
209 | } |
210 | |
211 | public void writeForget(int codepoint, int value) { |
212 | writeScalar1Byte(codepoint, value); |
213 | } |
214 | |
215 | public void writeReleaseConversation(int codepoint, int value) { |
216 | writeScalar1Byte(codepoint, value); |
217 | } |
218 | |
219 | void writeNullXID(int codepoint) { |
220 | int nullXID = -1; |
221 | writeScalar4Bytes(codepoint, nullXID); |
222 | } |
223 | |
224 | void writeXID(int codepoint, Xid xid) throws SqlException { |
225 | int len = 0; |
226 | int formatId = xid.getFormatId(); |
227 | byte[] gtrid = xid.getGlobalTransactionId(); |
228 | byte[] bqual = xid.getBranchQualifier(); |
229 | |
230 | markLengthBytes(codepoint); |
231 | |
232 | len = 4; // length of formatId |
233 | len += (bqual.length + 4); // bqual length |
234 | len += (gtrid.length + 4); // gtrid length |
235 | |
236 | write4Bytes(formatId); |
237 | write4Bytes(gtrid.length); |
238 | write4Bytes(bqual.length); |
239 | |
240 | // Mare sure request buffer has enough space to write this byte array. |
241 | ensureLength(offset_ + gtrid.length); |
242 | System.arraycopy(gtrid, 0, bytes_, offset_, gtrid.length); |
243 | offset_ += gtrid.length; |
244 | |
245 | ensureLength(offset_ + bqual.length); |
246 | System.arraycopy(bqual, 0, bytes_, offset_, bqual.length); |
247 | offset_ += bqual.length; |
248 | |
249 | updateLengthBytes(); |
250 | |
251 | } |
252 | |
253 | |
254 | void writeXAFlags(int codepoint, int xaFlags) { |
255 | writeScalar4Bytes(codepoint, xaFlags); |
256 | } |
257 | |
258 | |
259 | //----------------------helper methods---------------------------------------- |
260 | // These methods are "private protected", which is not a recognized java privilege, |
261 | // but means that these methods are private to this class and to subclasses, |
262 | // and should not be used as package-wide friendly methods. |
263 | |
264 | |
265 | |
266 | |
267 | |
268 | void buildSYNCCTLMigrate() throws SqlException { |
269 | } |
270 | |
271 | void buildSYNCCTLCommit(int xaFlags, Xid xid) throws SqlException { |
272 | createCommand(); |
273 | |
274 | // save the length bytes for later update |
275 | markLengthBytes(CodePoint.SYNCCTL); |
276 | |
277 | // SYNCTYPE |
278 | writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_COMMITTED); |
279 | |
280 | if (xid.getFormatId() != -1) { |
281 | writeXID(CodePoint.XID, xid); |
282 | } else |
283 | // write the null XID for local transaction on XA connection |
284 | { |
285 | writeNullXID(CodePoint.XID); |
286 | } |
287 | |
288 | writeXAFlags(CodePoint.XAFLAGS, xaFlags); |
289 | |
290 | updateLengthBytes(); |
291 | } |
292 | |
293 | void buildSYNCCTLRollback(int xaFlags) throws SqlException { |
294 | createCommand(); |
295 | |
296 | // save the length bytes for later update |
297 | markLengthBytes(CodePoint.SYNCCTL); |
298 | |
299 | // SYNCTYPE |
300 | writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_ROLLBACK); |
301 | |
302 | // write the null XID for local transaction on XA connection |
303 | writeNullXID(CodePoint.XID); |
304 | writeXAFlags(CodePoint.XAFLAGS, xaFlags); |
305 | updateLengthBytes(); |
306 | } |
307 | |
308 | } |
309 | |
310 | |
311 | |