View Javadoc
1   /*
2    * Copyright 2004-2022 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    public void close() {
82      if (delegate != null && sessionScope != null)
83        delegate.endSessionScope(sessionScope);
84      if (sessionScope != null)
85        sessionScope = null;
86      if (delegate != null)
87        delegate = null;
88      if (!closed)
89        closed = true;
90    }
91  
92    public Object insert(String id, Object param) throws SQLException {
93      return delegate.insert(sessionScope, id, param);
94    }
95  
96    public Object insert(String id) throws SQLException {
97      return insert(id, null);
98    }
99  
100   public int update(String id, Object param) throws SQLException {
101     return delegate.update(sessionScope, id, param);
102   }
103 
104   public int update(String id) throws SQLException {
105     return update(id, null);
106   }
107 
108   public int delete(String id, Object param) throws SQLException {
109     return delegate.delete(sessionScope, id, param);
110   }
111 
112   public int delete(String id) throws SQLException {
113     return delete(id, null);
114   }
115 
116   public Object queryForObject(String id, Object paramObject) throws SQLException {
117     return delegate.queryForObject(sessionScope, id, paramObject);
118   }
119 
120   public Object queryForObject(String id) throws SQLException {
121     return queryForObject(id, null);
122   }
123 
124   public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException {
125     return delegate.queryForObject(sessionScope, id, paramObject, resultObject);
126   }
127 
128   public List queryForList(String id, Object paramObject) throws SQLException {
129     return delegate.queryForList(sessionScope, id, paramObject);
130   }
131 
132   public List queryForList(String id) throws SQLException {
133     return queryForList(id, null);
134   }
135 
136   public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException {
137     return delegate.queryForList(sessionScope, id, paramObject, skip, max);
138   }
139 
140   public List queryForList(String id, int skip, int max) throws SQLException {
141     return queryForList(id, null, skip, max);
142   }
143 
144   /**
145    * @deprecated All paginated list features have been deprecated
146    */
147   public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException {
148     return delegate.queryForPaginatedList(sessionScope, id, paramObject, pageSize);
149   }
150 
151   /**
152    * @deprecated All paginated list features have been deprecated
153    */
154   public PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException {
155     return queryForPaginatedList(id, null, pageSize);
156   }
157 
158   public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException {
159     return delegate.queryForMap(sessionScope, id, paramObject, keyProp);
160   }
161 
162   public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException {
163     return delegate.queryForMap(sessionScope, id, paramObject, keyProp, valueProp);
164   }
165 
166   public void queryWithRowHandler(String id, Object paramObject, RowHandler rowHandler) throws SQLException {
167     delegate.queryWithRowHandler(sessionScope, id, paramObject, rowHandler);
168   }
169 
170   public void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException {
171     queryWithRowHandler(id, null, rowHandler);
172   }
173 
174   public void startTransaction() throws SQLException {
175     delegate.startTransaction(sessionScope);
176   }
177 
178   public void startTransaction(int transactionIsolation) throws SQLException {
179     delegate.startTransaction(sessionScope, transactionIsolation);
180   }
181 
182   public void commitTransaction() throws SQLException {
183     delegate.commitTransaction(sessionScope);
184   }
185 
186   public void endTransaction() throws SQLException {
187     delegate.endTransaction(sessionScope);
188   }
189 
190   public void startBatch() throws SQLException {
191     delegate.startBatch(sessionScope);
192   }
193 
194   public int executeBatch() throws SQLException {
195     return delegate.executeBatch(sessionScope);
196   }
197 
198   public List executeBatchDetailed() throws SQLException, BatchException {
199     return delegate.executeBatchDetailed(sessionScope);
200   }
201 
202   public void setUserConnection(Connection connection) throws SQLException {
203     delegate.setUserProvidedTransaction(sessionScope, connection);
204   }
205 
206   /**
207    * TODO Deprecated.
208    *
209    * @return Current connection
210    *
211    * @throws SQLException
212    *           the SQL exception
213    *
214    * @deprecated
215    */
216   public Connection getUserConnection() throws SQLException {
217     return getCurrentConnection();
218   }
219 
220   public Connection getCurrentConnection() throws SQLException {
221     try {
222       Connection conn = null;
223       Transaction trans = delegate.getTransaction(sessionScope);
224       if (trans != null) {
225         conn = trans.getConnection();
226       }
227       return conn;
228     } catch (TransactionException e) {
229       throw new NestedSQLException("Error getting Connection from Transaction.  Cause: " + e, e);
230     }
231   }
232 
233   public DataSource getDataSource() {
234     return delegate.getDataSource();
235   }
236 
237   /**
238    * Gets a mapped statement by ID.
239    *
240    * @param id
241    *          - the ID
242    *
243    * @return - the mapped statement
244    */
245   public MappedStatement getMappedStatement(String id) {
246     return delegate.getMappedStatement(id);
247   }
248 
249   /**
250    * Get the status of lazy loading.
251    *
252    * @return - the status
253    */
254   public boolean isLazyLoadingEnabled() {
255     return delegate.isLazyLoadingEnabled();
256   }
257 
258   /**
259    * Get the status of CGLib enhancements.
260    *
261    * @return - the status
262    */
263   public boolean isEnhancementEnabled() {
264     return delegate.isEnhancementEnabled();
265   }
266 
267   /**
268    * Get the SQL executor.
269    *
270    * @return - the executor
271    */
272   public SqlExecutor getSqlExecutor() {
273     return delegate.getSqlExecutor();
274   }
275 
276   /**
277    * Get the delegate.
278    *
279    * @return - the delegate
280    */
281   public SqlMapExecutorDelegate getDelegate() {
282     return delegate;
283   }
284 
285 }