001: /*
002:
003: Derby - Class org.apache.derby.impl.services.bytecode.CodeChunk
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.derbyTesting.functionTests.util;
023:
024: import java.io.Serializable;
025: import java.sql.Connection;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.Statement;
029:
030: import javax.transaction.xa.XAException;
031: import javax.transaction.xa.Xid;
032:
033: import org.apache.derby.tools.JDBCDisplayUtil;
034:
035: public class XATestUtil {
036:
037: /**
038: * Return a new Xid for testing.
039: */
040: public static Xid getXid(int xid, int b1, int b2) {
041: return new utilXid(xid, b1, b2);
042: }
043:
044: /**
045: * Dump an unexpected XAException.
046: * @param tag Useful info to print
047: * @param xae The exception
048: */
049: public static void dumpXAException(String tag, XAException xae) {
050:
051: System.out.println(tag + " : XAException - " + xae.getMessage()
052: + " errorCode " + errorCode(xae));
053: xae.printStackTrace(System.out);
054: }
055:
056: /**
057: * Create a view that allows useful inspection of the active
058: * global transactions.
059: */
060: public static void createXATransactionView(Connection conn)
061: throws SQLException {
062: Statement s = conn.createStatement();
063: s
064: .execute("create view XATESTUTIL.global_xactTable as "
065: + "select cast(global_xid as char(2)) as gxid,"
066: + " status, "
067: + " CAST (case when first_instant is NULL then 'NULL' else 'false' end AS VARCHAR(8)) as readOnly, "
068: + " cast (username as char(10)) as username, type "
069: + " from syscs_diag.transaction_table");
070: s.close();
071: }
072:
073: /**
074: * Display the active global transactions.
075: * @param conn
076: * @throws SQLException
077: */
078: public static void showXATransactionView(Connection conn)
079: throws SQLException {
080: Statement s = conn.createStatement();
081: ResultSet rs = s
082: .executeQuery("select * from XATESTUTIL.global_xactTable where gxid is not null order by gxid");
083: JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
084: rs.close();
085: }
086:
087: /**
088: * Return a string for the error code of the XAException.
089: */
090: public static String errorCode(XAException e) {
091: String error;
092: switch (e.errorCode) {
093: case XAException.XA_HEURCOM:
094: error = "XA_HEURCOM ";
095: break;
096: case XAException.XA_HEURHAZ:
097: error = "XA_HEURHAZ";
098: break;
099: case XAException.XA_HEURMIX:
100: error = "XA_HEURMIX";
101: break;
102: case XAException.XA_HEURRB:
103: error = "XA_HEURRB ";
104: break;
105: case XAException.XA_NOMIGRATE:
106: error = "XA_NOMIGRATE ";
107: break;
108: case XAException.XA_RBCOMMFAIL:
109: error = "XA_RBCOMMFAIL ";
110: break;
111: case XAException.XA_RBDEADLOCK:
112: error = "XA_RBDEADLOCK ";
113: break;
114: case XAException.XA_RBINTEGRITY:
115: error = "XA_RBINTEGRITY ";
116: break;
117: case XAException.XA_RBOTHER:
118: error = "XA_RBOTHER ";
119: break;
120: case XAException.XA_RBPROTO:
121: error = "XA_RBPROTO ";
122: break;
123: case XAException.XA_RBROLLBACK:
124: error = "XA_RBROLLBACK ";
125: break;
126: case XAException.XA_RBTIMEOUT:
127: error = "XA_RBTIMEOUT ";
128: break;
129: case XAException.XA_RBTRANSIENT:
130: error = "XA_RBTRANSIENT ";
131: break;
132: case XAException.XA_RDONLY:
133: error = "XA_RDONLY ";
134: break;
135: case XAException.XA_RETRY:
136: error = "XA_RETRY ";
137: break;
138: case XAException.XAER_ASYNC:
139: error = "XAER_ASYNC ";
140: break;
141: case XAException.XAER_DUPID:
142: error = "XAER_DUPID ";
143: break;
144: case XAException.XAER_INVAL:
145: error = "XAER_INVAL ";
146: break;
147: case XAException.XAER_NOTA:
148: error = "XAER_NOTA ";
149: break;
150: case XAException.XAER_OUTSIDE:
151: error = "XAER_OUTSIDE ";
152: break;
153: case XAException.XAER_PROTO:
154: error = "XAER_PROTO ";
155: break;
156: case XAException.XAER_RMERR:
157: error = "XAER_RMERR ";
158: break;
159: case XAException.XAER_RMFAIL:
160: error = "XAER_RMFAIL ";
161: break;
162: default:
163: error = Integer.toString(e.errorCode);
164: break;
165: }
166: return error;
167: }
168:
169: }
170:
171: /**
172: * Simple utility class implementation of Xid for tests.
173: *
174: */
175: class utilXid implements Xid, Serializable {
176: private static final long serialVersionUID = 64467338100036L;
177:
178: private final int format_id;
179:
180: private byte[] global_id;
181:
182: private byte[] branch_id;
183:
184: utilXid(int xid, int b1, int b2) {
185: format_id = xid;
186: global_id = new byte[Xid.MAXGTRIDSIZE];
187: branch_id = new byte[Xid.MAXBQUALSIZE];
188:
189: for (int i = 0; i < global_id.length; i++) {
190: global_id[i] = (byte) (b1 + i);
191: }
192:
193: for (int i = 0; i < branch_id.length; i++) {
194: branch_id[i] = (byte) (b2 + i);
195: }
196: }
197:
198: /**
199: * Obtain the format id part of the Xid.
200: * <p>
201: *
202: * @return Format identifier. O means the OSI CCR format.
203: **/
204: public int getFormatId() {
205: return (format_id);
206: }
207:
208: /**
209: * Obtain the global transaction identifier part of XID as an array of
210: * bytes.
211: * <p>
212: *
213: * @return A byte array containing the global transaction identifier.
214: **/
215: public byte[] getGlobalTransactionId() {
216: return (global_id);
217: }
218:
219: /**
220: * Obtain the transaction branch qualifier part of the Xid in a byte array.
221: * <p>
222: *
223: * @return A byte array containing the branch qualifier of the transaction.
224: **/
225: public byte[] getBranchQualifier() {
226: return (branch_id);
227: }
228: }
|