KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > impl > SqlMapSessionImpl


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
28 import javax.sql.DataSource JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * Implementation of SqlMapSession
36  */

37 public class SqlMapSessionImpl implements SqlMapSession {
38
39   protected SqlMapExecutorDelegate delegate;
40   protected SessionScope session;
41   protected boolean closed;
42
43   /**
44    * Constructor
45    *
46    * @param client - the client that will use the session
47    */

48   public SqlMapSessionImpl(ExtendedSqlMapClient client) {
49     this.delegate = client.getDelegate();
50     this.session = this.delegate.popSession();
51     this.session.setSqlMapClient(client);
52     this.session.setSqlMapExecutor(client);
53     this.session.setSqlMapTxMgr(client);
54     this.closed = false;
55   }
56
57   /**
58    * Start the session
59    */

60   public void open() {
61     session.setSqlMapTxMgr(this);
62   }
63
64   /**
65    * Getter to tell if the session is still open
66    *
67    * @return - the status of the session
68    */

69   public boolean isClosed() {
70     return closed;
71   }
72
73   public void close() {
74     if (delegate != null && session != null) delegate.pushSession(session);
75     if (session != null) session = null;
76     if (delegate != null) delegate = null;
77     if (!closed) closed = true;
78   }
79
80   public Object JavaDoc insert(String JavaDoc id, Object JavaDoc param) throws SQLException {
81     return delegate.insert(session, id, param);
82   }
83
84   public int update(String JavaDoc id, Object JavaDoc param) throws SQLException {
85     return delegate.update(session, id, param);
86   }
87
88   public int delete(String JavaDoc id, Object JavaDoc param) throws SQLException {
89     return delegate.delete(session, id, param);
90   }
91
92   public Object JavaDoc queryForObject(String JavaDoc id, Object JavaDoc paramObject) throws SQLException {
93     return delegate.queryForObject(session, id, paramObject);
94   }
95
96   public Object JavaDoc queryForObject(String JavaDoc id, Object JavaDoc paramObject, Object JavaDoc resultObject) throws SQLException {
97     return delegate.queryForObject(session, id, paramObject, resultObject);
98   }
99
100   public List queryForList(String JavaDoc id, Object JavaDoc paramObject) throws SQLException {
101     return delegate.queryForList(session, id, paramObject);
102   }
103
104   public List queryForList(String JavaDoc id, Object JavaDoc paramObject, int skip, int max) throws SQLException {
105     return delegate.queryForList(session, id, paramObject, skip, max);
106   }
107
108   public PaginatedList queryForPaginatedList(String JavaDoc id, Object JavaDoc paramObject, int pageSize) throws SQLException {
109     return delegate.queryForPaginatedList(session, id, paramObject, pageSize);
110   }
111
112   public Map JavaDoc queryForMap(String JavaDoc id, Object JavaDoc paramObject, String JavaDoc keyProp) throws SQLException {
113     return delegate.queryForMap(session, id, paramObject, keyProp);
114   }
115
116   public Map JavaDoc queryForMap(String JavaDoc id, Object JavaDoc paramObject, String JavaDoc keyProp, String JavaDoc valueProp) throws SQLException {
117     return delegate.queryForMap(session, id, paramObject, keyProp, valueProp);
118   }
119
120   public void queryWithRowHandler(String JavaDoc id, Object JavaDoc paramObject, RowHandler rowHandler) throws SQLException {
121     delegate.queryWithRowHandler(session, id, paramObject, rowHandler);
122   }
123
124   public void startTransaction() throws SQLException {
125     delegate.startTransaction(session);
126   }
127
128   public void startTransaction(int transactionIsolation) throws SQLException {
129     delegate.startTransaction(session, transactionIsolation);
130   }
131
132   public void commitTransaction() throws SQLException {
133     delegate.commitTransaction(session);
134   }
135
136   public void endTransaction() throws SQLException {
137     delegate.endTransaction(session);
138   }
139
140   public void startBatch() throws SQLException {
141     delegate.startBatch(session);
142   }
143
144   public int executeBatch() throws SQLException {
145     return delegate.executeBatch(session);
146   }
147
148   public void setUserConnection(Connection JavaDoc connection) throws SQLException {
149     delegate.setUserProvidedTransaction(session, connection);
150   }
151
152   /**
153    * TODO Deprecated
154    *
155    * @return
156    * @throws SQLException
157    * @deprecated
158    */

159   public Connection JavaDoc getUserConnection() throws SQLException {
160     return getCurrentConnection();
161   }
162
163   public Connection JavaDoc getCurrentConnection() throws SQLException {
164     try {
165       Connection JavaDoc conn = null;
166       Transaction trans = delegate.getTransaction(session);
167       if (trans != null) {
168         conn = trans.getConnection();
169       }
170       return conn;
171     } catch (TransactionException e) {
172       throw new NestedSQLException("Error getting Connection from Transaction. Cause: " + e, e);
173     }
174   }
175
176   public DataSource JavaDoc getDataSource() {
177     return delegate.getDataSource();
178   }
179
180   /**
181    * Gets a mapped statement by ID
182    *
183    * @param id - the ID
184    * @return - the mapped statement
185    */

186   public MappedStatement getMappedStatement(String JavaDoc id) {
187     return delegate.getMappedStatement(id);
188   }
189
190   /**
191    * Get the status of lazy loading
192    *
193    * @return - the status
194    */

195   public boolean isLazyLoadingEnabled() {
196     return delegate.isLazyLoadingEnabled();
197   }
198
199   /**
200    * Get the status of CGLib enhancements
201    *
202    * @return - the status
203    */

204   public boolean isEnhancementEnabled() {
205     return delegate.isEnhancementEnabled();
206   }
207
208   /**
209    * Get the SQL executor
210    *
211    * @return - the executor
212    */

213   public SqlExecutor getSqlExecutor() {
214     return delegate.getSqlExecutor();
215   }
216
217   /**
218    * Get the delegate
219    *
220    * @return - the delegate
221    */

222   public SqlMapExecutorDelegate getDelegate() {
223     return delegate;
224   }
225
226 }
227
Popular Tags