Source code: com/ibatis/sqlmap/engine/impl/SqlMapSessionImpl.java
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.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.SqlExecutor;
23 import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
24 import com.ibatis.sqlmap.engine.scope.SessionScope;
25 import com.ibatis.sqlmap.engine.transaction.Transaction;
26 import com.ibatis.sqlmap.engine.transaction.TransactionException;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import javax.sql.DataSource;
31 import java.sql.Connection;
32 import java.sql.SQLException;
33 import java.util.List;
34 import java.util.Map;
35
36 /**
37 * Implementation of SqlMapSession
38 */
39 public class SqlMapSessionImpl implements SqlMapSession {
40
41 private static final Log log = LogFactory.getLog(SqlMapSessionImpl.class);
42
43 protected SqlMapExecutorDelegate delegate;
44 protected SessionScope session;
45 protected boolean closed;
46
47 /**
48 * Constructor
49 *
50 * @param client - the client that will use the session
51 */
52 public SqlMapSessionImpl(ExtendedSqlMapClient client) {
53 this.delegate = client.getDelegate();
54 this.session = this.delegate.popSession();
55 this.session.setSqlMapClient(client);
56 this.session.setSqlMapExecutor(client);
57 this.session.setSqlMapTxMgr(client);
58 this.closed = false;
59 }
60
61 /**
62 * Start the session
63 */
64 public void open() {
65 session.setSqlMapTxMgr(this);
66 }
67
68 /**
69 * Getter to tell if the session is still open
70 *
71 * @return - the status of the session
72 */
73 public boolean isClosed() {
74 return closed;
75 }
76
77 public void close() {
78 if (delegate != null && session != null) delegate.pushSession(session);
79 if (session != null) session = null;
80 if (delegate != null) delegate = null;
81 if (!closed) closed = true;
82 }
83
84 public Object insert(String id, Object param) throws SQLException {
85 return delegate.insert(session, id, param);
86 }
87
88 public int update(String id, Object param) throws SQLException {
89 return delegate.update(session, id, param);
90 }
91
92 public int delete(String id, Object param) throws SQLException {
93 return delegate.delete(session, id, param);
94 }
95
96 public Object queryForObject(String id, Object paramObject) throws SQLException {
97 return delegate.queryForObject(session, id, paramObject);
98 }
99
100 public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException {
101 return delegate.queryForObject(session, id, paramObject, resultObject);
102 }
103
104 public List queryForList(String id, Object paramObject) throws SQLException {
105 return delegate.queryForList(session, id, paramObject);
106 }
107
108 public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException {
109 return delegate.queryForList(session, id, paramObject, skip, max);
110 }
111
112 public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException {
113 return delegate.queryForPaginatedList(session, id, paramObject, pageSize);
114 }
115
116 public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException {
117 return delegate.queryForMap(session, id, paramObject, keyProp);
118 }
119
120 public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException {
121 return delegate.queryForMap(session, id, paramObject, keyProp, valueProp);
122 }
123
124 public void queryWithRowHandler(String id, Object paramObject, RowHandler rowHandler) throws SQLException {
125 delegate.queryWithRowHandler(session, id, paramObject, rowHandler);
126 }
127
128 public void startTransaction() throws SQLException {
129 delegate.startTransaction(session);
130 }
131
132 public void startTransaction(int transactionIsolation) throws SQLException {
133 delegate.startTransaction(session, transactionIsolation);
134 }
135
136 public void commitTransaction() throws SQLException {
137 delegate.commitTransaction(session);
138 }
139
140 public void endTransaction() throws SQLException {
141 delegate.endTransaction(session);
142 }
143
144 public void startBatch() throws SQLException {
145 delegate.startBatch(session);
146 }
147
148 public int executeBatch() throws SQLException {
149 return delegate.executeBatch(session);
150 }
151
152 public void setUserConnection(Connection connection) throws SQLException {
153 delegate.setUserProvidedTransaction(session, connection);
154 }
155
156 /**
157 * TODO Deprecated
158 *
159 * @return
160 * @throws SQLException
161 * @deprecated
162 */
163 public Connection getUserConnection() throws SQLException {
164 return getCurrentConnection();
165 }
166
167 public Connection getCurrentConnection() throws SQLException {
168 try {
169 Connection conn = null;
170 Transaction trans = delegate.getTransaction(session);
171 if (trans != null) {
172 conn = trans.getConnection();
173 }
174 return conn;
175 } catch (TransactionException e) {
176 throw new NestedSQLException("Error getting Connection from Transaction. Cause: " + e, e);
177 }
178 }
179
180 public DataSource getDataSource() {
181 return delegate.getDataSource();
182 }
183
184 /**
185 * Gets a mapped statement by ID
186 *
187 * @param id - the ID
188 * @return - the mapped statement
189 */
190 public MappedStatement getMappedStatement(String id) {
191 return delegate.getMappedStatement(id);
192 }
193
194 /**
195 * Get the status of lazy loading
196 *
197 * @return - the status
198 */
199 public boolean isLazyLoadingEnabled() {
200 return delegate.isLazyLoadingEnabled();
201 }
202
203 /**
204 * Get the status of CGLib enhancements
205 *
206 * @return - the status
207 */
208 public boolean isEnhancementEnabled() {
209 return delegate.isEnhancementEnabled();
210 }
211
212 /**
213 * Get the SQL executor
214 *
215 * @return - the executor
216 */
217 public SqlExecutor getSqlExecutor() {
218 return delegate.getSqlExecutor();
219 }
220
221 /**
222 * Get the delegate
223 *
224 * @return - the delegate
225 */
226 public SqlMapExecutorDelegate getDelegate() {
227 return delegate;
228 }
229
230 }