View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
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    *    https://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.impl;
17  
18  import com.ibatis.common.jdbc.exception.NestedSQLException;
19  import com.ibatis.common.util.PaginatedList;
20  import com.ibatis.sqlmap.client.SqlMapSession;
21  import com.ibatis.sqlmap.client.event.RowHandler;
22  import com.ibatis.sqlmap.engine.execution.BatchException;
23  import com.ibatis.sqlmap.engine.execution.SqlExecutor;
24  import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
25  import com.ibatis.sqlmap.engine.scope.SessionScope;
26  import com.ibatis.sqlmap.engine.transaction.Transaction;
27  import com.ibatis.sqlmap.engine.transaction.TransactionException;
28  
29  import java.sql.Connection;
30  import java.sql.SQLException;
31  import java.util.List;
32  import java.util.Map;
33  
34  import javax.sql.DataSource;
35  
36  /**
37   * Implementation of SqlMapSession.
38   */
39  public class SqlMapSessionImpl implements SqlMapSession {
40  
41    /** The delegate. */
42    protected SqlMapExecutorDelegate delegate;
43  
44    /** The session scope. */
45    protected SessionScope sessionScope;
46  
47    /** The closed. */
48    protected boolean closed;
49  
50    /**
51     * Constructor.
52     *
53     * @param client
54     *          - the client that will use the session
55     */
56    public SqlMapSessionImpl(SqlMapClientImpl client) {
57      this.delegate = client.getDelegate();
58      this.sessionScope = this.delegate.beginSessionScope();
59      this.sessionScope.setSqlMapClient(client);
60      this.sessionScope.setSqlMapExecutor(client);
61      this.sessionScope.setSqlMapTxMgr(client);
62      this.closed = false;
63    }
64  
65    /**
66     * Start the session.
67     */
68    public void open() {
69      sessionScope.setSqlMapTxMgr(this);
70    }
71  
72    /**
73     * Getter to tell if the session is still open.
74     *
75     * @return - the status of the session
76     */
77    public boolean isClosed() {
78      return closed;
79    }
80  
81    @Override
82    public void close() {
83      if (delegate != null && sessionScope != null) {
84        delegate.endSessionScope(sessionScope);
85      }
86      if (sessionScope != null) {
87        sessionScope = null;
88      }
89      if (delegate != null) {
90        delegate = null;
91      }
92      if (!closed) {
93        closed = true;
94      }
95    }
96  
97    @Override
98    public Object insert(String id, Object param) throws SQLException {
99      return delegate.insert(sessionScope, id, param);
100   }
101 
102   @Override
103   public Object insert(String id) throws SQLException {
104     return insert(id, null);
105   }
106 
107   @Override
108   public int update(String id, Object param) throws SQLException {
109     return delegate.update(sessionScope, id, param);
110   }
111 
112   @Override
113   public int update(String id) throws SQLException {
114     return update(id, null);
115   }
116 
117   @Override
118   public int delete(String id, Object param) throws SQLException {
119     return delegate.delete(sessionScope, id, param);
120   }
121 
122   @Override
123   public int delete(String id) throws SQLException {
124     return delete(id, null);
125   }
126 
127   @Override
128   public Object queryForObject(String id, Object paramObject) throws SQLException {
129     return delegate.queryForObject(sessionScope, id, paramObject);
130   }
131 
132   @Override
133   public Object queryForObject(String id) throws SQLException {
134     return queryForObject(id, null);
135   }
136 
137   @Override
138   public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException {
139     return delegate.queryForObject(sessionScope, id, paramObject, resultObject);
140   }
141 
142   @Override
143   public List queryForList(String id, Object paramObject) throws SQLException {
144     return delegate.queryForList(sessionScope, id, paramObject);
145   }
146 
147   @Override
148   public List queryForList(String id) throws SQLException {
149     return queryForList(id, null);
150   }
151 
152   @Override
153   public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException {
154     return delegate.queryForList(sessionScope, id, paramObject, skip, max);
155   }
156 
157   @Override
158   public List queryForList(String id, int skip, int max) throws SQLException {
159     return queryForList(id, null, skip, max);
160   }
161 
162   /**
163    * @deprecated All paginated list features have been deprecated
164    */
165   @Override
166   @Deprecated
167   public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException {
168     return delegate.queryForPaginatedList(sessionScope, id, paramObject, pageSize);
169   }
170 
171   /**
172    * @deprecated All paginated list features have been deprecated
173    */
174   @Override
175   @Deprecated
176   public PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException {
177     return queryForPaginatedList(id, null, pageSize);
178   }
179 
180   @Override
181   public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException {
182     return delegate.queryForMap(sessionScope, id, paramObject, keyProp);
183   }
184 
185   @Override
186   public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException {
187     return delegate.queryForMap(sessionScope, id, paramObject, keyProp, valueProp);
188   }
189 
190   @Override
191   public void queryWithRowHandler(String id, Object paramObject, RowHandler rowHandler) throws SQLException {
192     delegate.queryWithRowHandler(sessionScope, id, paramObject, rowHandler);
193   }
194 
195   @Override
196   public void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException {
197     queryWithRowHandler(id, null, rowHandler);
198   }
199 
200   @Override
201   public void startTransaction() throws SQLException {
202     delegate.startTransaction(sessionScope);
203   }
204 
205   @Override
206   public void startTransaction(int transactionIsolation) throws SQLException {
207     delegate.startTransaction(sessionScope, transactionIsolation);
208   }
209 
210   @Override
211   public void commitTransaction() throws SQLException {
212     delegate.commitTransaction(sessionScope);
213   }
214 
215   @Override
216   public void endTransaction() throws SQLException {
217     delegate.endTransaction(sessionScope);
218   }
219 
220   @Override
221   public void startBatch() throws SQLException {
222     delegate.startBatch(sessionScope);
223   }
224 
225   @Override
226   public int executeBatch() throws SQLException {
227     return delegate.executeBatch(sessionScope);
228   }
229 
230   @Override
231   public List executeBatchDetailed() throws SQLException, BatchException {
232     return delegate.executeBatchDetailed(sessionScope);
233   }
234 
235   @Override
236   public void setUserConnection(Connection connection) throws SQLException {
237     delegate.setUserProvidedTransaction(sessionScope, connection);
238   }
239 
240   /**
241    * Get User connection.
242    *
243    * @return Current connection
244    *
245    * @throws SQLException
246    *           the SQL exception
247    *
248    * @deprecated Use {@link #getCurrentConnection()} instead.
249    */
250   @Override
251   @Deprecated
252   public Connection getUserConnection() throws SQLException {
253     return getCurrentConnection();
254   }
255 
256   @Override
257   public Connection getCurrentConnection() throws SQLException {
258     try {
259       Connection conn = null;
260       Transaction trans = delegate.getTransaction(sessionScope);
261       if (trans != null) {
262         conn = trans.getConnection();
263       }
264       return conn;
265     } catch (TransactionException e) {
266       throw new NestedSQLException("Error getting Connection from Transaction.  Cause: " + e, e);
267     }
268   }
269 
270   @Override
271   public DataSource getDataSource() {
272     return delegate.getDataSource();
273   }
274 
275   /**
276    * Gets a mapped statement by ID.
277    *
278    * @param id
279    *          - the ID
280    *
281    * @return - the mapped statement
282    */
283   public MappedStatement getMappedStatement(String id) {
284     return delegate.getMappedStatement(id);
285   }
286 
287   /**
288    * Get the status of lazy loading.
289    *
290    * @return - the status
291    */
292   public boolean isLazyLoadingEnabled() {
293     return delegate.isLazyLoadingEnabled();
294   }
295 
296   /**
297    * Get the status of CGLib enhancements.
298    *
299    * @return - the status
300    */
301   public boolean isEnhancementEnabled() {
302     return delegate.isEnhancementEnabled();
303   }
304 
305   /**
306    * Get the SQL executor.
307    *
308    * @return - the executor
309    */
310   public SqlExecutor getSqlExecutor() {
311     return delegate.getSqlExecutor();
312   }
313 
314   /**
315    * Get the delegate.
316    *
317    * @return - the delegate
318    */
319   public SqlMapExecutorDelegate getDelegate() {
320     return delegate;
321   }
322 
323 }