KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > net > NetXAConnectionRequest


1 /*
2
3    Derby - Class org.apache.derby.client.net.NetXAConnectionRequest
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.client.net;
23
24 import javax.transaction.xa.XAResource JavaDoc;
25 import javax.transaction.xa.Xid JavaDoc;
26
27 import org.apache.derby.client.am.SqlException;
28
29 public class NetXAConnectionRequest extends NetResultSetRequest {
30     NetXAConnectionRequest(NetAgent netAgent, CcsidManager ccsidManager, int bufferSize) {
31         super(netAgent, ccsidManager, bufferSize);
32     }
33
34     //----------------------------- entry points ---------------------------------
35

36
37     //Build the SYNNCTL commit command
38
public void writeLocalXACommit(NetConnection conn) throws SqlException {
39         NetXACallInfo callInfo =
40                 conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
41         Xid JavaDoc xid = callInfo.xid_;
42         buildSYNCCTLMigrate(); // xa migrate to resync server
43
buildSYNCCTLCommit(CodePoint.TMLOCAL, xid); // xa local commit
44
}
45
46     //Build the SYNNCTL rollback command
47
public void writeLocalXARollback(NetConnection conn) throws SqlException {
48         NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
49         buildSYNCCTLRollback(CodePoint.TMLOCAL); // xa local rollback
50
}
51
52     public void writeXaStartUnitOfWork(NetConnection conn) throws SqlException {
53         NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
54         Xid JavaDoc xid = callInfo.xid_;
55         int xaFlags = callInfo.xaFlags_;
56
57         // create DSS command with reply.
58
createCommand();
59
60         // save the length bytes for later update
61
markLengthBytes(CodePoint.SYNCCTL);
62
63         // SYNCTYPE
64
writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_NEW_UOW);
65
66         if (xid.getFormatId() != -1) {
67             writeXID(CodePoint.XID, xid);
68         } else
69         // write the null XID for local transaction on XA connection
70
{
71             writeNullXID(CodePoint.XID);
72         }
73
74         writeXAFlags(CodePoint.XAFLAGS, xaFlags);
75         updateLengthBytes();
76     }
77
78     public void writeXaEndUnitOfWork(NetConnection conn) throws SqlException {
79         NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
80         Xid JavaDoc xid = callInfo.xid_;
81         int xaFlags = callInfo.xaFlags_;
82
83         createCommand();
84
85         // save the length bytes for later update
86
markLengthBytes(CodePoint.SYNCCTL);
87
88         // SYNCTYPE
89
writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_END_UOW);
90
91         if (xid.getFormatId() != -1) {
92             writeXID(CodePoint.XID, xid);
93         } else
94         // write the null XID for local transaction on XA connection
95
{
96             writeNullXID(CodePoint.XID);
97         }
98
99         writeXAFlags(CodePoint.XAFLAGS, xaFlags);
100
101         updateLengthBytes();
102     }
103
104     protected void writeXaPrepare(NetConnection conn) throws SqlException {
105         NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
106         Xid JavaDoc xid = callInfo.xid_;
107         // don't forget that xars.prepare() does not have flags, assume TMNOFLAGS
108
int xaFlags = XAResource.TMNOFLAGS;
109
110         createCommand();
111
112         // save the length bytes for later update
113
markLengthBytes(CodePoint.SYNCCTL);
114
115         // SYNCTYPE
116
writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_PREPARE);
117
118         if (xid.getFormatId() != -1) {
119             writeXID(CodePoint.XID, xid);
120         } else
121         // write the null XID for local transaction on XA connection
122
{
123             writeNullXID(CodePoint.XID);
124         }
125
126         writeXAFlags(CodePoint.XAFLAGS, xaFlags);
127         updateLengthBytes();
128     }
129
130     protected void writeXaCommit(NetConnection conn, Xid JavaDoc xid) throws SqlException {
131         NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
132         int xaFlags = callInfo.xaFlags_;
133
134         // create DSS command with no reply.
135
createCommand();
136
137         // save the length bytes for later update
138
markLengthBytes(CodePoint.SYNCCTL);
139
140         // SYNCTYPE
141
writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_COMMITTED);
142
143         if (xid.getFormatId() != -1) {
144             writeXID(CodePoint.XID, xid);
145         } else
146         // write the null XID for local transaction on XA connection
147
{
148             writeNullXID(CodePoint.XID);
149         }
150
151         writeXAFlags(CodePoint.XAFLAGS, xaFlags);
152         updateLengthBytes();
153     }
154
155     protected void writeXaRollback(NetConnection conn, Xid JavaDoc xid) throws SqlException {
156         int xaFlags = XAResource.TMNOFLAGS;
157
158         // create DSS command with no reply.
159
createCommand();
160
161         // save the length bytes for later update
162
markLengthBytes(CodePoint.SYNCCTL);
163
164         // SYNCTYPE
165
writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_ROLLBACK);
166
167         if (xid.getFormatId() != -1) {
168             writeXID(CodePoint.XID, xid);
169         } else
170         // write the null XID for local transaction on XA connection
171
{
172             writeNullXID(CodePoint.XID);
173         }
174
175         writeXAFlags(CodePoint.XAFLAGS, xaFlags);
176         updateLengthBytes();
177     }
178
179     protected void writeXaRecover(NetConnection conn, int flag) throws SqlException {
180         // create DSS command with no reply.
181
createCommand();
182
183         // save the length bytes for later update
184
markLengthBytes(CodePoint.SYNCCTL);
185
186         // SYNCTYPE
187
writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_INDOUBT);
188         writeXAFlags(CodePoint.XAFLAGS, flag);
189         updateLengthBytes();
190     }
191
192     protected void writeXaForget(NetConnection conn, Xid JavaDoc xid) throws SqlException {
193
194         // create DSS command with no reply.
195
createCommand();
196
197         // save the length bytes for later update
198
markLengthBytes(CodePoint.SYNCCTL);
199
200         // SYNCTYPE
201
writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_REQ_FORGET);
202
203         writeXID(CodePoint.XID, xid);
204
205         updateLengthBytes();
206     }
207
208     public void writeSYNCType(int codepoint, int syncType) {
209         writeScalar1Byte(codepoint, syncType);
210     }
211
212     public void writeForget(int codepoint, int value) {
213         writeScalar1Byte(codepoint, value);
214     }
215
216     public void writeReleaseConversation(int codepoint, int value) {
217         writeScalar1Byte(codepoint, value);
218     }
219
220     void writeNullXID(int codepoint) {
221         int nullXID = -1;
222         writeScalar4Bytes(codepoint, nullXID);
223     }
224
225     void writeXID(int codepoint, Xid JavaDoc xid) throws SqlException {
226         int len = 0;
227         int formatId = xid.getFormatId();
228         byte[] gtrid = xid.getGlobalTransactionId();
229         byte[] bqual = xid.getBranchQualifier();
230
231         markLengthBytes(codepoint);
232
233         len = 4; // length of formatId
234
len += (bqual.length + 4); // bqual length
235
len += (gtrid.length + 4); // gtrid length
236

237         write4Bytes(formatId);
238         write4Bytes(gtrid.length);
239         write4Bytes(bqual.length);
240
241         // Mare sure request buffer has enough space to write this byte array.
242
ensureLength(offset_ + gtrid.length);
243         System.arraycopy(gtrid, 0, bytes_, offset_, gtrid.length);
244         offset_ += gtrid.length;
245
246         ensureLength(offset_ + bqual.length);
247         System.arraycopy(bqual, 0, bytes_, offset_, bqual.length);
248         offset_ += bqual.length;
249
250         updateLengthBytes();
251
252     }
253
254
255     void writeXAFlags(int codepoint, int xaFlags) {
256         writeScalar4Bytes(codepoint, xaFlags);
257     }
258
259
260     //----------------------helper methods----------------------------------------
261
// These methods are "private protected", which is not a recognized java privilege,
262
// but means that these methods are private to this class and to subclasses,
263
// and should not be used as package-wide friendly methods.
264

265
266
267
268
269     void buildSYNCCTLMigrate() throws SqlException {
270     }
271
272     void buildSYNCCTLCommit(int xaFlags, Xid JavaDoc xid) throws SqlException {
273         createCommand();
274
275         // save the length bytes for later update
276
markLengthBytes(CodePoint.SYNCCTL);
277
278         // SYNCTYPE
279
writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_COMMITTED);
280
281         if (xid.getFormatId() != -1) {
282             writeXID(CodePoint.XID, xid);
283         } else
284         // write the null XID for local transaction on XA connection
285
{
286             writeNullXID(CodePoint.XID);
287         }
288
289         writeXAFlags(CodePoint.XAFLAGS, xaFlags);
290
291         updateLengthBytes();
292     }
293
294     void buildSYNCCTLRollback(int xaFlags) throws SqlException {
295         createCommand();
296
297         // save the length bytes for later update
298
markLengthBytes(CodePoint.SYNCCTL);
299
300         // SYNCTYPE
301
writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_ROLLBACK);
302
303         // write the null XID for local transaction on XA connection
304
writeNullXID(CodePoint.XID);
305         writeXAFlags(CodePoint.XAFLAGS, xaFlags);
306         updateLengthBytes();
307     }
308
309 }
310
311
312
313
Popular Tags