我遇到了一个问题,
当我关闭休眠会话时,httpsession也自动关闭。
示例代码供参考,
TestAction类
public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = null;
String forward = "";
try {
//session validation
session = request.getSession(false);
if (session == null)
throw new InvalidSessionException("Unauthorized Access");
AuditDAO dao = new AuditDAO();
ArrayList list = dao.getAuditTrail(sc);
session.setAttribute("results", list.size());
forward="searchResult";
} catch (InvalidSessionException ise) {
}
return (mapping.findForward(forward));
}
}
AuditDAO类
public class AuditDAO extends HibernateBase{
public ArrayList<AuditVO> getAuditTrail(SearchCriteria sc) {
try {
Query query = getSession().createQuery(
auditDetailMap.get(constructed query));
return query.list();
} catch (Exception ex) {
System.out.println(ex);
} finally {
closeSession();
}
return auditDetailList;
}
}
HibernateBase类
public class HibernateBase {
private Session session1;
public Session getSession() {
try {
if (session1 == null || !session1.isOpen()) {
Context ctx = new InitialContext();
Object obj = ctx.lookup("HibernateSessionFactory");
SessionFactory sessionFactory = (SessionFactory) obj;
setSession(sessionFactory.openSession());
}
} catch (NamingException ex) {
System.out
.println("Failed get HibernateSessionFactory object from Context");
}
return session1;
}
public void setSession(Session session1) {
this.session1 = session1;
}
public void closeSession(){
if(session1!=null && session1.isOpen()){
session1.close();
}
}
}
注意:我没有关闭httpsession。
关闭休眠会话后,我尝试从httpSession获取值,但出现异常..任何人都可以帮忙