KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > XATestUtil


1 /*
2
3    Derby - Class org.apache.derby.impl.services.bytecode.CodeChunk
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.derbyTesting.functionTests.util;
23
24 import java.io.Serializable JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Statement JavaDoc;
29
30 import javax.transaction.xa.XAException JavaDoc;
31 import javax.transaction.xa.Xid JavaDoc;
32
33 import org.apache.derby.tools.JDBCDisplayUtil;
34
35 public class XATestUtil {
36     
37     /**
38      * Return a new Xid for testing.
39     */

40     public static Xid JavaDoc getXid(int xid, int b1, int b2) {
41         return new utilXid(xid, b1, b2);
42     }
43     
44     /**
45      * Dump an unexpected XAException.
46      * @param tag Useful info to print
47      * @param xae The exception
48      */

49     public static void dumpXAException(String JavaDoc tag, XAException JavaDoc xae) {
50
51         System.out.println(tag + " : XAException - " + xae.getMessage()
52                 + " errorCode " + errorCode(xae));
53         xae.printStackTrace(System.out);
54     }
55
56     /**
57      * Create a view that allows useful inspection of the active
58      * global transactions.
59     */

60     public static void createXATransactionView(Connection JavaDoc conn) throws SQLException JavaDoc
61     {
62         Statement JavaDoc s = conn.createStatement();
63         s.execute(
64                 "create view XATESTUTIL.global_xactTable as " +
65                 "select cast(global_xid as char(2)) as gxid," +
66                 " status, " +
67                 " CAST (case when first_instant is NULL then 'NULL' else 'false' end AS VARCHAR(8)) as readOnly, " +
68                 " cast (username as char(10)) as username, type " +
69                 " from syscs_diag.transaction_table");
70         s.close();
71     }
72     
73     /**
74      * Display the active global transactions.
75      * @param conn
76      * @throws SQLException
77      */

78     public static void showXATransactionView(Connection JavaDoc conn) throws SQLException JavaDoc
79     {
80         Statement JavaDoc s = conn.createStatement();
81         ResultSet JavaDoc rs = s.executeQuery(
82                 "select * from XATESTUTIL.global_xactTable where gxid is not null order by gxid");
83         JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
84         rs.close();
85     }
86     
87     /**
88      * Return a string for the error code of the XAException.
89     */

90     public static String JavaDoc errorCode(XAException JavaDoc e)
91     {
92         String JavaDoc error;
93         switch(e.errorCode)
94         {
95         case XAException.XA_HEURCOM : error = "XA_HEURCOM "; break;
96         case XAException.XA_HEURHAZ : error = "XA_HEURHAZ"; break;
97         case XAException.XA_HEURMIX : error = "XA_HEURMIX"; break;
98         case XAException.XA_HEURRB : error = "XA_HEURRB "; break;
99         case XAException.XA_NOMIGRATE : error = "XA_NOMIGRATE "; break;
100         case XAException.XA_RBCOMMFAIL : error = "XA_RBCOMMFAIL "; break;
101         case XAException.XA_RBDEADLOCK : error = "XA_RBDEADLOCK "; break;
102         case XAException.XA_RBINTEGRITY : error = "XA_RBINTEGRITY "; break;
103         case XAException.XA_RBOTHER : error = "XA_RBOTHER "; break;
104         case XAException.XA_RBPROTO : error = "XA_RBPROTO "; break;
105         case XAException.XA_RBROLLBACK : error = "XA_RBROLLBACK "; break;
106         case XAException.XA_RBTIMEOUT : error = "XA_RBTIMEOUT "; break;
107         case XAException.XA_RBTRANSIENT : error = "XA_RBTRANSIENT "; break;
108         case XAException.XA_RDONLY : error = "XA_RDONLY "; break;
109         case XAException.XA_RETRY : error = "XA_RETRY "; break;
110         case XAException.XAER_ASYNC : error = "XAER_ASYNC "; break;
111         case XAException.XAER_DUPID : error = "XAER_DUPID "; break;
112         case XAException.XAER_INVAL : error = "XAER_INVAL "; break;
113         case XAException.XAER_NOTA : error = "XAER_NOTA "; break;
114         case XAException.XAER_OUTSIDE : error = "XAER_OUTSIDE "; break;
115         case XAException.XAER_PROTO : error = "XAER_PROTO "; break;
116         case XAException.XAER_RMERR : error = "XAER_RMERR "; break;
117         case XAException.XAER_RMFAIL : error = "XAER_RMFAIL "; break;
118         default: error = Integer.toString(e.errorCode); break;
119         }
120         return error;
121     }
122     
123 }
124 /**
125  * Simple utility class implementation of Xid for tests.
126  *
127  */

128 class utilXid implements Xid JavaDoc, Serializable JavaDoc {
129     private static final long serialVersionUID = 64467338100036L;
130
131     private final int format_id;
132
133     private byte[] global_id;
134
135     private byte[] branch_id;
136
137     utilXid(int xid, int b1, int b2) {
138         format_id = xid;
139         global_id = new byte[Xid.MAXGTRIDSIZE];
140         branch_id = new byte[Xid.MAXBQUALSIZE];
141
142         for (int i = 0; i < global_id.length; i++) {
143             global_id[i] = (byte) (b1 + i);
144         }
145
146         for (int i = 0; i < branch_id.length; i++) {
147             branch_id[i] = (byte) (b2 + i);
148         }
149     }
150
151     /**
152      * Obtain the format id part of the Xid.
153      * <p>
154      *
155      * @return Format identifier. O means the OSI CCR format.
156      **/

157     public int getFormatId() {
158         return (format_id);
159     }
160
161     /**
162      * Obtain the global transaction identifier part of XID as an array of
163      * bytes.
164      * <p>
165      *
166      * @return A byte array containing the global transaction identifier.
167      **/

168     public byte[] getGlobalTransactionId() {
169         return (global_id);
170     }
171
172     /**
173      * Obtain the transaction branch qualifier part of the Xid in a byte array.
174      * <p>
175      *
176      * @return A byte array containing the branch qualifier of the transaction.
177      **/

178     public byte[] getBranchQualifier() {
179         return (branch_id);
180     }
181 }
Popular Tags