/src/main/java/org/apache/ibatis/session/SqlSessionManager.java

http://mybatis.googlecode.com/ · Java · 295 lines · 221 code · 59 blank · 15 comment · 20 complexity · 5cf4e1036d807061dd4c01945a3ba12a MD5 · raw file

  1. /*
  2. * Copyright 2009-2012 The MyBatis Team
  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 org.apache.ibatis.session;
  17. import java.io.InputStream;
  18. import java.io.Reader;
  19. import java.lang.reflect.InvocationHandler;
  20. import java.lang.reflect.Method;
  21. import java.lang.reflect.Proxy;
  22. import java.sql.Connection;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Properties;
  26. import org.apache.ibatis.executor.BatchResult;
  27. import org.apache.ibatis.reflection.ExceptionUtil;
  28. public class SqlSessionManager implements SqlSessionFactory, SqlSession {
  29. private final SqlSessionFactory sqlSessionFactory;
  30. private final SqlSession sqlSessionProxy;
  31. private ThreadLocal<SqlSession> localSqlSession = new ThreadLocal<SqlSession>();
  32. public static SqlSessionManager newInstance(Reader reader) {
  33. return new SqlSessionManager(new SqlSessionFactoryBuilder().build(reader, null, null));
  34. }
  35. public static SqlSessionManager newInstance(Reader reader, String environment) {
  36. return new SqlSessionManager(new SqlSessionFactoryBuilder().build(reader, environment, null));
  37. }
  38. public static SqlSessionManager newInstance(Reader reader, Properties properties) {
  39. return new SqlSessionManager(new SqlSessionFactoryBuilder().build(reader, null, properties));
  40. }
  41. public static SqlSessionManager newInstance(InputStream inputStream) {
  42. return new SqlSessionManager(new SqlSessionFactoryBuilder().build(inputStream, null, null));
  43. }
  44. public static SqlSessionManager newInstance(InputStream inputStream, String environment) {
  45. return new SqlSessionManager(new SqlSessionFactoryBuilder().build(inputStream, environment, null));
  46. }
  47. public static SqlSessionManager newInstance(InputStream inputStream, Properties properties) {
  48. return new SqlSessionManager(new SqlSessionFactoryBuilder().build(inputStream, null, properties));
  49. }
  50. public static SqlSessionManager newInstance(SqlSessionFactory sqlSessionFactory) {
  51. return new SqlSessionManager(sqlSessionFactory);
  52. }
  53. private SqlSessionManager(SqlSessionFactory sqlSessionFactory) {
  54. this.sqlSessionFactory = sqlSessionFactory;
  55. this.sqlSessionProxy = (SqlSession) Proxy.newProxyInstance(
  56. SqlSessionFactory.class.getClassLoader(),
  57. new Class[]{SqlSession.class},
  58. new SqlSessionInterceptor());
  59. }
  60. public void startManagedSession() {
  61. this.localSqlSession.set(openSession());
  62. }
  63. public void startManagedSession(boolean autoCommit) {
  64. this.localSqlSession.set(openSession(autoCommit));
  65. }
  66. public void startManagedSession(Connection connection) {
  67. this.localSqlSession.set(openSession(connection));
  68. }
  69. public void startManagedSession(TransactionIsolationLevel level) {
  70. this.localSqlSession.set(openSession(level));
  71. }
  72. public void startManagedSession(ExecutorType execType) {
  73. this.localSqlSession.set(openSession(execType));
  74. }
  75. public void startManagedSession(ExecutorType execType, boolean autoCommit) {
  76. this.localSqlSession.set(openSession(execType, autoCommit));
  77. }
  78. public void startManagedSession(ExecutorType execType, TransactionIsolationLevel level) {
  79. this.localSqlSession.set(openSession(execType, level));
  80. }
  81. public void startManagedSession(ExecutorType execType, Connection connection) {
  82. this.localSqlSession.set(openSession(execType, connection));
  83. }
  84. public boolean isManagedSessionStarted() {
  85. return this.localSqlSession.get() != null;
  86. }
  87. public SqlSession openSession() {
  88. return sqlSessionFactory.openSession();
  89. }
  90. public SqlSession openSession(boolean autoCommit) {
  91. return sqlSessionFactory.openSession(autoCommit);
  92. }
  93. public SqlSession openSession(Connection connection) {
  94. return sqlSessionFactory.openSession(connection);
  95. }
  96. public SqlSession openSession(TransactionIsolationLevel level) {
  97. return sqlSessionFactory.openSession(level);
  98. }
  99. public SqlSession openSession(ExecutorType execType) {
  100. return sqlSessionFactory.openSession(execType);
  101. }
  102. public SqlSession openSession(ExecutorType execType, boolean autoCommit) {
  103. return sqlSessionFactory.openSession(execType, autoCommit);
  104. }
  105. public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
  106. return sqlSessionFactory.openSession(execType, level);
  107. }
  108. public SqlSession openSession(ExecutorType execType, Connection connection) {
  109. return sqlSessionFactory.openSession(execType, connection);
  110. }
  111. public Configuration getConfiguration() {
  112. return sqlSessionFactory.getConfiguration();
  113. }
  114. public <T> T selectOne(String statement) {
  115. return sqlSessionProxy.<T> selectOne(statement);
  116. }
  117. public <T> T selectOne(String statement, Object parameter) {
  118. return sqlSessionProxy.<T> selectOne(statement, parameter);
  119. }
  120. public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
  121. return sqlSessionProxy.<K, V> selectMap(statement, mapKey);
  122. }
  123. public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
  124. return sqlSessionProxy.<K, V> selectMap(statement, parameter, mapKey);
  125. }
  126. public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
  127. return sqlSessionProxy.<K, V> selectMap(statement, parameter, mapKey, rowBounds);
  128. }
  129. public <E> List<E> selectList(String statement) {
  130. return sqlSessionProxy.<E> selectList(statement);
  131. }
  132. public <E> List<E> selectList(String statement, Object parameter) {
  133. return sqlSessionProxy.<E> selectList(statement, parameter);
  134. }
  135. public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
  136. return sqlSessionProxy.<E> selectList(statement, parameter, rowBounds);
  137. }
  138. public void select(String statement, ResultHandler handler) {
  139. sqlSessionProxy.select(statement, handler);
  140. }
  141. public void select(String statement, Object parameter, ResultHandler handler) {
  142. sqlSessionProxy.select(statement, parameter, handler);
  143. }
  144. public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
  145. sqlSessionProxy.select(statement, parameter, rowBounds, handler);
  146. }
  147. public int insert(String statement) {
  148. return sqlSessionProxy.insert(statement);
  149. }
  150. public int insert(String statement, Object parameter) {
  151. return sqlSessionProxy.insert(statement, parameter);
  152. }
  153. public int update(String statement) {
  154. return sqlSessionProxy.update(statement);
  155. }
  156. public int update(String statement, Object parameter) {
  157. return sqlSessionProxy.update(statement, parameter);
  158. }
  159. public int delete(String statement) {
  160. return sqlSessionProxy.delete(statement);
  161. }
  162. public int delete(String statement, Object parameter) {
  163. return sqlSessionProxy.delete(statement, parameter);
  164. }
  165. public <T> T getMapper(Class<T> type) {
  166. return getConfiguration().getMapper(type, this);
  167. }
  168. public Connection getConnection() {
  169. final SqlSession sqlSession = localSqlSession.get();
  170. if (sqlSession == null) throw new SqlSessionException("Error: Cannot get connection. No managed session is started.");
  171. return sqlSession.getConnection();
  172. }
  173. public void clearCache() {
  174. final SqlSession sqlSession = localSqlSession.get();
  175. if (sqlSession == null) throw new SqlSessionException("Error: Cannot clear the cache. No managed session is started.");
  176. sqlSession.clearCache();
  177. }
  178. public void commit() {
  179. final SqlSession sqlSession = localSqlSession.get();
  180. if (sqlSession == null) throw new SqlSessionException("Error: Cannot commit. No managed session is started.");
  181. sqlSession.commit();
  182. }
  183. public void commit(boolean force) {
  184. final SqlSession sqlSession = localSqlSession.get();
  185. if (sqlSession == null) throw new SqlSessionException("Error: Cannot commit. No managed session is started.");
  186. sqlSession.commit(force);
  187. }
  188. public void rollback() {
  189. final SqlSession sqlSession = localSqlSession.get();
  190. if (sqlSession == null) throw new SqlSessionException("Error: Cannot rollback. No managed session is started.");
  191. sqlSession.rollback();
  192. }
  193. public void rollback(boolean force) {
  194. final SqlSession sqlSession = localSqlSession.get();
  195. if (sqlSession == null) throw new SqlSessionException("Error: Cannot rollback. No managed session is started.");
  196. sqlSession.rollback(force);
  197. }
  198. public List<BatchResult> flushStatements() {
  199. final SqlSession sqlSession = localSqlSession.get();
  200. if (sqlSession == null) throw new SqlSessionException("Error: Cannot rollback. No managed session is started.");
  201. return sqlSession.flushStatements();
  202. }
  203. public void close() {
  204. final SqlSession sqlSession = localSqlSession.get();
  205. if (sqlSession == null) throw new SqlSessionException("Error: Cannot close. No managed session is started.");
  206. try {
  207. sqlSession.close();
  208. } finally {
  209. localSqlSession.set(null);
  210. }
  211. }
  212. private class SqlSessionInterceptor implements InvocationHandler {
  213. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  214. final SqlSession sqlSession = SqlSessionManager.this.localSqlSession.get();
  215. if (sqlSession != null) {
  216. try {
  217. return method.invoke(sqlSession, args);
  218. } catch (Throwable t) {
  219. throw ExceptionUtil.unwrapThrowable(t);
  220. }
  221. } else {
  222. final SqlSession autoSqlSession = openSession();
  223. try {
  224. final Object result = method.invoke(autoSqlSession, args);
  225. autoSqlSession.commit();
  226. return result;
  227. } catch (Throwable t) {
  228. autoSqlSession.rollback();
  229. throw ExceptionUtil.unwrapThrowable(t);
  230. } finally {
  231. autoSqlSession.close();
  232. }
  233. }
  234. }
  235. }
  236. }