30
30
import org .apache .ibatis .session .RowBounds ;
31
31
import org .apache .ibatis .session .SqlSession ;
32
32
import org .apache .ibatis .session .SqlSessionFactory ;
33
+ import org .springframework .dao .support .PersistenceExceptionTranslator ;
33
34
import org .springframework .jdbc .support .SQLExceptionTranslator ;
34
35
import org .springframework .util .Assert ;
35
36
46
47
* will be used.
47
48
* <p>
48
49
* This template converts MyBatis PersistenceExceptions into unchecked
49
- * DataAccessExceptions, using, by default, a {@link DataAccessExceptionTranslator }.
50
+ * DataAccessExceptions, using, by default, a {@link MyBatisExceptionTranslator }.
50
51
* <p>
51
52
* Because SqlSessionTemplate is thread safe, a single instance can be shared
52
53
* by all DAOs; there should also be a small memory savings by doing this. This
63
64
* @see SqlSessionFactory
64
65
* @see SqlSession
65
66
* @see ExecutorType
66
- * @see DataAccessExceptionTranslator
67
+ * @see MyBatisExceptionTranslator
67
68
* @see SqlSessionExceptionTranslator
68
69
* @version $Id$
69
70
*/
@@ -75,7 +76,7 @@ public class SqlSessionTemplate implements SqlSession {
75
76
76
77
private final SqlSession sqlSessionProxy ;
77
78
78
- private final SqlSessionExceptionTranslator exceptionTranslator ;
79
+ private final PersistenceExceptionTranslator exceptionTranslator ;
79
80
80
81
/**
81
82
* Constructs a Spring managed SqlSession with the {@link SqlSessionFactory}
@@ -98,7 +99,7 @@ public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
98
99
*/
99
100
public SqlSessionTemplate (SqlSessionFactory sqlSessionFactory , ExecutorType executorType ) {
100
101
this (sqlSessionFactory , executorType ,
101
- new DataAccessExceptionTranslator (
102
+ new MyBatisExceptionTranslator (
102
103
sqlSessionFactory .getConfiguration ().getEnvironment ().getDataSource (), true ));
103
104
}
104
105
@@ -117,7 +118,7 @@ public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType exec
117
118
* @param exceptionTranslator
118
119
*/
119
120
public SqlSessionTemplate (SqlSessionFactory sqlSessionFactory , ExecutorType executorType ,
120
- SqlSessionExceptionTranslator exceptionTranslator ) {
121
+ PersistenceExceptionTranslator exceptionTranslator ) {
121
122
122
123
Assert .notNull (sqlSessionFactory , "Property 'sqlSessionFactory' is required" );
123
124
Assert .notNull (executorType , "Property 'executorType' is required" );
@@ -139,7 +140,7 @@ public ExecutorType getExecutorType() {
139
140
return this .executorType ;
140
141
}
141
142
142
- public SqlSessionExceptionTranslator getExceptionTranslator () {
143
+ public PersistenceExceptionTranslator getExceptionTranslator () {
143
144
return this .exceptionTranslator ;
144
145
}
145
146
@@ -342,15 +343,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
342
343
} catch (Throwable t ) {
343
344
Throwable unwrapped = ExceptionUtil .unwrapThrowable (t );
344
345
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 ;
354
349
} finally {
355
350
SqlSessionUtils .closeSqlSession (sqlSession , SqlSessionTemplate .this .sqlSessionFactory );
356
351
}
0 commit comments