java 如何捕获mybatis异常“org.apache.ibatis.exceptions.PersistenceException”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16755443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:53:45  来源:igfitidea点击:

How to capture the mybatis exception "org.apache.ibatis.exceptions.PersistenceException"?

javaexceptionmybatis

提问by sureone

Here is the code sample, I want to capture the exception throwed by mybatis:

下面是代码示例,我想捕获mybatis抛出的异常:

String resource = "com/sureone/server/db/mybatis-config.xml";
Reader reader = null;
try {
    reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
}
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
sqlSession = factory.openSession(true);
tUserMapper = sqlSession.getMapper(TUserMapper.class);

if(tUserMapper.insert(user)>0){     <===Exception throwed here for duplicate entry problem 
   return verifyLogin(user.getAccount(),user.getPassword());
}
return null;

The exception I want to captured:

我想捕获的异常:

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'userName' for key 'account_UNIQUE'

回答by Michael Laffargue

You can capture the PersistenceExceptionas you would do usually :

您可以像往常PersistenceException一样捕获:

try {
  ...
} catch (PersistenceException pe) {

}

But don't forget that this Exceptionwraps the real one:

但不要忘记,这Exception包装了真正的:

From MyBatis code

来自 MyBatis 代码

} catch (Exception e) {
  throw ExceptionFactory.wrapException("Error committing transaction.  Cause: " + e, e);
}

So if you would like the get a grip on the cause of the PersistenceExceptionyou'll have to use .getCause()method on the PersistenceException

因此,如果您想了解原因,则PersistenceException必须.getCause()PersistenceException

Be aware that MyBatis can also launch its own PersistenceException(TooManyResultException,BindingException...) classes, those won't have a causeExceptionwrapped.

请注意,MyBatis的也可以推出自己的PersistenceExceptionTooManyResultExceptionBindingException...)班,那些不会有造成Exception缠绕。

回答by pmhargis

You can capture the ibatis exception by adding a try/catch block around your statements that invoke myBatis query/insert. For instance, if you use the SqlSessionTemplate and the selectList() method, you can do this:

您可以通过在调用 myBatis 查询/插入的语句周围添加 try/catch 块来捕获 ibatis 异常。例如,如果您使用 SqlSessionTemplate 和 selectList() 方法,您可以这样做:

        try {
            myResults = mySqlSessionTemplate.selectList("getInfoList", parameterMap);
        } catch (final org.apache.ibatis.exceptions.PersistenceException ex) {
            logger.error("Problem accessing database");
            throw ex;
        }

Whether you re-throw the exception or consume and deal with it here is your choice. However, beware of "eating" it and not dealing with the problem, since this will allow calling code to progress without knowing about the underlying data access problem.

是重新抛出异常还是在此处消费并处理它是您的选择。但是,请注意“吃掉”它而不是处理问题,因为这将允许在不了解底层数据访问问题的情况下调用代码。