Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate web项目仅关闭筛选器和服务中的会话,dao不关闭会话_Hibernate - Fatal编程技术网

Hibernate web项目仅关闭筛选器和服务中的会话,dao不关闭会话

Hibernate web项目仅关闭筛选器和服务中的会话,dao不关闭会话,hibernate,Hibernate,现在我在一个遗留的web项目上工作,使用SpringMVC(Spring4.0.5)和hibernate(3.3.2GA),我发现一些服务和dao最终显式关闭了会话,例如 public City getCityByCityId(Integer cityId) { Session s = HibernateSessionFactory.getSession(); City city = cityDAO.getById(City.class, cityId, s); s.cl

现在我在一个遗留的web项目上工作,使用SpringMVC(Spring4.0.5)和hibernate(3.3.2GA),我发现一些服务和dao最终显式关闭了会话,例如

public City getCityByCityId(Integer cityId) {
    Session s = HibernateSessionFactory.getSession();
    City city = cityDAO.getById(City.class, cityId, s);
    s.close(); // after getting the result explicitly close the session
    return city;
}
但其他人不这样做,会话放在threadLocal中

public static Session getSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    if (session == null || !session.isOpen()) {
        // build session
         threadLocal.set(session);
    }
    return session;
}
另外,我在一个过滤器中发现,在调用
chain.doFilter(…)
之后,仍然调用close session globaly,例如

try {
    chain.doFilter(request, response);
} catch (Exception e) {
    //......
}
try{
    //avoid forgettig to close session
    HibernateSessionFactory.closeSession();
}catch(Exception e){
    log.error("close session in filter failed",e);
}
现在我遇到了以下问题,即当我在事务中调用查询方法(显式关闭会话)时,例如

Session s = HibernateSessionFactory.getSession();
Transaction beginTransaction = s.beginTransaction();
try {
    usOrderDAO.save(order, s);
    City city = cityService.getCityByCityId(order.getCity()); // in this query it will close session
    addOrderBusinessDistricts(order, city, s);
    //...
    beginTransaction.commit();
} catch (Exception e) {
    //...
} finally {
    s.close();
}
它将抛出异常:

org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72)
at org.hibernate.impl.SessionImpl.createSQLQuery(SessionImpl.java:1655)
原因是在执行提交之前关闭了会话


现在为了解决上面的问题,我想删除所有那些
s.close()服务中的代码和dao,无论如何,在过滤器中它将关闭会话。但我不确定是否会导致一些潜在的错误。如何评估风险

试试看……如果它先工作,但它有bean在生产环境中运行,我不敢,哈,那么你打算在生产环境中尝试热修复吗?祝你好运