KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > util > jdbc > Transaction


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.util.jdbc;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.util.Vector JavaDoc;
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 JavaDoc vect = null;
38
39   /**
40    * default constructor
41    */

42   public Transaction()
43   {
44     this.vect = new Vector JavaDoc();
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 JavaDoc conn)
54     throws SQLException JavaDoc
55   {
56     // make sure auto commit is disabled
57
if (conn.getAutoCommit() == true)
58       conn.setAutoCommit(false);
59
60     // If this connection has already been begun then
61
// just return to the caller. Nothing more to do.
62
for (int i=0; i<vect.size(); i++)
63     {
64       if ((Connection JavaDoc)(vect.elementAt(i)) == conn)
65         return;
66     }
67
68     // add new connection to the collection
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 JavaDoc
77   {
78     // loop through all connections and commit them
79
for (int i=0; i<vect.size(); i++)
80     {
81       Connection JavaDoc conn = (Connection JavaDoc)vect.elementAt(i);
82       conn.commit();
83     }
84
85     // they're all committed, now let's discard them
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 JavaDoc
94   {
95     // loop through all collections and roll them back
96
for (int i=0; i<vect.size(); i++)
97     {
98       Connection JavaDoc conn = (Connection JavaDoc)vect.elementAt(i);
99       conn.rollback();
100     }
101
102     // they're all rolled back, now let's discard them
103
vect.removeAllElements();
104   }
105 }
106
Popular Tags