KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > recovery > test > TestOracleXA


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm.recovery.test;
23
24 import java.sql.*;
25 import javax.sql.*;
26
27 import oracle.jdbc.*;
28 import oracle.jdbc.pool.*;
29 import oracle.jdbc.xa.OracleXid;
30 import oracle.jdbc.xa.OracleXAException;
31 import oracle.jdbc.xa.client.*;
32
33 import javax.transaction.xa.*;
34
35 import org.jboss.tm.XidFactory;
36 import org.jboss.tm.XidImpl;
37
38
39 /**
40  * Comment
41  *
42  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
43  * @version $Revision: 37459 $
44  */

45 public class TestOracleXA
46 {
47
48    static String JavaDoc URL1 = "jdbc:oracle:thin:@192.168.1.102:1521:joracle";
49    static String JavaDoc URL2 = "jdbc:oracle:thin:@192.168.1.102:1521:joracle";
50
51
52    public static void main(String JavaDoc args [])
53    {
54       try
55       {
56          boolean fail = false;
57
58
59          if (factory == null)
60          {
61             factory = new XidFactory();
62             factory.setPad(true);
63             base = (XidImpl) factory.newXid();
64          }
65
66          if (args.length > 0)
67          {
68
69             if (args[0].equals("recover"))
70             {
71                recover2();
72                return;
73             }
74             else if (args[0].equals("print"))
75             {
76                DriverManager.registerDriver(new OracleDriver());
77                printResults();
78                return;
79             }
80             else if (args[0].equals("fail"))
81             {
82                fail = true;
83             }
84             else if (args[0].equals("nonxa"))
85             {
86                DriverManager.registerDriver(new OracleDriver());
87                nonXA();
88                return;
89             }
90          }
91
92
93          DriverManager.registerDriver(new OracleDriver());
94
95          // You can put a database name after the @ sign in the connection URL.
96
Connection conna =
97          DriverManager.getConnection(URL1, "HR", "H19");
98
99          Connection connb =
100          DriverManager.getConnection(URL2, "HR", "H19");
101
102          // Prepare a statement to create the table
103
Statement stmta = conna.createStatement();
104
105          // Prepare a statement to create the table
106
Statement stmtb = connb.createStatement();
107
108          try
109          {
110             // Drop the test table
111
stmta.execute("drop table my_table");
112          }
113          catch (SQLException e)
114          {
115             System.out.println("error");
116          }
117
118          try
119          {
120             // Create a test table
121
stmta.execute("create table my_table (col1 int)");
122          }
123          catch (SQLException e)
124          {
125             System.out.println("error");
126          }
127
128          try
129          {
130             // Drop the test table
131
stmtb.execute("drop table my_tab");
132          }
133          catch (SQLException e)
134          {
135             System.out.println("error");
136          }
137
138          try
139          {
140             // Create a test table
141
stmtb.execute("create table my_tab (col1 char(30))");
142          }
143          catch (SQLException e)
144          {
145             System.out.println("error");
146          }
147
148          stmta.close();
149          stmta = null;
150          stmtb.close();
151          stmtb = null;
152
153          conna.commit();
154          conna.close();
155          conna = null;
156          connb.commit();
157          connb.close();
158          connb = null;
159
160
161
162          // Create XADataSource instances and set properties.
163
OracleXADataSource oxds1 = new OracleXADataSource();
164          oxds1.setURL(URL1);
165          oxds1.setUser("HR");
166          oxds1.setPassword("H19");
167
168          OracleXADataSource oxds2 = new OracleXADataSource();
169
170          oxds2.setURL(URL2);
171          oxds2.setUser("HR");
172          oxds2.setPassword("H19");
173
174          // Get XA connections to the underlying data sources
175
XAConnection pc1 = oxds1.getXAConnection();
176          XAConnection pc2 = oxds2.getXAConnection();
177
178          // Get the physical connections
179
Connection conn1 = pc1.getConnection();
180          Connection conn2 = pc2.getConnection();
181
182          // Get the XA resources
183
XAResource oxar1 = pc1.getXAResource();
184          XAResource oxar2 = pc2.getXAResource();
185
186          // Create the Xids With the Same Global Ids
187
Xid xid1 = createXid(1);
188          Xid xid2 = createXid(2);
189
190          // Start the Resources
191
oxar1.start(xid1, XAResource.TMNOFLAGS);
192          oxar2.start(xid2, XAResource.TMNOFLAGS);
193
194          // Execute SQL operations with conn1 and conn2
195
doSomeWork1(conn1);
196          doSomeWork2(conn2);
197
198          // END both the branches -- IMPORTANT
199
oxar1.end(xid1, XAResource.TMSUCCESS);
200          oxar2.end(xid2, XAResource.TMSUCCESS);
201
202          // Prepare the RMs
203
int prp1 = oxar1.prepare(xid1);
204          int prp2 = oxar2.prepare(xid2);
205
206          System.out.println("Return value of prepare 1 is " + prp1);
207          System.out.println("Return value of prepare 2 is " + prp2);
208
209          boolean do_commit = true;
210
211          if (!((prp1 == XAResource.XA_OK) || (prp1 == XAResource.XA_RDONLY)))
212             do_commit = false;
213
214          if (!((prp2 == XAResource.XA_OK) || (prp2 == XAResource.XA_RDONLY)))
215             do_commit = false;
216
217          System.out.println("do_commit is " + do_commit);
218          System.out.println("Is oxar1 same as oxar2 ? " + oxar1.isSameRM(oxar2));
219
220
221          if (fail)
222          {
223             System.exit(1);
224             // Close connections
225
conn1.close();
226             conn1 = null;
227             conn2.close();
228             conn2 = null;
229
230             pc1.close();
231             pc1 = null;
232             pc2.close();
233             pc2 = null;
234
235             recover2();
236          }
237
238          else
239          {
240
241             if (prp1 == XAResource.XA_OK)
242                if (do_commit)
243                   oxar1.commit(xid1, false);
244                else
245                   oxar1.rollback(xid1);
246
247             if (prp2 == XAResource.XA_OK)
248                if (do_commit)
249                   oxar2.commit(xid2, false);
250                else
251                   oxar2.rollback(xid2);
252
253             // Close connections
254
conn1.close();
255             conn1 = null;
256             conn2.close();
257             conn2 = null;
258
259             pc1.close();
260             pc1 = null;
261             pc2.close();
262             pc2 = null;
263          }
264
265
266          printResults();
267
268       }
269       catch (SQLException sqe)
270       {
271          sqe.printStackTrace();
272       }
273       catch (XAException xae)
274       {
275          if (xae instanceof OracleXAException)
276          {
277             System.out.println("XA Error is " +
278             ((OracleXAException) xae).getXAError());
279             System.out.println("SQL Error is " +
280             ((OracleXAException) xae).getOracleError());
281          }
282          else
283          {
284             xae.printStackTrace();
285             System.out.println("error code: " + xae.errorCode);
286          }
287       }
288       catch (Throwable JavaDoc t)
289       {
290          System.out.println("****SHIT!!!");
291          t.printStackTrace();
292       }
293       System.out.println("DONE!!!");
294    }
295
296    private static void recover2()
297    throws SQLException, XAException
298    {
299       // Create XADataSource instances and set properties.
300
OracleXADataSource oxds1 = new OracleXADataSource();
301       oxds1.setURL(URL1);
302       oxds1.setUser("HR");
303       oxds1.setPassword("H19");
304
305       OracleXADataSource oxds2 = new OracleXADataSource();
306
307       oxds2.setURL(URL2);
308       oxds2.setUser("HR");
309       oxds2.setPassword("H19");
310
311       XAConnection pc1;
312       XAConnection pc2;
313       Connection conn1;
314       Connection conn2;
315       XAResource oxar1;
316       XAResource oxar2;
317       // TRY RECOVERING
318

319       // Get XA connections to the underlying data sources
320
pc1 = oxds1.getXAConnection();
321       pc2 = oxds2.getXAConnection();
322
323       // Get the physical connections
324
conn1 = pc1.getConnection();
325       conn2 = pc2.getConnection();
326
327       // Get the XA resources
328
oxar1 = pc1.getXAResource();
329       oxar2 = pc2.getXAResource();
330
331       System.out.println("*** TRYING TO RECOVER");
332
333       Xid[] recover1 = oxar1.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN);
334       if (recover1 != null)
335       {
336          System.out.println("RECOVERING 1");
337          for (int i = 0; i < recover1.length; i++)
338          {
339             Xid xid = factory.fromXid(recover1[i]);
340             System.out.println("recovering XID: " + xid);
341             oxar1.commit(recover1[i], false);
342          }
343       }
344       else
345       {
346          System.out.println("RECOVER1 returned null");
347       }
348
349       Xid[] recover2 = oxar2.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN);
350       if (recover2 != null)
351       {
352          System.out.println("RECOVERING 2");
353          for (int i = 0; i < recover2.length; i++)
354          {
355             Xid xid = factory.fromXid(recover2[i]);
356             System.out.println("recovering XID: " + xid);
357             oxar2.commit(recover2[i], false);
358          }
359       }
360       else
361       {
362          System.out.println("RECOVER1 returned null");
363       }
364
365       System.out.println("HERE!!!!!");
366
367       // Close connections
368
conn1.close();
369       conn1 = null;
370       conn2.close();
371       conn2 = null;
372
373       pc1.close();
374       pc1 = null;
375       pc2.close();
376       pc2 = null;
377    }
378
379    private static void printResults()
380    throws SQLException
381    {
382       Connection conna;
383       Connection connb;
384       Statement stmta;
385       Statement stmtb;
386       conna =
387       DriverManager.getConnection(URL1, "HR", "H19");
388
389       connb =
390       DriverManager.getConnection(URL2, "HR", "H19");
391
392       // Prepare a statement to create the table
393
stmta = conna.createStatement();
394
395       // Prepare a statement to create the table
396
stmtb = connb.createStatement();
397
398       ResultSet rset = stmta.executeQuery("select col1 from my_table");
399       while (rset.next())
400          System.out.println("Col1 is " + rset.getInt(1));
401
402       rset.close();
403       rset = null;
404
405       rset = stmtb.executeQuery("select col1 from my_tab");
406       while (rset.next())
407          System.out.println("Col1 is " + rset.getString(1));
408
409       rset.close();
410       rset = null;
411
412       stmta.close();
413       stmta = null;
414       stmtb.close();
415       stmtb = null;
416
417       conna.commit();
418       conna.close();
419       conna = null;
420       connb.commit();
421       connb.close();
422       connb = null;
423    }
424
425    private static void nonXA()
426    throws SQLException
427    {
428       Connection conna;
429       Connection connb;
430       Statement stmta;
431       Statement stmtb;
432       conna =
433       DriverManager.getConnection(URL1, "HR", "H19");
434       conna.setAutoCommit(false);
435
436       connb =
437       DriverManager.getConnection(URL2, "HR", "H19");
438
439       connb.setAutoCommit(false);
440
441       doSomeWork1(conna);
442       doSomeWork2(connb);
443
444       System.exit(1);
445
446       conna.commit();
447       conna.close();
448       conna = null;
449       connb.commit();
450       connb.close();
451       connb = null;
452    }
453
454    static XidFactory factory;
455    static XidImpl base;
456
457    static Xid createXid(int bids)
458    throws XAException
459    {
460       return factory.newBranch(base, bids);
461    }
462
463    private static void doSomeWork1(Connection conn)
464    throws SQLException
465    {
466       Statement st = conn.createStatement();
467       st.executeUpdate("insert into my_table values(1)");
468       st.close();
469    }
470
471    private static void doSomeWork2(Connection conn)
472    throws SQLException
473    {
474       Statement st = conn.createStatement();
475       st.executeUpdate("insert into my_tab values('world')");
476       st.close();
477    }
478
479
480 }
481
Popular Tags