KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JdbcDistExample


1 /*
2  * @(#) JdbcDistExample.java
3  *
4  *
5  * JOTM: Java Open Transaction Manager
6  *
7  *
8  * This module was orginally developed by
9  *
10  * - INRIA (www.inria.fr)inside the ObjectWeb Consortium
11  * (http://www.objectweb.org)
12  *
13  * --------------------------------------------------------------------------
14  * The original code and portions created by INRIA are
15  * Copyright (c) 2002 INRIA
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are met:
20  *
21  * -Redistributions of source code must retain the above copyright notice, this
22  * list of conditions and the following disclaimer.
23  *
24  * -Redistributions in binary form must reproduce the above copyright notice,
25  * this list of conditions and the following disclaimer in the documentation
26  * and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * --------------------------------------------------------------------------
41  * $Id: JdbcDistExample.java,v 1.1 2004/01/30 23:13:40 tonyortiz Exp $
42  * --------------------------------------------------------------------------
43  */

44
45 import java.sql.Connection JavaDoc;
46 import java.sql.PreparedStatement JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48 import java.sql.Statement JavaDoc;
49
50 import javax.naming.Context JavaDoc;
51 import javax.naming.InitialContext JavaDoc;
52 import javax.transaction.UserTransaction JavaDoc;
53
54 /**
55  * @author jmesnil
56  * Updated tortiz
57  *
58  * This class is an example of use of JOTM within a Distributed DataBase
59  */

60 public class JdbcDistExample {
61
62     private static final String JavaDoc USAGE = "usage: java JdbcDistExample [database1 database2] [commit|rollback] [number]";
63     private static final String JavaDoc SQL_REQUEST1 = "select id, foo from testdata";
64     private static final String JavaDoc SQL_QUERY1 = "update testdata set foo = ? where id=1";
65     private static final String JavaDoc SQL_REQUEST2 = "select id, foo from testdata";
66     private static final String JavaDoc SQL_QUERY2 = "update testdata set foo = ? where id=1";
67     private static final String JavaDoc USER_TRANSACTION_JNDI_NAME = "UserTransaction";
68     private static Connection JavaDoc conn1 = null;
69     private static Connection JavaDoc conn2 = null;
70
71     private static void printTable(Connection JavaDoc conn, String JavaDoc sqlRequest) {
72         try {
73             Statement JavaDoc stmt = conn.createStatement();
74             ResultSet JavaDoc rset = stmt.executeQuery(sqlRequest);
75             int numcols = rset.getMetaData().getColumnCount();
76             for (int i = 1; i <= numcols; i++) {
77                 System.out.print("\t" + rset.getMetaData().getColumnName(i));
78             }
79             System.out.println();
80             while (rset.next()) {
81                 for (int i = 1; i <= numcols; i++) {
82                     System.out.print("\t" + rset.getString(i));
83                 }
84                 System.out.println("");
85             }
86         } catch (Exception JavaDoc e) {
87             e.printStackTrace();
88         }
89     }
90
91     private static void updateTable(Connection JavaDoc conn, String JavaDoc sqlQuery, int newValue) {
92         try {
93             PreparedStatement JavaDoc pstmt = conn.prepareStatement(sqlQuery);
94             pstmt.setInt(1, newValue);
95             pstmt.executeUpdate();
96         } catch (Exception JavaDoc e) {
97             e.printStackTrace();
98         }
99     }
100
101     public static void main(String JavaDoc[] args) {
102
103         if (args.length != 4 || (!args[2].equals("commit") && !args[2].equals("rollback"))) {
104             System.out.println(USAGE + "\n");
105             System.exit(1);
106         }
107
108         String JavaDoc completion = args[2];
109
110         int newValue = 0;
111         try {
112             newValue = Integer.parseInt(args[3]);
113         } catch (NumberFormatException JavaDoc e) {
114             System.out.println(USAGE);
115             System.out.println("[number] has to be an integer\n");
116             System.exit(1);
117         }
118
119         System.out.println("start server");
120         DistDatabaseHelper ddbHelper = new DistDatabaseHelper(args[0], args[1]);
121
122         UserTransaction JavaDoc utx = null;
123         try {
124             System.out.println("create initial context");
125             Context JavaDoc ictx = new InitialContext JavaDoc();
126             System.out.println("lookup UserTransaction at : " + USER_TRANSACTION_JNDI_NAME);
127             utx = (UserTransaction JavaDoc) ictx.lookup(USER_TRANSACTION_JNDI_NAME);
128         } catch (Exception JavaDoc e) {
129             System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown");
130             System.out.println("Exception message :" + e.getMessage());
131             e.printStackTrace();
132             System.exit(1);
133         }
134
135         try {
136             System.out.println("get a connection");
137             conn1 = ddbHelper.getConnection1();
138             conn2 = ddbHelper.getConnection2();
139         } catch (Exception JavaDoc e) {
140             e.printStackTrace();
141         }
142
143         System.out.println("before transaction, table is:");
144         printTable(conn1, SQL_REQUEST1);
145         printTable(conn2, SQL_REQUEST2);
146
147         try {
148             System.out.println("begin a transaction");
149             utx.begin();
150
151             System.out.println("update the table");
152             updateTable(conn1, SQL_QUERY1, newValue);
153             updateTable(conn2, SQL_QUERY2, newValue);
154
155             if (completion.equals("commit")) {
156                 System.out.println("*commit* the transaction");
157                 utx.commit();
158             } else {
159                 System.out.println("*rollback* the transaction");
160                 utx.rollback();
161             }
162         } catch (Exception JavaDoc e) {
163             System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown");
164             System.out.println("Exception message :" + e.getMessage());
165             e.printStackTrace();
166             System.exit(1);
167         }
168
169         utx = null;
170
171         System.out.println("after transaction, table is:");
172         printTable(conn1, SQL_REQUEST1);
173         printTable(conn2, SQL_REQUEST2);
174
175         try {
176             System.out.println("close connection");
177             conn1.close();
178             conn2.close();
179
180         } catch (Exception JavaDoc e) {
181             e.printStackTrace();
182         } finally {
183             conn1 = null;
184             conn2 = null;
185         }
186
187         System.out.println("stop server");
188         ddbHelper.stop();
189
190         System.out.println("JDBC example is ok.\n");
191         System.exit(0);
192     }
193 }
194
Popular Tags