1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.juddi.util.jdbc;
17
18 import java.sql.Connection;
19 import java.sql.SQLException;
20 import java.util.Vector;
21
22 /***
23 * Transaction txn = new Transaction();
24 * txn.begin(conn1);
25 * txn.begin(conn2);
26 * txn.begin(conn3);
27 * txn.commit();
28 * txn.rollback();
29 *
30 * @author Graeme Riddell
31 */
32 public class Transaction
33 {
34 /***
35 * Vector of all connections involved in this transaction
36 */
37 private Vector vect = null;
38
39 /***
40 * default constructor
41 */
42 public Transaction()
43 {
44 this.vect = new Vector();
45 }
46
47 /***
48 * If the connection is known then do nothing. If the connection is
49 * new then issue a SQL begin work and hold onto it for later. Actually the
50 * begin work is implicit and autocommit drives whether a transaction is
51 * progressed.
52 */
53 public void begin(Connection conn)
54 throws SQLException
55 {
56
57 if (conn.getAutoCommit() == true)
58 conn.setAutoCommit(false);
59
60
61
62 for (int i=0; i<vect.size(); i++)
63 {
64 if ((Connection)(vect.elementAt(i)) == conn)
65 return;
66 }
67
68
69 vect.add(conn);
70 }
71
72 /***
73 * commit on all connections. This is not XA, but it could be one day.
74 */
75 public void commit()
76 throws SQLException
77 {
78
79 for (int i=0; i<vect.size(); i++)
80 {
81 Connection conn = (Connection)vect.elementAt(i);
82 conn.commit();
83 }
84
85
86 vect.removeAllElements();
87 }
88
89 /***
90 * rollback on all connections. This is not XA, but it could be one day.
91 */
92 public void rollback()
93 throws SQLException
94 {
95
96 for (int i=0; i<vect.size(); i++)
97 {
98 Connection conn = (Connection)vect.elementAt(i);
99 conn.rollback();
100 }
101
102
103 vect.removeAllElements();
104 }
105 }