【发布时间】:2025-12-30 04:25:12
【问题描述】:
我的要求是我想执行数据库操作。 所以,我正在做...
Public boolean myFunction(){
Session session = sessionFactory.getCurrentSession();
if(session!=null){
if (tx != null) {
Transaction tx = session.beginTransaction();
//Perform database operation...
tx.rollback();
if (session.isOpen()) {
session.close();
}
tx = null;
session = null;
}
}else{
return;
}
}
当我的会话不包含任何以前未提交/未回滚的事务时,这很有效。
现在,问题场景来了……
场景:
有一个服务...调用了 myFunction() 并且该服务在会话中已经有一个活动事务。
问题: 当 myfunction() 执行 tx.rollback()... 它也回滚了 parent 的事务。
1.) Why this happen???
2.) Is there nay way to determine... weather hibernate session contains any previous open/uncommited/active/unrolledback/continue transaction?
我试过了……
Public boolean myFunction(){
Session session = sessionFactory.getCurrentSession();
if(session!=null){
if (tx != null) {
boolean isAlreadyTransactionStarted = sessionFactory.getCurrentSession().getTransaction().isActive();
if(isAlreadyTransactionStarted){
Transaction tx = sessionFactory.getCurrentSession().getTransaction();
}else{
Transaction tx = session.beginTransaction();
}
//Perform database operation...
if(isAlreadyTransactionStarted){
tx.rollback();
if (session.isOpen()) {
session.close();
}
tx = null;
session = null;
}else{
//Nothing to do...
}
}
}else{
return;
}
}
但在这种情况下
1.) When parent call contains any active transactions then isAlreadyTransactionStarted becomes true.
2.) But in the case call which does not contains any transaction, also isAlreadyTransactionStarted becomes true.
我的问题还是一样:
有没有办法确定...天气休眠会话包含任何以前的打开/未提交/活动/未回滚/继续事务?
【问题讨论】:
标签: hibernate session transactions struts