KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Connect


1 import java.sql.Connection JavaDoc;
2 import java.sql.PreparedStatement JavaDoc;
3 import java.sql.ResultSet JavaDoc;
4 import java.sql.SQLException JavaDoc;
5 import java.sql.Timestamp JavaDoc;
6
7 import java.util.Properties JavaDoc;
8
9 import javax.naming.Context JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.sql.DataSource JavaDoc;
12 import javax.transaction.RollbackException JavaDoc;
13 import javax.transaction.Status JavaDoc;
14 import javax.transaction.Transaction JavaDoc;
15 import javax.transaction.TransactionManager JavaDoc;
16 import javax.transaction.UserTransaction JavaDoc;
17
18 import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
19 import org.enhydra.jdbc.standard.StandardXADataSource;
20 import org.objectweb.jotm.Jotm;
21 import org.objectweb.transaction.jta.TMService;
22
23 /**
24  * @author jule
25  *
26  * To change this generated comment edit the template variable "typecomment":
27  * Window>Preferences>Java>Templates.
28  * To enable and disable the creation of type comments go to
29  * Window>Preferences>Java>Code Generation.
30  */

31 public class Connect implements Runnable JavaDoc {
32
33     String JavaDoc client;
34     int nb;
35     UserTransaction JavaDoc ut;
36     String JavaDoc productlist;
37     private static TMService jotm;
38     private static TransactionManager JavaDoc tm;
39     private Transaction JavaDoc t;
40     private static StandardXAPoolDataSource spds1;
41     private static StandardXAPoolDataSource spds2;
42
43     /**
44      * Constructor for ConnectOracle.
45      */

46     public Connect(String JavaDoc s, String JavaDoc mydo, int index) {
47     super();
48
49     client = mydo;
50     nb = index;
51     productlist = s;
52
53     }
54
55     private static synchronized void init() {
56     String JavaDoc login = null;
57     String JavaDoc password = null;
58     String JavaDoc url = null;
59     String JavaDoc driver = null;
60
61     //init jotm
62
try {
63         if (jotm == null)
64         jotm = new Jotm(true, false);
65         if (tm == null)
66         tm = jotm.getTransactionManager();
67         if (tm == null) {
68         System.out.println("moniteur transactionel non initialisť");
69         return;
70         }
71
72
73
74         // first, load the properties from the spy.properties file
75
Properties JavaDoc prop = new Properties JavaDoc();
76         try {
77         prop.load(ClassLoader.getSystemResourceAsStream("spy.properties"));
78         } catch (Exception JavaDoc e) {
79         System.err.println("problem to load properties.");
80         e.printStackTrace();
81         System.exit(1);
82         }
83
84         login = prop.getProperty("login");
85         password = prop.getProperty("password");
86         url = prop.getProperty("url");
87         driver = prop.getProperty("driver");
88
89
90
91
92         //init xapool
93
if (spds1 == null) {
94
95         spds1 = new StandardXAPoolDataSource(4);
96         spds1.setMaxSize(15);
97         spds1.setMinSize(13);
98         spds1.setUser(login);
99         spds1.setPassword(password);
100         StandardXADataSource xds1 = new StandardXADataSource();
101
102         xds1.setDriverName(driver);
103         xds1.setUrl(url);
104         xds1.setUser(login);
105         xds1.setPassword(password);
106         spds1.setTransactionManager(tm);
107         spds1.setDataSource(xds1);
108         }
109         if (spds2 == null) {
110         spds2 = new StandardXAPoolDataSource(4);
111         spds2.setMaxSize(15);
112         spds2.setMinSize(13);
113         spds2.setUser(login);
114         spds2.setPassword(password);
115         StandardXADataSource xds2 = new StandardXADataSource();
116
117         xds2.setDriverName(driver);
118         xds2.setUrl(url);
119         xds2.setUser(login);
120         xds2.setPassword(password);
121         spds2.setTransactionManager(tm);
122         spds2.setDataSource(xds2);
123         }
124
125     } catch (Exception JavaDoc e) {
126         System.out.println("Problem jotm/xapool");
127         System.exit(0);
128     }
129     }
130
131     public void run() {
132     int res = 0;
133     //NDC.push(Thread.currentThread().toString());
134
ut = jotm.getUserTransaction();
135     DataSource JavaDoc hds1 = null;
136     PreparedStatement JavaDoc ps1 = null;
137     Connection JavaDoc conn1 = null;
138     PreparedStatement JavaDoc ps2b = null;
139     Connection JavaDoc conn2b = null;
140     DataSource JavaDoc hds2 = null;
141     PreparedStatement JavaDoc ps2 = null;
142     Connection JavaDoc conn2 = null;
143     for (int tr = 0; tr < productlist.length(); tr++) {
144         try {
145
146         if (ut == null)
147             throw new Exception JavaDoc("ut is null");
148         ut.begin();
149
150         //
151
int size = 9;
152         char[] cs = new char[size];
153         char c = productlist.charAt(tr);
154
155         for (int l = 0; l < size; l++)
156             cs[l] = c;
157         String JavaDoc obj = String.valueOf(cs);
158         String JavaDoc objs = Integer.toString(++nb);
159
160         Timestamp JavaDoc dtcr = null;
161
162         try {
163             System.out.println("insert BASE1 ");
164             hds1 = (DataSource JavaDoc) spds1;
165             conn1 = hds1.getConnection();
166
167             dtcr = new Timestamp JavaDoc(System.currentTimeMillis());
168             ps1 =
169             conn1.prepareStatement(
170                            "insert into hproduct (name,createdate,updatedate) VALUES (?,?,?)");
171
172             ps1.setString(1, obj);
173             ps1.setTimestamp(2, dtcr);
174             ps1.setTimestamp(3, dtcr);
175             res = ps1.executeUpdate();
176
177         } catch (Exception JavaDoc e) {
178             System.out.println(
179                        "pb insert BASE1: "
180                        + e.getClass()
181                        + ": "
182                        + e.getMessage());
183             ut.setRollbackOnly();
184
185         } finally {
186             try {
187             if (ps1 != null)
188                 ps1.close();
189             if (conn1 != null)
190                 conn1.close();
191             conn1 = null;
192             } catch (SQLException JavaDoc sqle) {
193             System.out.println(
194                        "close pb BASE1: "
195                        + sqle.getClass()
196                        + ": "
197                        + sqle.getMessage());
198             }
199
200         }
201         //
202

203         try {
204             System.out.println("insert BASE2 ");
205             hds2 = (DataSource JavaDoc) spds2;
206             conn2b = hds2.getConnection();
207
208             dtcr = new Timestamp JavaDoc(System.currentTimeMillis());
209             ps2b =
210             conn2b.prepareStatement(
211                         "insert into horder (ordernum,customer,createdate,updatedate) VALUES (?,?,?,?)");
212             ps2b.setString(1, objs);
213             ps2b.setString(2, client);
214             ps2b.setTimestamp(3, dtcr);
215             ps2b.setTimestamp(4, dtcr);
216             res = ps2b.executeUpdate();
217
218         } catch (Exception JavaDoc e) {
219             System.out.println(
220                        "pb insert BASE2: "
221                        + e.getClass()
222                        + ": "
223                        + e.getMessage());
224             ut.setRollbackOnly();
225         } finally {
226             try {
227             if (ps2b != null)
228                 ps2b.close();
229             if (conn2b != null)
230                 conn2b.close();
231             conn2b = null;
232             } catch (SQLException JavaDoc sqle) {
233             System.out.println(
234                        "close pb BASE2: "
235                        + sqle.getClass()
236                        + ": "
237                        + sqle.getMessage());
238             }
239         }
240
241         int stat = ut.getStatus();
242         if (stat == Status.STATUS_ACTIVE) {
243             System.out.println(
244                        "##### ut commit " + obj + " " + objs + " " + client);
245             ut.commit();
246         } else {
247             System.out.println(
248                        "##### ut rollback " + obj + " " + objs + " " + client);
249             ut.rollback();
250         }
251         } catch (Exception JavaDoc e) {
252         System.out.println(
253                    "UT termination: " + e.getClass() + ": " + e.getMessage());
254         try {
255             if (!(e instanceof RollbackException JavaDoc)) {
256             System.out.println("ut rollback...");
257             ut.rollback();
258             }
259         } catch (Exception JavaDoc e1) {
260             System.out.println(
261                        "rollback: " + e1.getClass() + ": " + e1.getMessage());
262         }
263
264         }
265
266     }
267
268     }
269     public static void main(String JavaDoc[] args) {
270
271     init();
272
273     Connect co1 =
274         new Connect("abcdefghijklmnopqrstuvwxyz", "leclerc", 1000);
275
276     Connect co2 =
277         new Connect("ABCDEFGHIJKLMNOPQRSTUVWXYa", "carrefour", 2000);
278
279     Thread JavaDoc t1 = new Thread JavaDoc(co1);
280     Thread JavaDoc t2 = new Thread JavaDoc(co2);
281
282     t1.start();
283     t2.start();
284     try {
285         t1.join();
286         t2.join();
287
288     } catch (InterruptedException JavaDoc ie) {
289
290     } finally {
291         System.exit(0);
292     }
293
294     }
295 }
296
Popular Tags