java – 迁移表单Hibernate3到Hibernate4不断支持嵌套事务
作者:互联网
在我们将应用程序从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();
}
}
道层
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;
}
}
我在堆栈轨道中看到的异常是
我的db.xml
解决方法:
有两种可能性:
在会话关闭时打开事务的实现定义行为:
确保在关闭连接之前提交(或回滚,对于只读而言无关紧要)事务. 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)
或者,如果您没有通过交易:
if (session.getTransaction() == null || !session.getTransaction().isActive())
session.beginTransaction();
// then use session's current transaction
但您仍可能需要检查代码以查看在给定连接上打开事务的其他位置.
标签:java,hibernate,java-ee,hibernate3,hibernate-4-x 来源: https://codeday.me/bug/20190703/1367504.html