Skip to content

Commit 2e775bf

Browse files
committed
Implemented ThreadLocal SqlSession.
1 parent 0e387db commit 2e775bf

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

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

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class SqlSessionManager implements SqlSessionFactory, SqlSession {
1414
private final SqlSession sqlSessionProxy;
1515
private final InvocationHandler sqlSessionInterceptor;
1616

17-
private SqlSession currentSqlSession;
17+
private ThreadLocal<SqlSession> localSqlSession = new ThreadLocal<SqlSession>();
1818

1919
public SqlSessionManager(Reader reader) {
2020
this(new SqlSessionFactoryBuilder().build(reader, null, null));
@@ -35,35 +35,35 @@ public SqlSessionManager(SqlSessionFactory sqlSessionFactory) {
3535
}
3636

3737
public void startManagedSession() {
38-
this.currentSqlSession = openSession();
38+
this.localSqlSession.set(openSession());
3939
}
4040

4141
public void startManagedSession(boolean autoCommit) {
42-
this.currentSqlSession = openSession(autoCommit);
42+
this.localSqlSession.set(openSession(autoCommit));
4343
}
4444

4545
public void startManagedSession(Connection connection) {
46-
this.currentSqlSession = openSession(connection);
46+
this.localSqlSession.set(openSession(connection));
4747
}
4848

4949
public void startManagedSession(TransactionIsolationLevel level) {
50-
this.currentSqlSession = openSession(level);
50+
this.localSqlSession.set(openSession(level));
5151
}
5252

5353
public void startManagedSession(ExecutorType execType) {
54-
this.currentSqlSession = openSession(execType);
54+
this.localSqlSession.set(openSession(execType));
5555
}
5656

5757
public void startManagedSession(ExecutorType execType, boolean autoCommit) {
58-
this.currentSqlSession = openSession(execType, autoCommit);
58+
this.localSqlSession.set(openSession(execType, autoCommit));
5959
}
6060

6161
public void startManagedSession(ExecutorType execType, TransactionIsolationLevel level) {
62-
this.currentSqlSession = openSession(execType, level);
62+
this.localSqlSession.set(openSession(execType, level));
6363
}
6464

6565
public void startManagedSession(ExecutorType execType, Connection connection) {
66-
this.currentSqlSession = openSession(execType, connection);
66+
this.localSqlSession.set(openSession(execType, connection));
6767
}
6868

6969
public SqlSession openSession() {
@@ -163,63 +163,67 @@ public <T> T getMapper(Class<T> type) {
163163
}
164164

165165
public Connection getConnection() {
166-
if (currentSqlSession == null) throw new SqlSessionException("Error: Cannot get connection. No managed session is started.");
167-
return currentSqlSession.getConnection();
166+
final SqlSession sqlSession = localSqlSession.get();
167+
if (sqlSession == null) throw new SqlSessionException("Error: Cannot get connection. No managed session is started.");
168+
return sqlSession.getConnection();
168169
}
169170

170171
public void clearCache() {
171-
if (currentSqlSession == null) throw new SqlSessionException("Error: Cannot clear the cache. No managed session is started.");
172-
currentSqlSession.clearCache();
172+
final SqlSession sqlSession = localSqlSession.get();
173+
if (sqlSession == null) throw new SqlSessionException("Error: Cannot clear the cache. No managed session is started.");
174+
sqlSession.clearCache();
173175
}
174176

175177
public void commit() {
176-
if (currentSqlSession == null) throw new SqlSessionException("Error: Cannot commit. No managed session is started.");
177-
currentSqlSession.commit();
178+
final SqlSession sqlSession = localSqlSession.get();
179+
if (sqlSession == null) throw new SqlSessionException("Error: Cannot commit. No managed session is started.");
180+
sqlSession.commit();
178181
}
179182

180183
public void commit(boolean force) {
181-
if (currentSqlSession == null) throw new SqlSessionException("Error: Cannot commit. No managed session is started.");
182-
currentSqlSession.commit(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);
183187
}
184188

185189
public void rollback() {
186-
if (currentSqlSession == null) throw new SqlSessionException("Error: Cannot rollback. No managed session is started.");
187-
currentSqlSession.rollback();
190+
final SqlSession sqlSession = localSqlSession.get();
191+
if (sqlSession == null) throw new SqlSessionException("Error: Cannot rollback. No managed session is started.");
192+
sqlSession.rollback();
188193
}
189194

190195
public void rollback(boolean force) {
191-
if (currentSqlSession == null) throw new SqlSessionException("Error: Cannot rollback. No managed session is started.");
192-
currentSqlSession.rollback(force);
196+
final SqlSession sqlSession = localSqlSession.get();
197+
if (sqlSession == null) throw new SqlSessionException("Error: Cannot rollback. No managed session is started.");
198+
sqlSession.rollback(force);
193199
}
194200

195201
public void close() {
196-
if (currentSqlSession == null) throw new SqlSessionException("Error: Cannot close. No managed session is started.");
202+
final SqlSession sqlSession = localSqlSession.get();
203+
if (sqlSession == null) throw new SqlSessionException("Error: Cannot close. No managed session is started.");
197204
try {
198-
currentSqlSession.close();
205+
sqlSession.close();
199206
} finally {
200-
currentSqlSession = null;
207+
localSqlSession.set(null);
201208
}
202209
}
203210

204211
private class SqlSessionInterceptor implements InvocationHandler {
205212
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();
213+
final SqlSession sqlSesson = SqlSessionManager.this.localSqlSession.get();
214+
if (sqlSesson != null) {
215+
return method.invoke(sqlSesson, args);
216+
} else {
217+
final SqlSession autoSqlSession = openSession();
218+
try {
219+
final Object result = method.invoke(autoSqlSession, args);
220+
autoSqlSession.commit();
221+
return result;
222+
} catch (Throwable t) {
223+
autoSqlSession.rollback();
224+
throw t;
225+
} finally {
226+
autoSqlSession.close();
223227
}
224228
}
225229
}

0 commit comments

Comments
 (0)