下面的函数是我在Web应用程序中具有的按钮的actionListener,我正在从数据库中的表中删除旧选择的行,并将新行插入数据库中。
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();
}
}
}
我一直在例外:会话关闭!有人可以解释一下如何在hibernate状态下关闭会话(自动吗?)以及如何使用它们。
这是该异常的详细堆栈跟踪:
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
谢谢,
在Hibernate会话显然配置为基于请求的情况下,您在会话范围内的受管Bean中使用了延迟获取。
长话短说:了解Hibernate中的懒惰获取。
问题内容: 当我按如下方式调用session.begin事务方法时: 然后我得到以下异常消息 造成此错误的原因是什么? 问题答案: 更新: 我想调用并不能保证该会话实际上是打开的。第一次,您应该使用 代替。该建议实际上与您找到的页面一致。 之前: 根据到目前为止的可用信息,我们可以得出结论,错误的原因是会话未打开;-)
问题内容: 一个只有一个数据库的hibernate会话将保持多少个连接? 问题答案: 在给定的时间,给定的会话将仅保留一个连接,您可以使用connect()方法进行访问。 使用reconnect()方法可以更改所使用的连接。
问题内容: 我有以下方法,每隔几秒钟插入一大批记录。运行一段时间后,出现如下错误: 错误:通讯链接失败 从服务器成功接收到的最后一个数据包是523毫秒前。成功发送到服务器的最后一个数据包是8毫秒前。 2013年5月16日,上午9:48:30 com.mchange.v2.c3p0.stmt.GooGooStatementCache checkinStatement INFO:签入语句出现问题,被丢
问题内容: 我在hibernate和延迟加载方面遇到问题。 背景:我有一个Spring MVC Web应用程序,我将Hibernate用于持久层。我正在使用OpenSessionInViewFilter使我能够在视图层中延迟加载实体。我正在扩展HibernateDaoSupport类,并使用HibernateTemplate保存/加载对象。一切都进行得很好。直至现在。 问题:我有一个可以通过Web
问题内容: 我正在使用Struts 2.2.1.1和Hibernate 3.6.2.Final。我还将C3P0用于在Tomcat 7.0.11上运行的连接池。 我遇到的问题是无法关闭我的Hibernate会话,并且我很快超过了“ hibernate.c3p0.max_size”属性中配置的最大打开连接数。 我认为这是因为我的hibernate会话已打开但从未关闭。我正在从存储在ServletCon
问题内容: 要获得答案,请向下滚动到此内容的末尾… 基本问题与多次询问相同。我有一个带有两个POJO事件和用户的简单程序-一个用户可以拥有多个事件。 用户: 注意:这是一个示例项目。我 真的 很想在这里使用Lazy抓取。 现在我们需要配置spring和hibernate,并有一个简单的basic-db.xml用于加载: 注意:我玩过CustomScopeConfigurer和SimpleThrea