KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > transaction > TransactionManager


1 /*
2  * Copyright 2004 Clinton Begin
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 com.ibatis.sqlmap.engine.transaction;
17
18 import com.ibatis.common.util.Throttle;
19 import com.ibatis.sqlmap.engine.scope.SessionScope;
20
21 import javax.sql.DataSource JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 public class TransactionManager {
25
26   private TransactionConfig transactionConfig;
27
28   private boolean forceCommit;
29
30   private Throttle txThrottle;
31
32   public TransactionManager(TransactionConfig transactionConfig) {
33     this.transactionConfig = transactionConfig;
34     this.txThrottle = new Throttle(transactionConfig.getMaximumConcurrentTransactions());
35   }
36
37
38   public void begin(SessionScope session) throws SQLException JavaDoc, TransactionException {
39     begin(session, IsolationLevel.UNSET_ISOLATION_LEVEL);
40   }
41
42   public void begin(SessionScope session, int transactionIsolation) throws SQLException JavaDoc, TransactionException {
43     Transaction trans = session.getTransaction();
44     TransactionState state = session.getTransactionState();
45     if (state == TransactionState.STATE_STARTED) {
46       throw new TransactionException("TransactionManager could not start a new transaction. " +
47           "A transaction is already started.");
48     } else if (state == TransactionState.STATE_USER_PROVIDED) {
49       throw new TransactionException("TransactionManager could not start a new transaction. " +
50           "A user provided connection is currently being used by this session. " +
51           "The calling .setUserConnection (null) will clear the user provided transaction.");
52     }
53
54     txThrottle.increment();
55
56     try {
57       trans = transactionConfig.newTransaction(transactionIsolation);
58       session.setCommitRequired(false);
59     } catch (SQLException JavaDoc e) {
60       txThrottle.decrement();
61       throw e;
62     } catch (TransactionException e) {
63       txThrottle.decrement();
64       throw e;
65     }
66
67     session.setTransaction(trans);
68     session.setTransactionState(TransactionState.STATE_STARTED);
69   }
70
71   public void commit(SessionScope session) throws SQLException JavaDoc, TransactionException {
72     Transaction trans = session.getTransaction();
73     TransactionState state = session.getTransactionState();
74     if (state == TransactionState.STATE_USER_PROVIDED) {
75       throw new TransactionException("TransactionManager could not commit. " +
76           "A user provided connection is currently being used by this session. " +
77           "You must call the commit() method of the Connection directly. " +
78           "The calling .setUserConnection (null) will clear the user provided transaction.");
79     } else if (state != TransactionState.STATE_STARTED) {
80       throw new TransactionException("TransactionManager could not commit. No transaction is started.");
81     }
82     if (session.isCommitRequired() || forceCommit) {
83       trans.commit();
84       session.setCommitRequired(false);
85     }
86     session.setTransactionState(TransactionState.STATE_COMMITTED);
87   }
88
89   public void end(SessionScope session) throws SQLException JavaDoc, TransactionException {
90     Transaction trans = session.getTransaction();
91     TransactionState state = session.getTransactionState();
92
93     if (state == TransactionState.STATE_USER_PROVIDED) {
94       throw new TransactionException("TransactionManager could not end this transaction. " +
95           "A user provided connection is currently being used by this session. " +
96           "You must call the rollback() method of the Connection directly. " +
97           "The calling .setUserConnection (null) will clear the user provided transaction.");
98     }
99
100     try {
101       if (trans != null) {
102         try {
103           if (state != TransactionState.STATE_COMMITTED) {
104             if (session.isCommitRequired() || forceCommit) {
105               trans.rollback();
106               session.setCommitRequired(false);
107             }
108           }
109         } finally {
110           trans.close();
111         }
112       }
113     } finally {
114
115       if (state != TransactionState.STATE_ENDED) {
116         txThrottle.decrement();
117       }
118
119       session.setTransaction(null);
120       session.setTransactionState(TransactionState.STATE_ENDED);
121     }
122   }
123
124   public DataSource JavaDoc getDataSource() {
125     return transactionConfig.getDataSource();
126   }
127
128   public void setDataSource(DataSource JavaDoc ds) {
129     transactionConfig.setDataSource(ds);
130   }
131
132   public boolean isForceCommit() {
133     return forceCommit;
134   }
135
136   public void setForceCommit(boolean forceCommit) {
137     this.forceCommit = forceCommit;
138   }
139
140 }
141
142
Popular Tags