KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.CayenneException;
28
29 /**
30  * A Cayenne transaction. Currently supports managing JDBC connections.
31  *
32  * @author Andrus Adamchik
33  * @since 1.1
34  */

35 public abstract class Transaction {
36
37     /**
38      * A ThreadLocal that stores current thread transaction.
39      *
40      * @since 1.2
41      */

42     static final ThreadLocal JavaDoc currentTransaction = new InheritableThreadLocal JavaDoc();
43
44     private static final Transaction NO_TRANSACTION = new Transaction() {
45
46         public void begin() {
47
48         }
49
50         public void commit() {
51
52         }
53
54         public void rollback() {
55
56         }
57     };
58
59     public static final int STATUS_ACTIVE = 1;
60     public static final int STATUS_COMMITTING = 2;
61     public static final int STATUS_COMMITTED = 3;
62     public static final int STATUS_ROLLEDBACK = 4;
63     public static final int STATUS_ROLLING_BACK = 5;
64     public static final int STATUS_NO_TRANSACTION = 6;
65     public static final int STATUS_MARKED_ROLLEDBACK = 7;
66
67     protected Map JavaDoc connections;
68     protected int status;
69     protected TransactionDelegate delegate;
70
71     static String JavaDoc decodeStatus(int status) {
72         switch (status) {
73             case STATUS_ACTIVE:
74                 return "STATUS_ACTIVE";
75             case STATUS_COMMITTING:
76                 return "STATUS_COMMITTING";
77             case STATUS_COMMITTED:
78                 return "STATUS_COMMITTED";
79             case STATUS_ROLLEDBACK:
80                 return "STATUS_ROLLEDBACK";
81             case STATUS_ROLLING_BACK:
82                 return "STATUS_ROLLING_BACK";
83             case STATUS_NO_TRANSACTION:
84                 return "STATUS_NO_TRANSACTION";
85             case STATUS_MARKED_ROLLEDBACK:
86                 return "STATUS_MARKED_ROLLEDBACK";
87             default:
88                 return "Unknown Status - " + status;
89         }
90     }
91
92     /**
93      * Binds a Transaction to the current thread.
94      *
95      * @since 1.2
96      */

97     public static void bindThreadTransaction(Transaction transaction) {
98         currentTransaction.set(transaction);
99     }
100
101     /**
102      * Returns a Transaction associated with the current thread, or null if there is no
103      * such Transaction.
104      *
105      * @since 1.2
106      */

107     public static Transaction getThreadTransaction() {
108         return (Transaction) currentTransaction.get();
109     }
110
111     /**
112      * Factory method returning a new transaction instance that would propagate
113      * commit/rollback to participating connections. Connections will be closed when
114      * commit or rollback is called.
115      */

116     public static Transaction internalTransaction(TransactionDelegate delegate) {
117         return new InternalTransaction(delegate);
118     }
119
120     /**
121      * Factory method returning a new transaction instance that would NOT propagate
122      * commit/rollback to participating connections. Connections will still be closed when
123      * commit or rollback is called.
124      */

125     public static Transaction externalTransaction(TransactionDelegate delegate) {
126         return new ExternalTransaction(delegate);
127     }
128
129     /**
130      * Factory method returning a transaction instance that does not alter the state of
131      * participating connections in any way. Commit and rollback methods do not do
132      * anything.
133      */

134     public static Transaction noTransaction() {
135         return NO_TRANSACTION;
136     }
137
138     /**
139      * Creates new inactive transaction.
140      */

141     protected Transaction() {
142         status = STATUS_NO_TRANSACTION;
143     }
144
145     public TransactionDelegate getDelegate() {
146         return delegate;
147     }
148
149     public void setDelegate(TransactionDelegate delegate) {
150         this.delegate = delegate;
151     }
152
153     public int getStatus() {
154         return status;
155     }
156
157     public synchronized void setRollbackOnly() {
158         setStatus(STATUS_MARKED_ROLLEDBACK);
159     }
160
161     public synchronized void setStatus(int status) {
162         if (delegate != null
163                 && status == STATUS_MARKED_ROLLEDBACK
164                 && !delegate.willMarkAsRollbackOnly(this)) {
165             return;
166         }
167
168         this.status = status;
169     }
170
171     /**
172      * Starts a Transaction. If Transaction is not started explicitly, it will be started
173      * when the first connection is added.
174      */

175     public abstract void begin();
176
177     public abstract void commit() throws IllegalStateException JavaDoc, SQLException JavaDoc,
178             CayenneException;
179
180     public abstract void rollback() throws IllegalStateException JavaDoc, SQLException JavaDoc,
181             CayenneException;
182
183     /**
184      * @since 1.2
185      */

186     public Connection JavaDoc getConnection(String JavaDoc name) {
187         return (connections != null) ? (Connection JavaDoc) connections.get(name) : null;
188     }
189
190     /**
191      * @since 1.2
192      */

193     public boolean addConnection(String JavaDoc name, Connection JavaDoc connection) throws SQLException JavaDoc {
194         if (delegate != null && !delegate.willAddConnection(this, connection)) {
195             return false;
196         }
197
198         if (connections == null) {
199             connections = new HashMap JavaDoc();
200         }
201
202         return connections.put(name, connection) != connection;
203     }
204 }
205
Popular Tags