1 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 ; 29 import java.sql.Connection ; 30 import java.sql.SQLException ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 37 public class SqlMapSessionImpl implements SqlMapSession { 38 39 protected SqlMapExecutorDelegate delegate; 40 protected SessionScope session; 41 protected boolean closed; 42 43 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 60 public void open() { 61 session.setSqlMapTxMgr(this); 62 } 63 64 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 insert(String id, Object param) throws SQLException { 81 return delegate.insert(session, id, param); 82 } 83 84 public int update(String id, Object param) throws SQLException { 85 return delegate.update(session, id, param); 86 } 87 88 public int delete(String id, Object param) throws SQLException { 89 return delegate.delete(session, id, param); 90 } 91 92 public Object queryForObject(String id, Object paramObject) throws SQLException { 93 return delegate.queryForObject(session, id, paramObject); 94 } 95 96 public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException { 97 return delegate.queryForObject(session, id, paramObject, resultObject); 98 } 99 100 public List queryForList(String id, Object paramObject) throws SQLException { 101 return delegate.queryForList(session, id, paramObject); 102 } 103 104 public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException { 105 return delegate.queryForList(session, id, paramObject, skip, max); 106 } 107 108 public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException { 109 return delegate.queryForPaginatedList(session, id, paramObject, pageSize); 110 } 111 112 public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException { 113 return delegate.queryForMap(session, id, paramObject, keyProp); 114 } 115 116 public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException { 117 return delegate.queryForMap(session, id, paramObject, keyProp, valueProp); 118 } 119 120 public void queryWithRowHandler(String id, Object 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 connection) throws SQLException { 149 delegate.setUserProvidedTransaction(session, connection); 150 } 151 152 159 public Connection getUserConnection() throws SQLException { 160 return getCurrentConnection(); 161 } 162 163 public Connection getCurrentConnection() throws SQLException { 164 try { 165 Connection 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 getDataSource() { 177 return delegate.getDataSource(); 178 } 179 180 186 public MappedStatement getMappedStatement(String id) { 187 return delegate.getMappedStatement(id); 188 } 189 190 195 public boolean isLazyLoadingEnabled() { 196 return delegate.isLazyLoadingEnabled(); 197 } 198 199 204 public boolean isEnhancementEnabled() { 205 return delegate.isEnhancementEnabled(); 206 } 207 208 213 public SqlExecutor getSqlExecutor() { 214 return delegate.getSqlExecutor(); 215 } 216 217 222 public SqlMapExecutorDelegate getDelegate() { 223 return delegate; 224 } 225 226 } 227 | Popular Tags |