1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.sqlmap.engine.scope;
17
18 import com.ibatis.sqlmap.client.SqlMapClient;
19 import com.ibatis.sqlmap.client.SqlMapException;
20 import com.ibatis.sqlmap.client.SqlMapExecutor;
21 import com.ibatis.sqlmap.client.SqlMapTransactionManager;
22 import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
23 import com.ibatis.sqlmap.engine.transaction.Transaction;
24 import com.ibatis.sqlmap.engine.transaction.TransactionState;
25
26 import java.sql.PreparedStatement;
27 import java.sql.SQLException;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.Map;
31
32
33
34
35 public class SessionScope {
36
37
38 private static long nextId;
39
40
41 private long id;
42
43
44
45 private SqlMapClient sqlMapClient;
46
47
48 private SqlMapExecutor sqlMapExecutor;
49
50
51 private SqlMapTransactionManager sqlMapTxMgr;
52
53
54 private int requestStackDepth;
55
56
57
58 private Transaction transaction;
59
60
61 private TransactionState transactionState;
62
63
64
65 private TransactionState savedTransactionState;
66
67
68
69 private boolean inBatch;
70
71
72
73 private Object batch;
74
75
76 private boolean commitRequired;
77
78
79 private Map preparedStatements;
80
81
82
83
84 public SessionScope() {
85 this.preparedStatements = new HashMap();
86 this.inBatch = false;
87 this.requestStackDepth = 0;
88 this.id = getNextId();
89 }
90
91
92
93
94
95
96 public SqlMapClient getSqlMapClient() {
97 return sqlMapClient;
98 }
99
100
101
102
103
104
105
106 public void setSqlMapClient(SqlMapClient sqlMapClient) {
107 this.sqlMapClient = sqlMapClient;
108 }
109
110
111
112
113
114
115 public SqlMapExecutor getSqlMapExecutor() {
116 return sqlMapExecutor;
117 }
118
119
120
121
122
123
124
125 public void setSqlMapExecutor(SqlMapExecutor sqlMapExecutor) {
126 this.sqlMapExecutor = sqlMapExecutor;
127 }
128
129
130
131
132
133
134 public SqlMapTransactionManager getSqlMapTxMgr() {
135 return sqlMapTxMgr;
136 }
137
138
139
140
141
142
143
144 public void setSqlMapTxMgr(SqlMapTransactionManager sqlMapTxMgr) {
145 this.sqlMapTxMgr = sqlMapTxMgr;
146 }
147
148
149
150
151
152
153 public boolean isInBatch() {
154 return inBatch;
155 }
156
157
158
159
160
161
162
163 public void setInBatch(boolean inBatch) {
164 this.inBatch = inBatch;
165 }
166
167
168
169
170
171
172 public Transaction getTransaction() {
173 return transaction;
174 }
175
176
177
178
179
180
181
182 public void setTransaction(Transaction transaction) {
183 this.transaction = transaction;
184 }
185
186
187
188
189
190
191 public TransactionState getTransactionState() {
192 return transactionState;
193 }
194
195
196
197
198
199
200
201 public void setTransactionState(TransactionState transactionState) {
202 this.transactionState = transactionState;
203 }
204
205
206
207
208
209
210 public Object getBatch() {
211 return batch;
212 }
213
214
215
216
217
218
219
220 public void setBatch(Object batch) {
221 this.batch = batch;
222 }
223
224
225
226
227
228
229 public int getRequestStackDepth() {
230 return requestStackDepth;
231 }
232
233
234
235
236 public void incrementRequestStackDepth() {
237 requestStackDepth++;
238 }
239
240
241
242
243 public void decrementRequestStackDepth() {
244 requestStackDepth--;
245 }
246
247
248
249
250
251
252 public boolean isCommitRequired() {
253 return commitRequired;
254 }
255
256
257
258
259
260
261
262 public void setCommitRequired(boolean commitRequired) {
263 this.commitRequired = commitRequired;
264 }
265
266
267
268
269
270
271
272
273
274 public boolean hasPreparedStatementFor(String sql) {
275 return preparedStatements.containsKey(sql);
276 }
277
278
279
280
281
282
283
284
285
286 public boolean hasPreparedStatement(PreparedStatement ps) {
287 return preparedStatements.containsValue(ps);
288 }
289
290
291
292
293
294
295
296
297
298
299
300
301 public PreparedStatement getPreparedStatement(String sql) throws SQLException {
302 if (!hasPreparedStatementFor(sql))
303 throw new SqlMapException("Could not get prepared statement. This is likely a bug.");
304 PreparedStatement ps = (PreparedStatement) preparedStatements.get(sql);
305 return ps;
306 }
307
308
309
310
311
312
313
314
315
316
317
318 public void putPreparedStatement(SqlMapExecutorDelegate delegate, String sql, PreparedStatement ps) {
319 if (delegate.isStatementCacheEnabled()) {
320 if (!isInBatch()) {
321 if (hasPreparedStatementFor(sql))
322 throw new SqlMapException("Duplicate prepared statement found. This is likely a bug.");
323 preparedStatements.put(sql, ps);
324 }
325 }
326 }
327
328
329
330
331 public void closePreparedStatements() {
332 Iterator keys = preparedStatements.keySet().iterator();
333 while (keys.hasNext()) {
334 PreparedStatement ps = (PreparedStatement) preparedStatements.get(keys.next());
335 try {
336 ps.close();
337 } catch (Exception e) {
338
339 }
340 }
341 preparedStatements.clear();
342 }
343
344
345
346
347 public void cleanup() {
348 closePreparedStatements();
349 preparedStatements.clear();
350 }
351
352 @Override
353 public boolean equals(Object parameterObject) {
354 if (this == parameterObject)
355 return true;
356 if (!(parameterObject instanceof SessionScope))
357 return false;
358 final SessionScope sessionScope = (SessionScope) parameterObject;
359 if (id != sessionScope.id)
360 return false;
361 return true;
362 }
363
364 @Override
365 public int hashCode() {
366 return (int) (id ^ (id >>> 32));
367 }
368
369
370
371
372
373
374 public synchronized static long getNextId() {
375 return nextId++;
376 }
377
378
379
380
381 public void saveTransactionState() {
382 savedTransactionState = transactionState;
383 }
384
385
386
387
388 public void recallTransactionState() {
389 transactionState = savedTransactionState;
390 }
391
392 }