为什么没有提交一个会话的多个事务?

时间:2015-02-18 12:38:47

标签: hibernate transactions

我尝试使用hibernate在一个会话中提交多个事务。但由于某种原因,只有第一个提交到数据库:

private final DaoFactory df = new DaoFactory();
df.beginConnectionScope();

Object1Dao o1d = df.createDao(Object1Dao.class);

try{
    df.beginTransaction();
    //update object1
    df.endTransaction();
    if (something){
        df.beginTransaction();
        Object2Dao o2d = df.createDao(Object2Dao.class);
        //update object2
        df.endTransaction();
    } else if (orderItem.isClosed()){
        df.beginTransaction();
        Object2Dao o2d = df.createDao(Object2Dao.class);
        //update object 2
        df.endTransaction();
    }
    success = true;
} catch (Exception ex){
    df.rollback();
} finally {         
    df.endConnectionScope();
} 

我的DaoFactory.beginTransaction:

public void beginTransaction(){
    if (tx == null || !tx.isActive()) {
        tx = session.beginTransaction();
    }
}

我的DaoFactory.endTransaction:

public void endTransaction(){
    tx.commit();
    tx = null;
}

会话由DaoFactory控制,并且仅在调用DaoFactory.endConnectionScope时才会重新发布。

我做错了什么?

0 个答案:

没有答案