5

我的要求是我想执行数据库操作。所以,我在做...

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()... 它也回滚了父事务。

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.

我的问题还是一样:

有没有办法确定......天气休眠会话包含任何以前的打开/未提交/活动/未回滚/继续事务?

4

1 回答 1

1

你可以使用 Session.isDirty() 吗?

于 2013-07-17T10:18:37.897 回答