KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SaveAutoCommit


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  * c = ds.getConnection();
19  * c.setAutoCommit(autocommit);
20  * utx.begin();
21  * ...
22  * utx.commit();
23  * autocommit2 = c.getAutoCommit();
24  * // autocommit == autocommit2
25  * c.close();
26  * </pre>
27  */

28 public class SaveAutoCommit {
29     private String JavaDoc SQL_REQUEST = "select id, foo from testdata";
30     private String JavaDoc SQL_QUERY = "update testdata set foo = ? where id=1";
31     private String JavaDoc USAGE = "usage: java SaveAutoCommit [number] [autocommit]";
32
33     private Connection JavaDoc conn;
34     private TMService jotm;
35
36     private String JavaDoc login = null;
37     private String JavaDoc password = null;
38     private String JavaDoc url = null;
39     private String JavaDoc driver = null;
40     private String JavaDoc USER_TRANSACTION_JNDI_NAME = "UserTransaction";
41     private PreparedStatement JavaDoc STMT = null;
42
43     public SaveAutoCommit(String JavaDoc [] args) throws Exception JavaDoc {
44         if (args.length != 2) {
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         // Get a transaction manager
75
try {
76             // creates an instance of JOTM with a local transaction factory which is not bound to a registry
77
jotm = new Jotm(true, false);
78             InitialContext JavaDoc ictx = new InitialContext JavaDoc();
79             ictx.rebind(USER_TRANSACTION_JNDI_NAME, jotm.getUserTransaction());
80         } catch (Exception JavaDoc e) {
81             System.err.println("JOTM problem");
82             e.printStackTrace();
83             System.exit(1);
84         }
85
86         // get an user transaction
87
UserTransaction JavaDoc utx = null;
88         try {
89             System.out.println("create initial context");
90             Context JavaDoc ictx = new InitialContext JavaDoc();
91             System.out.println("lookup UserTransaction at : " + USER_TRANSACTION_JNDI_NAME);
92             utx = (UserTransaction JavaDoc) ictx.lookup(USER_TRANSACTION_JNDI_NAME);
93         } catch (Exception JavaDoc e) {
94             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
95             System.err.println("Exception message :" + e.getMessage());
96             e.printStackTrace();
97             System.exit(1);
98         }
99
100
101         // create an XA pool datasource with a minimum of 4 objects
102
StandardXAPoolDataSource spds = new StandardXAPoolDataSource(4);
103         spds.setMaxSize(15);
104         spds.setMinSize(13);
105         spds.setUser(login);
106         spds.setPassword(password);
107
108         // create an XA datasource which will be given to the XA pool
109
StandardXADataSource xads = new StandardXADataSource();
110         try {
111             xads.setDriverName(driver);
112             xads.setUrl(url);
113             xads.setUser(login);
114             xads.setPassword(password);
115             xads.setTransactionManager(jotm.getTransactionManager());
116         } catch (Exception JavaDoc e) {
117             e.printStackTrace();
118             System.exit(1);
119         }
120         // give the XA datasource to the pool (to create futur objects)
121
spds.setDataSource(xads);
122
123         try {
124             conn = spds.getConnection(login, password);
125             boolean autocom =(new Boolean JavaDoc(args[1].trim())).booleanValue();
126             conn.setAutoCommit(autocom);
127
128             utx.begin();
129             PreparedStatement JavaDoc pstmt = conn.prepareStatement(SQL_QUERY);
130             pstmt.setInt(1, newValue);
131             pstmt.executeUpdate();
132             utx.commit();
133             System.out.println("SetAutoCommit is '"+conn.getAutoCommit()+"'"+
134                     " and should be '"+autocom+"'");
135             conn.close();
136
137         } catch (Exception JavaDoc e) {
138             System.out.println("Exception of type :" + e.getClass().getName() + " has been thrown");
139             System.out.println("Exception message :" + e.getMessage());
140             e.printStackTrace();
141             System.exit(1);
142         }
143
144         stop();
145     }
146
147     public void printTable() {
148         try {
149         STMT = conn.prepareStatement(SQL_REQUEST);
150             ResultSet JavaDoc rset = STMT.executeQuery(SQL_REQUEST);
151             int numcols = rset.getMetaData().getColumnCount();
152             for (int i = 1; i <= numcols; i++) {
153                 System.out.print("\t" + rset.getMetaData().getColumnName(i));
154             }
155             System.out.println();
156             while (rset.next()) {
157                 for (int i = 1; i <= numcols; i++) {
158                     System.out.print("\t" + rset.getString(i));
159                 }
160                 System.out.println("");
161             }
162         } catch (Exception JavaDoc e) {
163             e.printStackTrace();
164         }
165     }
166
167     public void stop() {
168         try {
169            InitialContext JavaDoc ictx = new InitialContext JavaDoc();
170            ictx.unbind(USER_TRANSACTION_JNDI_NAME);
171         } catch (Exception JavaDoc e) {
172             e.printStackTrace();
173         }
174         jotm.stop();
175         jotm = null;
176     }
177
178     static public void main(String JavaDoc [] argv) throws Exception JavaDoc{
179         SaveAutoCommit spdse = new SaveAutoCommit(argv);
180         System.exit(1);
181     }
182 }
183
Popular Tags