KickJava   Java API By Example, From Geeks To Geeks.

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


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 Cayenne-managed local Transaction.
30  *
31  * @since 1.2 moved to a top-level class.
32  * @author Andrus Adamchik
33  */

34 class InternalTransaction extends ExternalTransaction {
35
36     InternalTransaction(TransactionDelegate delegate) {
37         super(delegate);
38     }
39
40     public void begin() {
41         super.begin();
42         QueryLogger.logBeginTransaction("transaction started.");
43     }
44
45     void fixConnectionState(Connection JavaDoc connection) throws SQLException JavaDoc {
46         if (connection.getAutoCommit()) {
47             // some DBs are very particular about that, (e.g. Informix SE 7.0 per
48
// CAY-179), so do a try-catch and ignore exception
49

50             // TODO: maybe allow adapter to provide transaction instance?
51
try {
52                 connection.setAutoCommit(false);
53             }
54             catch (SQLException JavaDoc cay179Ex) {
55                 // Can't set autocommit, ignoring...
56
}
57         }
58     }
59
60     void processCommit() throws SQLException JavaDoc, CayenneException {
61         status = Transaction.STATUS_COMMITTING;
62
63         if (connections != null && connections.size() > 0) {
64             Throwable JavaDoc deferredException = null;
65             Iterator JavaDoc it = connections.values().iterator();
66             while (it.hasNext()) {
67                 Connection JavaDoc connection = (Connection JavaDoc) it.next();
68                 try {
69
70                     if (deferredException == null) {
71                         connection.commit();
72                     }
73                     else {
74                         // we must do a partial rollback if only to cleanup
75
// uncommitted
76
// connections.
77
connection.rollback();
78                     }
79
80                 }
81                 catch (Throwable JavaDoc th) {
82                     // there is no such thing as "partial" rollback in real
83
// transactions, so we can't set any meaningful status.
84
// status = ?;
85
setRollbackOnly();
86
87                     // stores last exception
88
// TODO: chain exceptions...
89
deferredException = th;
90                 }
91             }
92
93             if (deferredException != null) {
94                 QueryLogger.logRollbackTransaction("transaction rolledback.");
95                 if (deferredException instanceof SQLException JavaDoc) {
96                     throw (SQLException JavaDoc) deferredException;
97                 }
98                 else {
99                     throw new CayenneException(deferredException);
100                 }
101             }
102             else {
103                 QueryLogger.logCommitTransaction("transaction committed.");
104             }
105         }
106     }
107
108     void processRollback() throws SQLException JavaDoc, CayenneException {
109         status = Transaction.STATUS_ROLLING_BACK;
110
111         if (connections != null && connections.size() > 0) {
112             Throwable JavaDoc deferredException = null;
113
114             Iterator JavaDoc it = connections.values().iterator();
115             while (it.hasNext()) {
116                 Connection JavaDoc connection = (Connection JavaDoc) it.next();
117
118                 try {
119                     // continue with rollback even if an exception was thrown before
120
connection.rollback();
121                 }
122                 catch (Throwable JavaDoc th) {
123                     // stores last exception
124
// TODO: chain exceptions...
125
deferredException = th;
126                 }
127             }
128
129             if (deferredException != null) {
130                 if (deferredException instanceof SQLException JavaDoc) {
131                     throw (SQLException JavaDoc) deferredException;
132                 }
133                 else {
134                     throw new CayenneException(deferredException);
135                 }
136             }
137         }
138     }
139 }
140
Popular Tags