Skip to content

Commit 3890691

Browse files
committed
Preserve original MyBatis exception message
1 parent 05247f9 commit 3890691

File tree

3 files changed

+16
-56
lines changed

3 files changed

+16
-56
lines changed

src/main/java/org/mybatis/spring/DataAccessExceptionTranslator.java renamed to src/main/java/org/mybatis/spring/MyBatisExceptionTranslator.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919

2020
import javax.sql.DataSource;
2121

22-
import org.apache.ibatis.exceptions.PersistenceException;
23-
import org.apache.ibatis.session.SqlSession;
2422
import org.springframework.dao.DataAccessException;
23+
import org.springframework.dao.support.PersistenceExceptionTranslator;
2524
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
2625
import org.springframework.jdbc.support.SQLExceptionTranslator;
2726

@@ -34,10 +33,9 @@
3433
* first exception is translated.
3534
*
3635
* @see DataAccessException
37-
* @see SqlSession
3836
* @version $Id$
3937
*/
40-
public class DataAccessExceptionTranslator implements SqlSessionExceptionTranslator {
38+
public class MyBatisExceptionTranslator implements PersistenceExceptionTranslator {
4139

4240
private final Object $lock = new Object();
4341

@@ -52,7 +50,7 @@ public class DataAccessExceptionTranslator implements SqlSessionExceptionTransla
5250
* @param exceptionTranslatorLazyInit if true, the translator instantiates internal stuff only the first time will
5351
* have the need to translate exceptions.
5452
*/
55-
public DataAccessExceptionTranslator(DataSource dataSource, boolean exceptionTranslatorLazyInit) {
53+
public MyBatisExceptionTranslator(DataSource dataSource, boolean exceptionTranslatorLazyInit) {
5654
this.dataSource = dataSource;
5755

5856
if (!exceptionTranslatorLazyInit) {
@@ -63,17 +61,16 @@ public DataAccessExceptionTranslator(DataSource dataSource, boolean exceptionTra
6361
/**
6462
* {@inheritDoc}
6563
*/
66-
public RuntimeException translateException(PersistenceException e, String statement) {
64+
public DataAccessException translateExceptionIfPossible(RuntimeException e) {
6765
if (e.getCause() instanceof SQLException) {
6866
if (this.exceptionTranslator == null) {
6967
synchronized (this.$lock) {
7068
this.initExceptionTranslator();
7169
}
7270
}
73-
74-
return this.exceptionTranslator.translate("SqlSession operation", statement, (SQLException) e.getCause());
71+
return this.exceptionTranslator.translate(e.getMessage() + "\n", null, (SQLException) e.getCause());
7572
}
76-
return new MyBatisSystemException("SqlSession operation", e);
73+
return new MyBatisSystemException(e.getMessage(), e);
7774
}
7875

7976
/**
@@ -82,5 +79,4 @@ public RuntimeException translateException(PersistenceException e, String statem
8279
private void initExceptionTranslator() {
8380
this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(this.dataSource);
8481
}
85-
8682
}

src/main/java/org/mybatis/spring/SqlSessionExceptionTranslator.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/main/java/org/mybatis/spring/SqlSessionTemplate.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.ibatis.session.RowBounds;
3131
import org.apache.ibatis.session.SqlSession;
3232
import org.apache.ibatis.session.SqlSessionFactory;
33+
import org.springframework.dao.support.PersistenceExceptionTranslator;
3334
import org.springframework.jdbc.support.SQLExceptionTranslator;
3435
import org.springframework.util.Assert;
3536

@@ -46,7 +47,7 @@
4647
* will be used.
4748
* <p>
4849
* This template converts MyBatis PersistenceExceptions into unchecked
49-
* DataAccessExceptions, using, by default, a {@link DataAccessExceptionTranslator}.
50+
* DataAccessExceptions, using, by default, a {@link MyBatisExceptionTranslator}.
5051
* <p>
5152
* Because SqlSessionTemplate is thread safe, a single instance can be shared
5253
* by all DAOs; there should also be a small memory savings by doing this. This
@@ -63,7 +64,7 @@
6364
* @see SqlSessionFactory
6465
* @see SqlSession
6566
* @see ExecutorType
66-
* @see DataAccessExceptionTranslator
67+
* @see MyBatisExceptionTranslator
6768
* @see SqlSessionExceptionTranslator
6869
* @version $Id$
6970
*/
@@ -75,7 +76,7 @@ public class SqlSessionTemplate implements SqlSession {
7576

7677
private final SqlSession sqlSessionProxy;
7778

78-
private final SqlSessionExceptionTranslator exceptionTranslator;
79+
private final PersistenceExceptionTranslator exceptionTranslator;
7980

8081
/**
8182
* Constructs a Spring managed SqlSession with the {@link SqlSessionFactory}
@@ -98,7 +99,7 @@ public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
9899
*/
99100
public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
100101
this(sqlSessionFactory, executorType,
101-
new DataAccessExceptionTranslator(
102+
new MyBatisExceptionTranslator(
102103
sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true));
103104
}
104105

@@ -117,7 +118,7 @@ public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType exec
117118
* @param exceptionTranslator
118119
*/
119120
public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
120-
SqlSessionExceptionTranslator exceptionTranslator) {
121+
PersistenceExceptionTranslator exceptionTranslator) {
121122

122123
Assert.notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
123124
Assert.notNull(executorType, "Property 'executorType' is required");
@@ -139,7 +140,7 @@ public ExecutorType getExecutorType() {
139140
return this.executorType;
140141
}
141142

142-
public SqlSessionExceptionTranslator getExceptionTranslator() {
143+
public PersistenceExceptionTranslator getExceptionTranslator() {
143144
return this.exceptionTranslator;
144145
}
145146

@@ -342,15 +343,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
342343
} catch (Throwable t) {
343344
Throwable unwrapped = ExceptionUtil.unwrapThrowable(t);
344345
if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {
345-
String statement = null;
346-
if (args.length > 0 && args[0] instanceof String) {
347-
statement = (String) args[0];
348-
}
349-
throw SqlSessionTemplate.this.exceptionTranslator.translateException(
350-
(PersistenceException) unwrapped, statement);
351-
} else {
352-
throw unwrapped;
353-
}
346+
unwrapped = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);
347+
}
348+
throw unwrapped;
354349
} finally {
355350
SqlSessionUtils.closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
356351
}

0 commit comments

Comments
 (0)