@@ -14,7 +14,7 @@ public class SqlSessionManager implements SqlSessionFactory, SqlSession {
14
14
private final SqlSession sqlSessionProxy ;
15
15
private final InvocationHandler sqlSessionInterceptor ;
16
16
17
- private SqlSession currentSqlSession ;
17
+ private ThreadLocal < SqlSession > localSqlSession = new ThreadLocal < SqlSession >() ;
18
18
19
19
public SqlSessionManager (Reader reader ) {
20
20
this (new SqlSessionFactoryBuilder ().build (reader , null , null ));
@@ -35,35 +35,35 @@ public SqlSessionManager(SqlSessionFactory sqlSessionFactory) {
35
35
}
36
36
37
37
public void startManagedSession () {
38
- this .currentSqlSession = openSession ();
38
+ this .localSqlSession . set ( openSession () );
39
39
}
40
40
41
41
public void startManagedSession (boolean autoCommit ) {
42
- this .currentSqlSession = openSession (autoCommit );
42
+ this .localSqlSession . set ( openSession (autoCommit ) );
43
43
}
44
44
45
45
public void startManagedSession (Connection connection ) {
46
- this .currentSqlSession = openSession (connection );
46
+ this .localSqlSession . set ( openSession (connection ) );
47
47
}
48
48
49
49
public void startManagedSession (TransactionIsolationLevel level ) {
50
- this .currentSqlSession = openSession (level );
50
+ this .localSqlSession . set ( openSession (level ) );
51
51
}
52
52
53
53
public void startManagedSession (ExecutorType execType ) {
54
- this .currentSqlSession = openSession (execType );
54
+ this .localSqlSession . set ( openSession (execType ) );
55
55
}
56
56
57
57
public void startManagedSession (ExecutorType execType , boolean autoCommit ) {
58
- this .currentSqlSession = openSession (execType , autoCommit );
58
+ this .localSqlSession . set ( openSession (execType , autoCommit ) );
59
59
}
60
60
61
61
public void startManagedSession (ExecutorType execType , TransactionIsolationLevel level ) {
62
- this .currentSqlSession = openSession (execType , level );
62
+ this .localSqlSession . set ( openSession (execType , level ) );
63
63
}
64
64
65
65
public void startManagedSession (ExecutorType execType , Connection connection ) {
66
- this .currentSqlSession = openSession (execType , connection );
66
+ this .localSqlSession . set ( openSession (execType , connection ) );
67
67
}
68
68
69
69
public SqlSession openSession () {
@@ -163,63 +163,67 @@ public <T> T getMapper(Class<T> type) {
163
163
}
164
164
165
165
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 ();
168
169
}
169
170
170
171
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 ();
173
175
}
174
176
175
177
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 ();
178
181
}
179
182
180
183
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 );
183
187
}
184
188
185
189
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 ();
188
193
}
189
194
190
195
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 );
193
199
}
194
200
195
201
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." );
197
204
try {
198
- currentSqlSession .close ();
205
+ sqlSession .close ();
199
206
} finally {
200
- currentSqlSession = null ;
207
+ localSqlSession . set ( null ) ;
201
208
}
202
209
}
203
210
204
211
private class SqlSessionInterceptor implements InvocationHandler {
205
212
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 ();
223
227
}
224
228
}
225
229
}
0 commit comments