KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DistributedDataSourceExample


1 /*
2  * @(#) DistributedDataSourceExample.java
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  * This project is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: DistributedDataSourceExample.java,v 1.1 2003/04/02 13:18:55 jmesnil Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.Statement JavaDoc;
33
34 import javax.naming.InitialContext JavaDoc;
35 import javax.sql.DataSource JavaDoc;
36 import javax.transaction.UserTransaction JavaDoc;
37
38 /**
39  * @author jmesnil
40  */

41 public class DistributedDataSourceExample {
42
43     private static final String JavaDoc SQL_REQUEST = "select id, foo from testdata";
44
45     private static final String JavaDoc SQL_QUERY =
46         "update testdata set foo = ? where id=1";
47
48     private static Connection JavaDoc conn;
49
50     private static void updateTable(int newValue) {
51         try {
52             Statement JavaDoc stmt = conn.createStatement();
53             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
54             pstmt.setInt(1, newValue);
55             pstmt.executeUpdate();
56         } catch (Exception JavaDoc e) {
57             e.printStackTrace();
58         }
59     }
60
61     private static void printTable() {
62         try {
63             Statement JavaDoc stmt = conn.createStatement();
64             ResultSet JavaDoc rset = stmt.executeQuery(SQL_REQUEST);
65             int numcols = rset.getMetaData().getColumnCount();
66             for (int i = 1; i <= numcols; i++) {
67                 System.out.print("\t" + rset.getMetaData().getColumnName(i));
68             }
69             System.out.println();
70             while (rset.next()) {
71                 for (int i = 1; i <= numcols; i++) {
72                     System.out.print("\t" + rset.getString(i));
73                 }
74                 System.out.println("");
75             }
76         } catch (Exception JavaDoc e) {
77             e.printStackTrace();
78         }
79     }
80
81     public static void main(String JavaDoc[] args) {
82         
83         if (args.length != 2|| (!args[0].equals("commit") && !args[0].equals("rollback"))) {
84           System.err.println("usage: java DistributedDataSourceExample [completion] [number]\n");
85             System.exit(1);
86         }
87     
88         String JavaDoc completion = args[0];
89         int value = 0;
90         try {
91             value = Integer.parseInt(args[1]);
92         } catch (NumberFormatException JavaDoc e) {
93             System.out.println("[number] has to be an integer\n");
94             System.exit(1);
95         }
96
97         UserTransaction JavaDoc utx = null;
98         try {
99             System.out.println("create initial context");
100             InitialContext JavaDoc ictx = new InitialContext JavaDoc();
101             System.out.println("lookup UserTransaction at : UserTransaction");
102             utx = (UserTransaction JavaDoc) ictx.lookup("UserTransaction");
103             System.out.println("lookup DataSource at: DataSource");
104             DataSource JavaDoc ds = (DataSource JavaDoc) ictx.lookup("DataSource");
105             System.out.println("get a connection");
106             conn = ds.getConnection("mojo", "jojo");
107         } catch (Exception JavaDoc e) {
108             System.out.println(
109                 "Exception of type :"
110                     + e.getClass().getName()
111                     + " has been thrown");
112             System.out.println("Exception message :" + e.getMessage());
113             e.printStackTrace();
114             System.exit(1);
115         }
116
117         System.out.println("before transaction, table is:");
118         printTable();
119
120         try {
121             System.out.println("begin a transaction");
122             utx.begin();
123
124             System.out.println("update the table");
125             updateTable(value);
126
127             if (completion.equals("commit")) {
128                 System.out.println("*commit* the transaction");
129                 utx.commit();
130             } else {
131                 System.out.println("*rollback* the transaction");
132                 utx.rollback();
133             }
134         } catch (Exception JavaDoc e) {
135             System.out.println(
136                 "Exception of type :"
137                     + e.getClass().getName()
138                     + " has been thrown");
139             System.out.println("Exception message :" + e.getMessage());
140             e.printStackTrace();
141             System.exit(1);
142         }
143
144         System.out.println("after transaction, table is:");
145         printTable();
146         System.out.println("Recovery Example is ok.\n");
147         System.exit(0);
148     }
149 }
150
Popular Tags