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
How to capture the mybatis exception "org.apache.ibatis.exceptions.PersistenceException"?
提问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 PersistenceException
as you would do usually :
您可以像往常PersistenceException
一样捕获:
try {
...
} catch (PersistenceException pe) {
}
But don't forget that this Exception
wraps 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 PersistenceException
you'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 causeException
wrapped.
请注意,MyBatis的也可以推出自己的PersistenceException
(TooManyResultException
,BindingException
...)班,那些不会有造成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.
是重新抛出异常还是在此处消费并处理它是您的选择。但是,请注意“吃掉”它而不是处理问题,因为这将允许在不了解底层数据访问问题的情况下调用代码。