KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > scope > SessionScope


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.scope;
17
18 import com.ibatis.sqlmap.client.SqlMapClient;
19 import com.ibatis.sqlmap.client.SqlMapExecutor;
20 import com.ibatis.sqlmap.client.SqlMapTransactionManager;
21 import com.ibatis.sqlmap.engine.transaction.Transaction;
22 import com.ibatis.sqlmap.engine.transaction.TransactionState;
23
24 /**
25  * A Session based implementation of the Scope interface
26  */

27 public class SessionScope extends BaseScope {
28
29   private static long nextId;
30
31   private long id;
32
33   // Used by Any
34
private SqlMapClient sqlMapClient;
35   private SqlMapExecutor sqlMapExecutor;
36   private SqlMapTransactionManager sqlMapTxMgr;
37   private int requestStackDepth;
38
39   // Used by TransactionManager
40
private Transaction transaction;
41   private TransactionState transactionState;
42
43   // Used by SqlMapExecutorDelegate.setUserProvidedTransaction()
44
private TransactionState savedTransactionState;
45
46   // Used by StandardSqlMapClient and GeneralStatement
47
private boolean inBatch;
48
49   // Used by SqlExecutor
50
private Object JavaDoc batch;
51
52   private boolean commitRequired;
53
54   /**
55    * Default constructor
56    */

57   public SessionScope() {
58     reset();
59   }
60
61   /**
62    * Get the SqlMapClient for the session
63    *
64    * @return - the SqlMapClient
65    */

66   public SqlMapClient getSqlMapClient() {
67     return sqlMapClient;
68   }
69
70   /**
71    * Set the SqlMapClient for the session
72    *
73    * @param sqlMapClient - the SqlMapClient
74    */

75   public void setSqlMapClient(SqlMapClient sqlMapClient) {
76     this.sqlMapClient = sqlMapClient;
77   }
78
79   /**
80    * Get the SQL executor for the session
81    *
82    * @return - the SQL executor
83    */

84   public SqlMapExecutor getSqlMapExecutor() {
85     return sqlMapExecutor;
86   }
87
88   /**
89    * Get the SQL executor for the session
90    *
91    * @param sqlMapExecutor - the SQL executor
92    */

93   public void setSqlMapExecutor(SqlMapExecutor sqlMapExecutor) {
94     this.sqlMapExecutor = sqlMapExecutor;
95   }
96
97   /**
98    * Get the transaction manager
99    *
100    * @return - the transaction manager
101    */

102   public SqlMapTransactionManager getSqlMapTxMgr() {
103     return sqlMapTxMgr;
104   }
105
106   /**
107    * Set the transaction manager
108    *
109    * @param sqlMapTxMgr - the transaction manager
110    */

111   public void setSqlMapTxMgr(SqlMapTransactionManager sqlMapTxMgr) {
112     this.sqlMapTxMgr = sqlMapTxMgr;
113   }
114
115   /**
116    * Tells us if we are in batch mode or not
117    *
118    * @return - true if we are working with a batch
119    */

120   public boolean isInBatch() {
121     return inBatch;
122   }
123
124   /**
125    * Turn batch mode on or off
126    *
127    * @param inBatch - the switch
128    */

129   public void setInBatch(boolean inBatch) {
130     this.inBatch = inBatch;
131   }
132
133   /**
134    * Getter for the session transaction
135    *
136    * @return - the transaction
137    */

138   public Transaction getTransaction() {
139     return transaction;
140   }
141
142   /**
143    * Setter for the session transaction
144    *
145    * @param transaction - the transaction
146    */

147   public void setTransaction(Transaction transaction) {
148     this.transaction = transaction;
149   }
150
151   /**
152    * Getter for the transaction state of the session
153    *
154    * @return - the state
155    */

156   public TransactionState getTransactionState() {
157     return transactionState;
158   }
159
160   /**
161    * Setter for the transaction state of the session
162    *
163    * @param transactionState - the new transaction state
164    */

165   public void setTransactionState(TransactionState transactionState) {
166     this.transactionState = transactionState;
167   }
168
169   /**
170    * Getter for the batch of the session
171    *
172    * @return - the batch
173    */

174   public Object JavaDoc getBatch() {
175     return batch;
176   }
177
178   /**
179    * Stter for the batch of the session
180    *
181    * @param batch the new batch
182    */

183   public void setBatch(Object JavaDoc batch) {
184     this.batch = batch;
185   }
186
187   /**
188    * Get the request stack depth
189    *
190    * @return - the stack depth
191    */

192   public int getRequestStackDepth() {
193     return requestStackDepth;
194   }
195
196   /**
197    * Increment the stack depth by one.
198    */

199   public void incrementRequestStackDepth() {
200     requestStackDepth++;
201   }
202
203   /**
204    * Decrement the stack depth by one.
205    */

206   public void decrementRequestStackDepth() {
207     requestStackDepth--;
208   }
209
210   /**
211    * Getter to tell if a commit is required for the session
212    *
213    * @return - true if a commit is required
214    */

215   public boolean isCommitRequired() {
216     return commitRequired;
217   }
218
219   /**
220    * Setter to tell the session that a commit is required for the session
221    *
222    * @param commitRequired - the flag
223    */

224   public void setCommitRequired(boolean commitRequired) {
225     this.commitRequired = commitRequired;
226   }
227
228   public void reset() {
229     super.reset();
230     this.batch = null;
231     sqlMapExecutor = null;
232     sqlMapTxMgr = null;
233     inBatch = false;
234     transaction = null;
235     transactionState = null;
236     batch = null;
237     requestStackDepth = 0;
238     id = getNextId();
239   }
240
241   public boolean equals(Object JavaDoc parameterObject) {
242     if (this == parameterObject) return true;
243     if (!(parameterObject instanceof SessionScope)) return false;
244
245     final SessionScope sessionScope = (SessionScope) parameterObject;
246
247     if (id != sessionScope.id) return false;
248
249     return true;
250   }
251
252   public int hashCode() {
253     return (int) (id ^ (id >>> 32));
254   }
255
256   /**
257    * Method to get a unique ID
258    *
259    * @return - the new ID
260    */

261   public synchronized static long getNextId() {
262     return nextId++;
263   }
264
265   /**
266    * Saves the current transaction state
267    */

268   public void saveTransactionState() {
269     savedTransactionState = transactionState;
270   }
271
272   /**
273    * Restores the previously saved transaction state
274    */

275   public void recallTransactionState() {
276     transactionState = savedTransactionState;
277   }
278 }
279
Popular Tags