在抛出约束违反异常后,是否有任何方法可以继续使用线程绑定的休眠会话?我在这里举一个简短的例子:
Parent other=service.load(33); // loads a new parent
try {
Parent p=new Parent();
p.setName("A name");
service.save(p); // a @Transactional spring service class, throws ConstraintViolationException - name should be at least 15 characters long
} catch (ConstraintViolationException e){
// i would like to handle validation errors and proceed normally
// but the session is allready closed here
}
System.out.println("Children: " + other.getChildren()); // lazy initialization exception, even when using opensessioninview
从现在开始,休眠会话完全无用,即使对于只读操作(例如使用 OpenSessionInView 模式在视图中渲染惰性集合)也是如此。
Session的文档指出如果会话抛出异常,则必须回滚事务并丢弃会话。异常发生后Session的内部状态可能与数据库不一致。.
AFAIK,没有办法从中恢复,我记得工作中有人警告我不要使用 session-per-request/OpenSessionInView-pattern 因为这类问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)