休眠会话关闭的问题

休眠会话关闭的问题

本文介绍了休眠会话关闭的问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的函数是我的web应用程序中的按钮的actionListener,我从数据库中的表中删除旧的选定行并将新的行插入到数据库中。

  public void getSelectedExemptionItems(ActionEvent ae){
Session hibernateSession;
Session hibernate2Session;
selectedExemptions = new ArrayList< ExemptionBean>();
for(ExemptionBean eBean:exemBean){//迭代ExemptionBean类的列表
if(selectedExemptionIDs.get(eBean.getExemptionID())。booleanValue()){
selectedExemptions.add (eBean); //添加所选的每个ExemptionBean。

$ b //获取importane会话变量
SessionBean1 theSessionBean =(SessionBean1)FacesContext.getCurrentInstance()。getExternalContext()。getSessionMap()。get(SessionBean1) ;
JAdmApplication admApplication = theSessionBean.getAdmApplication();
long admAppID = admApplication.getJAdmApplicationId();
// ArrayList< JAdmAppExemption> admAppExemption =(ArrayList< JAdmAppExemption>)theSessionBean.getAdmAppExemption();

JAdmAppExemption admAppExem = new JAdmAppExemption();

//尝试catch:通过登录用户从数据库中删除以前选择的所有免除项目。
try {
hibernateSession = HibernateUtil.getAdmSessionFactory()。getCurrentSession();
Transaction t = hibernateSession.beginTransaction();
t.begin();
String sequel =从JAdmAppExemption删除WHERE createdby =?;
Query q = hibernateSession.createQuery(sequel);
q.setParameter(0,theSessionBean.getPortalUserId());

} catch(Exception e){
System.out.println(Row不能从数据库中退出!!);


//将所有选定的豁免添加到数据库

用于(ExemptionBean e:selectedExemptions){
admAppExem.setAdClientId(1000001);
admAppExem.setAdOrgId(1000001);
admAppExem.setCreated(getTimestamp());
admAppExem.setCreatedby(theSessionBean.getPortalUserId());
admAppExem.setIsactive('Y');
admAppExem.setJAdmAppExemptionId(getJAdmAppExemptionIdSequence());
admAppExem.setJAdmApplicationId(admAppID);
admAppExem.setJAdmExemptionId(e.getExemptionID());
admAppExem.setUpdated(getTimestamp());
admAppExem.setUpdatedby(theSessionBean.getPortalUserId());

hibernate2Session = HibernateUtil.getAdmSessionFactory()。getCurrentSession(); //会话在这里打开!
Transaction tx = hibernate2Session.beginTransaction();
tx.begin();
尝试{
hibernate2Session.beginTransaction();
hibernate2Session.save(admAppExem); //是保存还是保存或更新?
hibernate2Session.getTransaction()。commit();
updateJAdmAppExemptionIdSequence();
$ b $ catch(Throwable ex){
if(hibernate2Session.getTransaction()。isActive()){
hibernate2Session.getTransaction()。rollback();
}
ex.printStackTrace();
}
}
}

我总是收到一个异常:会议闭幕!有人可以解释一下session是如何在hibernate中自动关闭的(自动??)以及如何使用它们。



这里是异常的详细堆栈跟踪: p>

  javax.faces.FacesException:org.hibernate.SessionException:Session关闭! 
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:90)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at com.icesoft.faces.webapp.http.core.JsfLifecycleExecutor.apply(JsfLifecycleExecutor.java:18)
在com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.renderCycle(ReceiveSendUpdates.java:132)
at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.service(ReceiveSendUpdates.java:74)
at com.icesoft.faces.webapp.http.core.RequestVerifier.service(RequestVerifier.java:31)
at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service( PathDispatcherServer.java:24)
at com.icesoft.faces.webapp.http.servlet.BasicAdaptingServlet.service(BasicAdaptingServlet.java:16)
at com.icesoft.faces.webapp.http.servlet。 PathDispatcher.service(PathD ispatcher.java:23)
at com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:53)
at com.icesoft.faces.webapp.http.servlet。 SessionVerifier.service(SessionVerifier.java:26)
at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
at com.icesoft.faces.webapp。 http.servlet.MainServlet.service(MainServlet.java:131)
位于javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
位于com.icesoft.faces.webapp.xmlhttp。 BlockingServlet.service(BlockingServlet.java:56)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter( ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardCo
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java: 102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
at org.apache.coyote.http11.Http11AprProtocol $ Http11ConnectionHandler.process(Http11AprProtocol.java:565)
在org.apache.tomcat.util.net.AprEndpoint $ Worker.run(AprEndpoint.java:1509)$ b $在java.lang.Thread.run(Thread.java:619)
导致:javax .faces.el.E​​valuationException:org.hibernate.SessionException:Session关闭!
at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:380)
at com .icesoft.faces.component.panelseries.UISeries $ RowEvent.broadcast(UISeries.java:617)
at com.icesoft.faces.component.panelseries.UISeries.broadcast(UISeries.java:285)
在com.icesoft.faces.component.paneltabset.PanelTabSet.broadcast(PanelTabSet.java:320)
在javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
在javax.faces .component.UIViewRoot.processApplication(UIViewRoot.java:756)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
... 27 more
引起通过:org.hibernate.SessionException:会话关闭!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1319)
at sun.reflect .GeneratedMethodAccessor2480.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $ Proxy233.beginTransaction(Unknown Source)
at univportal.AdmissionApplication.Application.getSelectedExemptionItems(Application.java :1595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.ja va:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:172)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 34更多

谢谢,

解决方案



长篇小说:


The function below is the actionListener for a button I have in my web application, I am deleting the old chosen rows from a table in a database and inserting the new ones to the database.

public void getSelectedExemptionItems(ActionEvent ae) {
        Session hibernateSession;
        Session hibernate2Session;
        selectedExemptions = new ArrayList<ExemptionBean>();
        for (ExemptionBean eBean : exemBean) { // iterating over the list of ExemptionBean class
            if (selectedExemptionIDs.get(eBean.getExemptionID()).booleanValue()) {
                selectedExemptions.add(eBean); // adding every ExemptionBean that is selected.
            }
        }
        // getting importane session variables
        SessionBean1 theSessionBean = (SessionBean1) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("SessionBean1");
        JAdmApplication admApplication = theSessionBean.getAdmApplication();
        long admAppID = admApplication.getJAdmApplicationId();
//        ArrayList<JAdmAppExemption> admAppExemption = (ArrayList<JAdmAppExemption>)theSessionBean.getAdmAppExemption();

        JAdmAppExemption admAppExem = new JAdmAppExemption();

        // try catch : delete all previously chosen exemptions from database by logged-in user.
        try {
            hibernateSession = HibernateUtil.getAdmSessionFactory().getCurrentSession();
            Transaction t = hibernateSession.beginTransaction();
            t.begin();
            String sequel = "DELETE FROM JAdmAppExemption WHERE createdby = ?";
            Query q = hibernateSession.createQuery(sequel);
            q.setParameter(0, theSessionBean.getPortalUserId());

        } catch (Exception e){
            System.out.println("Row could not be deledted from database!!");
        }

//         adding all selected exemptions to the database

        for (ExemptionBean e : selectedExemptions) {
            admAppExem.setAdClientId(1000001);
            admAppExem.setAdOrgId(1000001);
            admAppExem.setCreated(getTimestamp());
            admAppExem.setCreatedby(theSessionBean.getPortalUserId());
            admAppExem.setIsactive('Y');
            admAppExem.setJAdmAppExemptionId(getJAdmAppExemptionIdSequence());
            admAppExem.setJAdmApplicationId(admAppID);
            admAppExem.setJAdmExemptionId(e.getExemptionID());
            admAppExem.setUpdated(getTimestamp());
            admAppExem.setUpdatedby(theSessionBean.getPortalUserId());

            hibernate2Session = HibernateUtil.getAdmSessionFactory().getCurrentSession(); // session is opened here!
            Transaction tx = hibernate2Session.beginTransaction();
            tx.begin();
            try {
                hibernate2Session.beginTransaction();
                hibernate2Session.save(admAppExem); // is it save or saveOrUpdate??
                hibernate2Session.getTransaction().commit();
                updateJAdmAppExemptionIdSequence();

            } catch (Throwable ex){
                if (hibernate2Session.getTransaction().isActive()) {
                    hibernate2Session.getTransaction().rollback();
                }
                ex.printStackTrace();
            }
        }
    }

I keep getting an exception : Session is closed !! can someone please explain how sessions are closed in hibernate (automatically??) and how to work with them..

here is the detailed stack trace of the exception:

javax.faces.FacesException: org.hibernate.SessionException: Session is closed!
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:90)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
        at com.icesoft.faces.webapp.http.core.JsfLifecycleExecutor.apply(JsfLifecycleExecutor.java:18)
        at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.renderCycle(ReceiveSendUpdates.java:132)
        at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.service(ReceiveSendUpdates.java:74)
        at com.icesoft.faces.webapp.http.core.RequestVerifier.service(RequestVerifier.java:31)
        at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service(PathDispatcherServer.java:24)
        at com.icesoft.faces.webapp.http.servlet.BasicAdaptingServlet.service(BasicAdaptingServlet.java:16)
        at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
        at com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:53)
        at com.icesoft.faces.webapp.http.servlet.SessionVerifier.service(SessionVerifier.java:26)
        at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
        at com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:131)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at com.icesoft.faces.webapp.xmlhttp.BlockingServlet.service(BlockingServlet.java:56)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
        at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)
        at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
        at java.lang.Thread.run(Thread.java:619)
Caused by: javax.faces.el.EvaluationException: org.hibernate.SessionException: Session is closed!
        at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
        at javax.faces.component.UICommand.broadcast(UICommand.java:380)
        at com.icesoft.faces.component.panelseries.UISeries$RowEvent.broadcast(UISeries.java:617)
        at com.icesoft.faces.component.panelseries.UISeries.broadcast(UISeries.java:285)
        at com.icesoft.faces.component.paneltabset.PanelTabSet.broadcast(PanelTabSet.java:320)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
        ... 27 more
Caused by: org.hibernate.SessionException: Session is closed!
        at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
        at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1319)
        at sun.reflect.GeneratedMethodAccessor2480.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
        at $Proxy233.beginTransaction(Unknown Source)
        at univportal.AdmissionApplication.Application.getSelectedExemptionItems(Application.java:1595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.el.parser.AstValue.invoke(AstValue.java:172)
        at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
        at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
        ... 34 more

Thanks,

解决方案

You're using lazy fetching in a session scoped managed bean while the Hibernate session is apparently configured to be request based.

Long story short: Understanding Lazy Fetching in Hibernate.

这篇关于休眠会话关闭的问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:43