KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > ExternalTransaction


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

19
20 package org.apache.cayenne.access;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.cayenne.CayenneException;
27
28 /**
29  * Represents a container-managed transaction.
30  *
31  * @since 1.2 moved to a top-level class.
32  * @author Andrus Adamchik
33  */

34 class ExternalTransaction extends Transaction {
35
36     ExternalTransaction() {
37     }
38
39     ExternalTransaction(TransactionDelegate delegate) {
40         setDelegate(delegate);
41     }
42
43     public synchronized void begin() {
44         if (status != Transaction.STATUS_NO_TRANSACTION) {
45             throw new IllegalStateException JavaDoc(
46                     "Transaction must have 'STATUS_NO_TRANSACTION' to begin. "
47                             + "Current status: "
48                             + Transaction.decodeStatus(status));
49         }
50
51         status = Transaction.STATUS_ACTIVE;
52     }
53
54     public boolean addConnection(String JavaDoc name, Connection JavaDoc connection) throws SQLException JavaDoc {
55         if (super.addConnection(name, connection)) {
56
57             // implicitly begin transaction
58
if (status == Transaction.STATUS_NO_TRANSACTION) {
59                 begin();
60             }
61
62             if (status != Transaction.STATUS_ACTIVE) {
63                 throw new IllegalStateException JavaDoc(
64                         "Transaction must have 'STATUS_ACTIVE' to add a connection. "
65                                 + "Current status: "
66                                 + Transaction.decodeStatus(status));
67             }
68
69             fixConnectionState(connection);
70             return true;
71         }
72         else {
73             return false;
74         }
75
76     }
77
78     public void commit() throws IllegalStateException JavaDoc, SQLException JavaDoc, CayenneException {
79         try {
80             if (status == Transaction.STATUS_NO_TRANSACTION) {
81                 return;
82             }
83
84             if (delegate != null && !delegate.willCommit(this)) {
85                 return;
86             }
87
88             if (status != Transaction.STATUS_ACTIVE) {
89                 throw new IllegalStateException JavaDoc(
90                         "Transaction must have 'STATUS_ACTIVE' to be committed. "
91                                 + "Current status: "
92                                 + Transaction.decodeStatus(status));
93             }
94
95             processCommit();
96
97             status = Transaction.STATUS_COMMITTED;
98             if (delegate != null) {
99                 delegate.didCommit(this);
100             }
101         }
102         finally {
103             close();
104         }
105     }
106
107     public void rollback() throws IllegalStateException JavaDoc, SQLException JavaDoc, CayenneException {
108
109         try {
110             if (status == Transaction.STATUS_NO_TRANSACTION
111                     || status == Transaction.STATUS_ROLLEDBACK
112                     || status == Transaction.STATUS_ROLLING_BACK) {
113                 return;
114             }
115
116             if (delegate != null && !delegate.willRollback(this)) {
117                 return;
118             }
119
120             if (status != Transaction.STATUS_ACTIVE
121                     && status != Transaction.STATUS_MARKED_ROLLEDBACK) {
122                 throw new IllegalStateException JavaDoc(
123                         "Transaction must have 'STATUS_ACTIVE' or 'STATUS_MARKED_ROLLEDBACK' to be rolled back. "
124                                 + "Current status: "
125                                 + Transaction.decodeStatus(status));
126             }
127
128             processRollback();
129
130             status = Transaction.STATUS_ROLLEDBACK;
131             if (delegate != null) {
132                 delegate.didRollback(this);
133             }
134         }
135         finally {
136             close();
137         }
138     }
139
140     void fixConnectionState(Connection JavaDoc connection) throws SQLException JavaDoc {
141         // NOOP
142
}
143
144     void processCommit() throws SQLException JavaDoc, CayenneException {
145         QueryLogger
146                 .logCommitTransaction("no commit - transaction controlled externally.");
147     }
148
149     void processRollback() throws SQLException JavaDoc, CayenneException {
150         QueryLogger
151                 .logRollbackTransaction("no rollback - transaction controlled externally.");
152     }
153
154     /**
155      * Closes all connections associated with transaction.
156      */

157     void close() {
158         if (connections == null || connections.isEmpty()) {
159             return;
160         }
161
162         Iterator JavaDoc it = connections.values().iterator();
163         while (it.hasNext()) {
164             try {
165
166                 ((Connection JavaDoc) it.next()).close();
167             }
168             catch (Throwable JavaDoc th) {
169                 // TODO: chain exceptions...
170
// ignore for now
171
}
172         }
173     }
174 }
175
Popular Tags