KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FalseRollback


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 java.util.Properties JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.PreparedStatement JavaDoc;
11 import java.sql.Connection JavaDoc;
12
13 /**
14  * this test is used to show the following process:<br>
15  * <pre>
16  * c = ds.getConnection();
17  * c.setAutocommit(true);
18  * ...
19  * c.close();
20  * c = ds.getConnection();
21  * c.setAutoCommit(false);
22  * ...
23  * c.close();
24  * </pre>
25  */

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