Skip to content

Commit d693245

Browse files
committed
1 parent e60389e commit d693245

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -233,24 +233,28 @@ public void close() {
233233
}
234234

235235
private class SqlSessionInterceptor implements InvocationHandler {
236-
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
237-
final SqlSession sqlSesson = SqlSessionManager.this.localSqlSession.get();
238-
if (sqlSesson != null) {
239-
return method.invoke(sqlSesson, args);
240-
} else {
241-
final SqlSession autoSqlSession = openSession();
242-
try {
243-
final Object result = method.invoke(autoSqlSession, args);
244-
autoSqlSession.commit();
245-
return result;
246-
} catch (Throwable t) {
247-
autoSqlSession.rollback();
248-
throw ExceptionUtil.unwrapThrowable(t);
249-
} finally {
250-
autoSqlSession.close();
251-
}
236+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
237+
final SqlSession sqlSession = SqlSessionManager.this.localSqlSession.get();
238+
if (sqlSession != null) {
239+
try {
240+
return method.invoke(sqlSession, args);
241+
} catch (Throwable t) {
242+
throw ExceptionUtil.unwrapThrowable(t);
243+
}
244+
} else {
245+
final SqlSession autoSqlSession = openSession();
246+
try {
247+
final Object result = method.invoke(autoSqlSession, args);
248+
autoSqlSession.commit();
249+
return result;
250+
} catch (Throwable t) {
251+
autoSqlSession.rollback();
252+
throw ExceptionUtil.unwrapThrowable(t);
253+
} finally {
254+
autoSqlSession.close();
252255
}
253256
}
254257
}
258+
}
255259

256260
}

src/test/java/org/apache/ibatis/session/SqlSessionManagerTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import domain.blog.mappers.BlogMapper;
66
import org.apache.ibatis.BaseDataTest;
77
import org.apache.ibatis.cache.impl.PerpetualCache;
8+
import org.apache.ibatis.exceptions.PersistenceException;
89
import org.apache.ibatis.io.Resources;
910
import org.junit.Before;
1011
import org.junit.BeforeClass;
@@ -17,8 +18,6 @@
1718
import java.util.Map;
1819

1920
import static org.junit.Assert.*;
20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertNull;
2221

2322
public class SqlSessionManagerTest extends BaseDataTest {
2423

@@ -250,8 +249,21 @@ public void shouldThrowExceptionIfMappedStatementDoesNotExist() throws Exception
250249
try {
251250
manager.selectList("ThisStatementDoesNotExist");
252251
fail("Expected exception to be thrown due to statement that does not exist.");
253-
} catch (Exception e) {
252+
} catch (PersistenceException e) {
253+
assertTrue(e.getMessage().contains("does not contain value for ThisStatementDoesNotExist"));
254+
}
255+
}
256+
257+
@Test
258+
public void shouldThrowExceptionIfMappedStatementDoesNotExistAndSqlSessionIsOpen() throws Exception {
259+
try {
260+
manager.startManagedSession();
261+
manager.selectList("ThisStatementDoesNotExist");
262+
fail("Expected exception to be thrown due to statement that does not exist.");
263+
} catch (PersistenceException e) {
254264
assertTrue(e.getMessage().contains("does not contain value for ThisStatementDoesNotExist"));
265+
} finally {
266+
manager.close();
255267
}
256268
}
257269

0 commit comments

Comments
 (0)