KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MultipleTransaction


1
2 import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
3 import org.enhydra.jdbc.standard.StandardXADataSource;
4 import org.objectweb.jotm.Jotm;
5 import org.objectweb.transaction.jta.TMService;
6
7 import javax.naming.InitialContext JavaDoc;
8 import javax.naming.Context JavaDoc;
9 import javax.transaction.UserTransaction JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import java.sql.ResultSet JavaDoc;
13 import java.sql.PreparedStatement JavaDoc;
14 import java.sql.Connection JavaDoc;
15
16 /**
17  * this test is used to show the following process:<br>
18  * <pre>
19  * c = ds.getConnection();
20  * utx.begin();
21  * ...
22  * utx.commit();
23  * utx.begin();
24  * ...
25  * utx.rollback();
26  * utx.close();
27  * </pre>
28  */

29 public class MultipleTransaction {
30     private String JavaDoc SQL_REQUEST = "select id, foo from testdata";
31     private String JavaDoc SQL_QUERY = "update testdata set foo = ? where id=1";
32     private String JavaDoc USAGE = "usage: java MultipleTransaction [number]";
33
34     private Connection JavaDoc conn;
35     private TMService jotm;
36
37     private String JavaDoc login = null;
38     private String JavaDoc password = null;
39     private String JavaDoc url = null;
40     private String JavaDoc driver = null;
41     private String JavaDoc USER_TRANSACTION_JNDI_NAME = "UserTransaction";
42
43     public MultipleTransaction(String JavaDoc [] args) throws Exception JavaDoc {
44         if (args.length != 1) {
45             System.out.println(USAGE);
46             System.exit(1);
47         }
48
49         // first, load the properties from the spy.properties file
50
Properties JavaDoc prop = new Properties JavaDoc();
51         try {
52             prop.load(ClassLoader.getSystemResourceAsStream("spy.properties"));
53         } catch (Exception JavaDoc e) {
54             System.err.println("problem to load properties.");
55             e.printStackTrace();
56             System.exit(1);
57         }
58
59         login = prop.getProperty("login");
60         password = prop.getProperty("password");
61         url = prop.getProperty("url");
62         driver = prop.getProperty("driver");
63
64         // get the new value which will be assign to the database
65
int newValue = 0;
66         try {
67             newValue = Integer.parseInt(args[0]);
68         } catch (NumberFormatException JavaDoc e) {
69             System.err.println(USAGE);
70             System.err.println("[number] has to be an integer\n");
71             System.exit(1);
72         }
73
74
75         // Get a transaction manager
76
try {
77             // creates an instance of JOTM with a local transaction factory which is not bound to a registry
78
jotm = new Jotm(true, false);
79             InitialContext JavaDoc ictx = new InitialContext JavaDoc();
80             ictx.rebind(USER_TRANSACTION_JNDI_NAME, jotm.getUserTransaction());
81         } catch (Exception JavaDoc e) {
82             e.printStackTrace();
83             System.exit(1);
84         }
85
86         // get an user transaction
87
UserTransaction JavaDoc utx = null;
88
89         try {
90             System.out.println("create initial context");
91             Context JavaDoc ictx = new InitialContext JavaDoc();
92             System.out.println("lookup UserTransaction at : " + USER_TRANSACTION_JNDI_NAME);
93             utx = (UserTransaction JavaDoc) ictx.lookup(USER_TRANSACTION_JNDI_NAME);
94         } catch (Exception JavaDoc e) {
95             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
96             System.err.println("Exception message :" + e.getMessage());
97             e.printStackTrace();
98             System.exit(1);
99         }
100
101         // create an XA pool datasource with a minimum of 4 objects
102
StandardXAPoolDataSource spds = new StandardXAPoolDataSource(4);
103         spds.setUser(login);
104         spds.setPassword(password);
105
106         // create an XA datasource which will be given to the XA pool
107
StandardXADataSource xads = new StandardXADataSource();
108         try {
109             xads.setDriverName(driver);
110             xads.setUrl(url);
111             xads.setUser(login);
112             xads.setPassword(password);
113             xads.setTransactionManager(jotm.getTransactionManager());
114         } catch (Exception JavaDoc e) {
115             System.err.println("JOTM problem.");
116             e.printStackTrace();
117             System.exit(1);
118         }
119         
120         // give the XA datasource to the pool (to create futur objects)
121
spds.setTransactionManager(jotm.getTransactionManager());
122         spds.setDataSource(xads);
123         
124
125         conn = spds.getConnection(login, password);
126         try {
127             utx.begin();
128             PreparedStatement JavaDoc pstmt0 = conn.prepareStatement(SQL_QUERY);
129             pstmt0.setInt(1, 13);
130             pstmt0.executeUpdate();
131             System.out.println("dump, with 13 before commit:");
132             printTable();
133             System.out.println("** commit **");
134             utx.commit();
135
136             System.out.println("dump, with 13 after commit:");
137             printTable();
138
139             utx.begin();
140             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
141             pstmt.setInt(1, newValue);
142             pstmt.executeUpdate();
143             System.out.println("dump, with the new value("+newValue+") before rollback:");
144             printTable();
145             System.out.println("** rollback **");
146             utx.rollback();
147
148             System.out.println("dump, with the new value("+newValue+") after rollback:");
149             printTable();
150         } catch (Exception JavaDoc e) {
151             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
152             System.err.println("Exception message :" + e.getMessage());
153             e.printStackTrace();
154             System.exit(1);
155         }
156
157         System.out.println("Print table after work:");
158         printTable();
159         conn.close();
160         stop();
161     }
162
163     public void printTable() {
164         try {
165             PreparedStatement JavaDoc stmt = conn.prepareStatement(SQL_REQUEST);
166             ResultSet JavaDoc rset = stmt.executeQuery(SQL_REQUEST);
167             int numcols = rset.getMetaData().getColumnCount();
168             for (int i = 1; i <= numcols; i++) {
169                 System.out.print("\t" + rset.getMetaData().getColumnName(i));
170             }
171             System.out.println();
172             while (rset.next()) {
173                 for (int i = 1; i <= numcols; i++) {
174                     System.out.print("\t" + rset.getString(i));
175                 }
176                 System.out.println("");
177             }
178         } catch (Exception JavaDoc e) {
179             e.printStackTrace();
180         }
181     }
182
183     public void stop() {
184         try {
185            InitialContext JavaDoc ictx = new InitialContext JavaDoc();
186            ictx.unbind(USER_TRANSACTION_JNDI_NAME);
187         } catch (Exception JavaDoc e) {
188             e.printStackTrace();
189         }
190         jotm.stop();
191         jotm = null;
192     }
193
194     static public void main(String JavaDoc [] argv) throws Exception JavaDoc{
195         MultipleTransaction spdse = new MultipleTransaction(argv);
196         System.exit(1);
197     }
198 }
199
Popular Tags