KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > manager > XATransactionTester


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.transaction.manager;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.Statement JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.sql.XAConnection JavaDoc;
26 import javax.sql.XADataSource JavaDoc;
27 import javax.transaction.TransactionManager JavaDoc;
28 import javax.transaction.xa.XAResource JavaDoc;
29 import javax.transaction.xa.Xid JavaDoc;
30
31 /**
32  *
33  *
34  *
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class XATransactionTester {
38     private TransactionManager JavaDoc manager;
39     private XADataSource JavaDoc ds;
40     private Xid JavaDoc xid;
41
42     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
43         new XATransactionTester().run(args);
44     }
45
46     public void run(String JavaDoc[] args) throws Exception JavaDoc {
47         ds = getDataSource(args);
48         XAConnection JavaDoc xaConn = ds.getXAConnection("test", "test");
49         XAResource JavaDoc xaRes = xaConn.getXAResource();
50         manager = new TransactionManagerImpl(10, new DummyLog());
51         Connection JavaDoc c = xaConn.getConnection();
52         Statement JavaDoc s = c.createStatement();
53
54         manager.begin();
55         manager.getTransaction().enlistResource(xaRes);
56         s.execute("UPDATE XA_TEST SET X=X+1");
57         manager.getTransaction().delistResource(xaRes, XAResource.TMSUCCESS);
58         manager.commit();
59
60 /*
61         manager.begin();
62         manager.getTransaction().enlistResource(xaRes);
63         xid = new XidImpl(xid, 1);
64         System.out.println("xid = " + xid);
65         s.execute("UPDATE XA_TEST SET X=X+1");
66
67         xaRes.end(xid, XAResource.TMSUCCESS);
68         xaRes.prepare(xid);
69         c.close();
70 */

71
72 /*
73         Xid[] prepared = xaRes.recover(XAResource.TMNOFLAGS);
74         for (int i = 0; i < prepared.length; i++) {
75             Xid xid = prepared[i];
76             StringBuffer s = new StringBuffer();
77             s.append(Integer.toHexString(xid.getFormatId())).append('.');
78             byte[] globalId = xid.getGlobalTransactionId();
79             for (int j = 0; j < globalId.length; j++) {
80                 s.append(Integer.toHexString(globalId[j]));
81             }
82
83             System.out.println("recovery = " + s);
84             xaRes.forget(xid);
85         }
86 */

87
88     }
89
90     /*
91      * @todo get something that loads this from a file
92      */

93     private XADataSource JavaDoc getDataSource(String JavaDoc[] args) throws Exception JavaDoc {
94 // oracle.jdbc.xa.client.OracleXADataSource ds = new oracle.jdbc.xa.client.OracleXADataSource();
95
// ds.setConnectionURL("jdbc:oracle:thin:@localhost:1521:ABU");
96
// return ds;
97
return null;
98     }
99
100     private class DummyLog implements TransactionLog {
101
102         public void begin(Xid JavaDoc xid) throws LogException {
103             XATransactionTester.this.xid = xid;
104         }
105
106         public Object JavaDoc prepare(Xid JavaDoc xid, List JavaDoc branches) throws LogException {
107             return new Object JavaDoc();
108         }
109
110         public void commit(Xid JavaDoc xid, Object JavaDoc logMark) throws LogException {
111         }
112
113         public void rollback(Xid JavaDoc xid, Object JavaDoc logMark) throws LogException {
114         }
115
116         public Collection JavaDoc recover(XidFactory xidFactory) throws LogException {
117             return new ArrayList JavaDoc();
118         }
119
120         public String JavaDoc getXMLStats() {
121             return null;
122         }
123
124         public int getAverageForceTime() {
125             return 0;
126         }
127
128         public int getAverageBytesPerForce() {
129             return 0;
130         }
131     }
132 }
133
Popular Tags