KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MultipleConnection


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 import java.sql.ResultSet JavaDoc;
12 import java.sql.PreparedStatement JavaDoc;
13 import java.sql.Connection JavaDoc;
14
15 /**
16  * this test is used to show the following process:<br>
17  * <pre>
18  * utx.begin();
19  * c = ds.getConnection();
20  * ...
21  * c.close();
22  * c = ds.getConnection();
23  * ...
24  * c.close();
25  * utx.commit() or utx.rollback();
26  * </pre>
27  * This process should work in all cases.
28  */

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