从Hibernate 3迁移到Hibernate 4不断支持嵌套事务

时间:2013-10-24 05:18:38

标签: java hibernate java-ee hibernate-4.x hibernate3

在我们将应用程序从hibernate3迁移到hibernate4之前一周,hibernate3中的所有内容都运行正常,但是在迁移到hibernate4之后。我不断支持嵌套交易

以下是我的服务层,它正在调用dao

public class LeaveApplicationService implementsSerializable,LeaveApplicationInterfaceService{
    @Autowired
    private LeaveApplicationInterfaceDao _leavApplicationInterfaceDao;
       //getter setter
    @Override
    public synchronized void clickOnAddLeaveButton(LeaveApplicationViewBean leaveApplicationViewBean) {
    SessionFactory sessionFactory=(SessionFactory) ObjectFactory.getBean("sessionFactory");
    sessionFactory.getCurrentSession().beginTransaction();


        try{
                leaveApplicationViewBean.get_employeeListObj().clear();
                leaveApplicationViewBean.get_leaveTypelist().clear();
                leaveApplicationViewBean.get_leaveApproveers().clear();
                //leaveApplicationViewBean.set_employeeListObj(get_leavApplicationInterfaceDao().getEmployeeList());
                leaveApplicationViewBean.set_leaveTypelist(get_leavApplicationInterfaceDao().getLeaveType());
                leaveApplicationViewBean.set_leaveApproveers(get_leavApplicationInterfaceDao().getLeaveApprover(CmsUtil.getSession("userId").toString()));

        }catch(Exception e){
            CmsLogger.errorLog(LeaveApplicationService.class, e);
        }finally{
            sessionFactory.getCurrentSession().close();
        }
}

Dao层

public class LeaveApplicationDao extends TransactionService implements Serializable,LeaveApplicationInterfaceDao{
    private static final long serialVersionUID = 6237725881698448330L;

    public List<LeaveApprover> getLeaveApprover(String userId) throws Exception {
      List<LeaveApprover> _leavApprovers=new ArrayList<LeaveApprover>();
      Iterator it=getSessionFactory().getCurrentSession().createQuery(sql.toString()).setParameter("practiceAreaId",CmsUtil.getSession("practiceAreaId").toString())                                            .setParameter("userId",userId).setCacheable(true)
                                                                                    .list().iterator();
      while(it.hasNext()){
        Object[] obj=(Object[]) it.next();
        LeaveApprover leaveApprover=new LeaveApprover();
        leaveApprover.set_approverId((String) obj[0]);
        leaveApprover.set_approverName((String) obj[1]);
        _leavApprovers.add(leaveApprover);
    }

    return _leavApprovers;
}



public List<TimeProjectCategory> getLeaveType() throws Exception{
    List<TimeProjectCategory> timeProjectCategories=new ArrayList<TimeProjectCategory>();
    Iterator it =getSessionFactory().getCurrentSession().createQuery(sql.toString()).setCacheable(true).list().iterator();
        while(it.hasNext()){
            Object[] obj=(Object[]) it.next();
            TimeProjectCategory category=(TimeProjectCategory) ObjectFactory.getBean("domainTimeProjectCategoryObj");
            category.getProjectId().set_projectId((String) obj[0]);
            category.setTimeCategory((String) obj[1]);
            category.setTimeProjectCategoryId((String) obj[2]);
            timeProjectCategories.add(category);
        }


    return timeProjectCategories;

}

}

我的TransactionService类

public class TransactionService{
/**
 * 
 */
private static final long serialVersionUID = 3747504246445270484L;

@Autowired
private DataSource dataSource;

@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
    return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}


public DataSource getDataSource() {
    return dataSource;
}
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}


}

我在堆栈轨道中看到的异常是

Stack Trace for the code

我的db.xml

db.xml file

1 个答案:

答案 0 :(得分:2)

有两种可能性:

在会话关闭时打开事务的实现定义行为:

确保在关闭连接之前提交(或回滚,对于只读而言无关紧要)事务。 Hibernate Session.close()关闭底层连接;和http://docs.oracle.com/javase/6/docs/api/java/sql/Connection.html#close%28%29表明事务处于打开状态时的行为是实现定义的(即某些实现将自动关闭事务,其他实现可以引用计算连接并保持打开)。

一般模式是:

try {
    Transaction t = sessionFactory.getCurrentSession().beginTransaction();
    try {
        // do work
        t.commit();
    } catch (Exception e) { 
        t.rollback();
        throw e;
    }
} finally {
    sessionFactory.getCurrentSession().close();
}

之前的交易仍然有效:

如果先前的交易仍在其他地方处于活动状态,您可以执行以下操作:

Transaction t = session.getTransaction();
if (t == null || !t.isActive()) 
    t = session.beginTransaction();

// then use t (or session's current transaction, same thing)

或者,如果你没有传递Transaction

if (session.getTransaction() == null || !session.getTransaction().isActive())
    session.beginTransaction();

// then use session's current transaction

但您仍可能需要检查代码以查看在给定连接上打开事务的其他位置。