001: /*
002:
003: Derby - Class org.apache.derby.client.net.NetXAConnectionRequest
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.client.net;
023:
024: import javax.transaction.xa.XAResource;
025: import javax.transaction.xa.Xid;
026:
027: import org.apache.derby.client.am.SqlException;
028:
029: public class NetXAConnectionRequest extends NetResultSetRequest {
030: NetXAConnectionRequest(NetAgent netAgent,
031: CcsidManager ccsidManager, int bufferSize) {
032: super (netAgent, ccsidManager, bufferSize);
033: }
034:
035: //----------------------------- entry points ---------------------------------
036:
037: //Build the SYNNCTL commit command
038: public void writeLocalXACommit(NetConnection conn)
039: throws SqlException {
040: NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
041: Xid xid = callInfo.xid_;
042: buildSYNCCTLMigrate(); // xa migrate to resync server
043: buildSYNCCTLCommit(CodePoint.TMLOCAL, xid); // xa local commit
044: }
045:
046: //Build the SYNNCTL rollback command
047: public void writeLocalXARollback(NetConnection conn)
048: throws SqlException {
049: NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
050: buildSYNCCTLRollback(CodePoint.TMLOCAL); // xa local rollback
051: }
052:
053: public void writeXaStartUnitOfWork(NetConnection conn)
054: throws SqlException {
055: NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
056: Xid xid = callInfo.xid_;
057: int xaFlags = callInfo.xaFlags_;
058:
059: // create DSS command with reply.
060: createCommand();
061:
062: // save the length bytes for later update
063: markLengthBytes(CodePoint.SYNCCTL);
064:
065: // SYNCTYPE
066: writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_NEW_UOW);
067:
068: if (xid.getFormatId() != -1) {
069: writeXID(CodePoint.XID, xid);
070: } else
071: // write the null XID for local transaction on XA connection
072: {
073: writeNullXID(CodePoint.XID);
074: }
075:
076: writeXAFlags(CodePoint.XAFLAGS, xaFlags);
077: updateLengthBytes();
078: }
079:
080: public void writeXaEndUnitOfWork(NetConnection conn)
081: throws SqlException {
082: NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
083: Xid xid = callInfo.xid_;
084: int xaFlags = callInfo.xaFlags_;
085:
086: createCommand();
087:
088: // save the length bytes for later update
089: markLengthBytes(CodePoint.SYNCCTL);
090:
091: // SYNCTYPE
092: writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_END_UOW);
093:
094: if (xid.getFormatId() != -1) {
095: writeXID(CodePoint.XID, xid);
096: } else
097: // write the null XID for local transaction on XA connection
098: {
099: writeNullXID(CodePoint.XID);
100: }
101:
102: writeXAFlags(CodePoint.XAFLAGS, xaFlags);
103:
104: updateLengthBytes();
105: }
106:
107: protected void writeXaPrepare(NetConnection conn)
108: throws SqlException {
109: NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
110: Xid xid = callInfo.xid_;
111: // don't forget that xars.prepare() does not have flags, assume TMNOFLAGS
112: int xaFlags = XAResource.TMNOFLAGS;
113:
114: createCommand();
115:
116: // save the length bytes for later update
117: markLengthBytes(CodePoint.SYNCCTL);
118:
119: // SYNCTYPE
120: writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_PREPARE);
121:
122: if (xid.getFormatId() != -1) {
123: writeXID(CodePoint.XID, xid);
124: } else
125: // write the null XID for local transaction on XA connection
126: {
127: writeNullXID(CodePoint.XID);
128: }
129:
130: writeXAFlags(CodePoint.XAFLAGS, xaFlags);
131: updateLengthBytes();
132: }
133:
134: protected void writeXaCommit(NetConnection conn, Xid xid)
135: throws SqlException {
136: NetXACallInfo callInfo = conn.xares_.callInfoArray_[conn.currXACallInfoOffset_];
137: int xaFlags = callInfo.xaFlags_;
138:
139: // create DSS command with no reply.
140: createCommand();
141:
142: // save the length bytes for later update
143: markLengthBytes(CodePoint.SYNCCTL);
144:
145: // SYNCTYPE
146: writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_COMMITTED);
147:
148: if (xid.getFormatId() != -1) {
149: writeXID(CodePoint.XID, xid);
150: } else
151: // write the null XID for local transaction on XA connection
152: {
153: writeNullXID(CodePoint.XID);
154: }
155:
156: writeXAFlags(CodePoint.XAFLAGS, xaFlags);
157: updateLengthBytes();
158: }
159:
160: protected void writeXaRollback(NetConnection conn, Xid xid)
161: throws SqlException {
162: int xaFlags = XAResource.TMNOFLAGS;
163:
164: // create DSS command with no reply.
165: createCommand();
166:
167: // save the length bytes for later update
168: markLengthBytes(CodePoint.SYNCCTL);
169:
170: // SYNCTYPE
171: writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_ROLLBACK);
172:
173: if (xid.getFormatId() != -1) {
174: writeXID(CodePoint.XID, xid);
175: } else
176: // write the null XID for local transaction on XA connection
177: {
178: writeNullXID(CodePoint.XID);
179: }
180:
181: writeXAFlags(CodePoint.XAFLAGS, xaFlags);
182: updateLengthBytes();
183: }
184:
185: protected void writeXaRecover(NetConnection conn, int flag)
186: throws SqlException {
187: // create DSS command with no reply.
188: createCommand();
189:
190: // save the length bytes for later update
191: markLengthBytes(CodePoint.SYNCCTL);
192:
193: // SYNCTYPE
194: writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_INDOUBT);
195: writeXAFlags(CodePoint.XAFLAGS, flag);
196: updateLengthBytes();
197: }
198:
199: protected void writeXaForget(NetConnection conn, Xid xid)
200: throws SqlException {
201:
202: // create DSS command with no reply.
203: createCommand();
204:
205: // save the length bytes for later update
206: markLengthBytes(CodePoint.SYNCCTL);
207:
208: // SYNCTYPE
209: writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_REQ_FORGET);
210:
211: writeXID(CodePoint.XID, xid);
212:
213: updateLengthBytes();
214: }
215:
216: public void writeSYNCType(int codepoint, int syncType) {
217: writeScalar1Byte(codepoint, syncType);
218: }
219:
220: public void writeForget(int codepoint, int value) {
221: writeScalar1Byte(codepoint, value);
222: }
223:
224: public void writeReleaseConversation(int codepoint, int value) {
225: writeScalar1Byte(codepoint, value);
226: }
227:
228: void writeNullXID(int codepoint) {
229: int nullXID = -1;
230: writeScalar4Bytes(codepoint, nullXID);
231: }
232:
233: void writeXID(int codepoint, Xid xid) throws SqlException {
234: int len = 0;
235: int formatId = xid.getFormatId();
236: byte[] gtrid = xid.getGlobalTransactionId();
237: byte[] bqual = xid.getBranchQualifier();
238:
239: markLengthBytes(codepoint);
240:
241: len = 4; // length of formatId
242: len += (bqual.length + 4); // bqual length
243: len += (gtrid.length + 4); // gtrid length
244:
245: write4Bytes(formatId);
246: write4Bytes(gtrid.length);
247: write4Bytes(bqual.length);
248:
249: // Mare sure request buffer has enough space to write this byte array.
250: ensureLength(offset_ + gtrid.length);
251: System.arraycopy(gtrid, 0, bytes_, offset_, gtrid.length);
252: offset_ += gtrid.length;
253:
254: ensureLength(offset_ + bqual.length);
255: System.arraycopy(bqual, 0, bytes_, offset_, bqual.length);
256: offset_ += bqual.length;
257:
258: updateLengthBytes();
259:
260: }
261:
262: void writeXAFlags(int codepoint, int xaFlags) {
263: writeScalar4Bytes(codepoint, xaFlags);
264: }
265:
266: //----------------------helper methods----------------------------------------
267: // These methods are "private protected", which is not a recognized java privilege,
268: // but means that these methods are private to this class and to subclasses,
269: // and should not be used as package-wide friendly methods.
270:
271: void buildSYNCCTLMigrate() throws SqlException {
272: }
273:
274: void buildSYNCCTLCommit(int xaFlags, Xid xid) throws SqlException {
275: createCommand();
276:
277: // save the length bytes for later update
278: markLengthBytes(CodePoint.SYNCCTL);
279:
280: // SYNCTYPE
281: writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_COMMITTED);
282:
283: if (xid.getFormatId() != -1) {
284: writeXID(CodePoint.XID, xid);
285: } else
286: // write the null XID for local transaction on XA connection
287: {
288: writeNullXID(CodePoint.XID);
289: }
290:
291: writeXAFlags(CodePoint.XAFLAGS, xaFlags);
292:
293: updateLengthBytes();
294: }
295:
296: void buildSYNCCTLRollback(int xaFlags) throws SqlException {
297: createCommand();
298:
299: // save the length bytes for later update
300: markLengthBytes(CodePoint.SYNCCTL);
301:
302: // SYNCTYPE
303: writeSYNCType(CodePoint.SYNCTYPE, CodePoint.SYNCTYPE_ROLLBACK);
304:
305: // write the null XID for local transaction on XA connection
306: writeNullXID(CodePoint.XID);
307: writeXAFlags(CodePoint.XAFLAGS, xaFlags);
308: updateLengthBytes();
309: }
310:
311: }
|