1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.sqlmap.engine.transaction;
17
18 import com.ibatis.sqlmap.engine.scope.SessionScope;
19
20 import java.sql.SQLException;
21
22
23
24
25 public class TransactionManager {
26
27
28 private TransactionConfig config;
29
30
31
32
33
34
35
36 public TransactionManager(TransactionConfig transactionConfig) {
37 this.config = transactionConfig;
38 }
39
40
41
42
43
44
45
46
47
48
49
50
51 public void begin(SessionScope sessionScope) throws SQLException, TransactionException {
52 begin(sessionScope, IsolationLevel.UNSET_ISOLATION_LEVEL);
53 }
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public void begin(SessionScope sessionScope, int transactionIsolation) throws SQLException, TransactionException {
69 Transaction trans = sessionScope.getTransaction();
70 TransactionState state = sessionScope.getTransactionState();
71 if (state == TransactionState.STATE_STARTED) {
72 throw new TransactionException(
73 "TransactionManager could not start a new transaction. " + "A transaction is already started.");
74 } else if (state == TransactionState.STATE_USER_PROVIDED) {
75 throw new TransactionException("TransactionManager could not start a new transaction. "
76 + "A user provided connection is currently being used by this session. "
77 + "The calling .setUserConnection (null) will clear the user provided transaction.");
78 }
79
80 trans = config.newTransaction(transactionIsolation);
81 sessionScope.setCommitRequired(false);
82
83 sessionScope.setTransaction(trans);
84 sessionScope.setTransactionState(TransactionState.STATE_STARTED);
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98 public void commit(SessionScope sessionScope) throws SQLException, TransactionException {
99 Transaction trans = sessionScope.getTransaction();
100 TransactionState state = sessionScope.getTransactionState();
101 if (state == TransactionState.STATE_USER_PROVIDED) {
102 throw new TransactionException("TransactionManager could not commit. "
103 + "A user provided connection is currently being used by this session. "
104 + "You must call the commit() method of the Connection directly. "
105 + "The calling .setUserConnection (null) will clear the user provided transaction.");
106 } else if (state != TransactionState.STATE_STARTED && state != TransactionState.STATE_COMMITTED) {
107 throw new TransactionException("TransactionManager could not commit. No transaction is started.");
108 }
109 if (sessionScope.isCommitRequired() || config.isForceCommit()) {
110 trans.commit();
111 sessionScope.setCommitRequired(false);
112 }
113 sessionScope.setTransactionState(TransactionState.STATE_COMMITTED);
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127 public void end(SessionScope sessionScope) throws SQLException, TransactionException {
128 Transaction trans = sessionScope.getTransaction();
129 TransactionState state = sessionScope.getTransactionState();
130
131 if (state == TransactionState.STATE_USER_PROVIDED) {
132 throw new TransactionException("TransactionManager could not end this transaction. "
133 + "A user provided connection is currently being used by this session. "
134 + "You must call the rollback() method of the Connection directly. "
135 + "The calling .setUserConnection (null) will clear the user provided transaction.");
136 }
137
138 try {
139 if (trans != null) {
140 try {
141 if (state != TransactionState.STATE_COMMITTED) {
142 if (sessionScope.isCommitRequired() || config.isForceCommit()) {
143 trans.rollback();
144 sessionScope.setCommitRequired(false);
145 }
146 }
147 } finally {
148 sessionScope.closePreparedStatements();
149 trans.close();
150 }
151 }
152 } finally {
153 sessionScope.setTransaction(null);
154 sessionScope.setTransactionState(TransactionState.STATE_ENDED);
155 }
156 }
157
158
159
160
161
162
163 public TransactionConfig getConfig() {
164 return config;
165 }
166
167 }