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