Reputation: 2511
I am trying to catch SQLException
on mybatis but it is not allowing me to use this.
try{
session.insert("insertMyData", insertData);
}
catch (SQLException sqle) { // mybatis not supporting SQLException
//exception handling code
}
so instead I try to use SqlSessionException
try{
session.insert("insertMyData", insertData);
}
catch (SqlSessionException sqle) { // mybatis support this
//exception handling code
}
but the issue using this is is not all SQL exceptions are caught. if I try to insert duplicate data it is not catching SQLIntegrityConstraintViolationException
Is there a way to catch all SQL exception in mybatis.
Upvotes: 1
Views: 5746
Reputation: 3150
Although undocumented, the execution of the SqlSession#insert
or SqlSession#selectList
or any other API methods in case of an error will end up with the PersistenceException
. The source code can be found here.
It is important because the cause
of this exception is going to be driver specific SQLException
that you are looking for:
try {
sqlSession.insert("anything", object);
} catch (PersistenceException e){
Throwable cause = e.getCause(); // Here is the native SQLException from the driver
}
Upvotes: 0