0

Hibernate Problem: Session is closed !

asked 2010-03-31 11:46:24 +0800

yuanbohan gravatar image yuanbohan
258 2 7

updated 2010-03-31 11:47:59 +0800

Hi, I want to know how to solve the problem with :"Session is closed!!" after I try to use the find() method again. I use the hibernateUtil provided by zk to get the current session, and I run the SensorServiceImpl as java application, not on the server. So when I run the main function, I can not use the session for more than 1 time. I don't close the session by myself, but I guess it is closed somewhere I don't know. Anyone helps me with this problem? I just want to test if my method works well, so I run the java class as an application. The following is my codes:

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.zkoss.zkplus.hibernate.HibernateUtil;


public class SensorDAOImpl implements SensorDAO {

Session _session;
public SensorDAOImpl(){
_session = HibernateUtil.getSessionFactory().getCurrentSession();
}

@SuppressWarnings("unchecked")
@Override
public List<Sensor> find() {
Transaction tr = null;
List<Sensor> result = null;
try {
tr = _session.beginTransaction();
result = this._session.createQuery("from Sensor").list();
tr.commit();
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
}
return result;
}

public Session get_session() {
return _session;
}

public void set_session(Session session) {
_session = session;
}

public static void main(String[] args) {
SensorDAOImpl service = new SensorDAOImpl();
List<Sensor> data = service.find();
System.out.println(data.size());


data = service.find();
System.out.println(data.size());
}

}


********************************************the output is:********************************************

Hibernate: select sensor0_.id as id0_, sensor0_.sensorId as sensorId0_, sensor0_.time as time0_, sensor0_.position as position0_ from sensors sensor0_
21
org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1346)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at $Proxy0.beginTransaction(Unknown Source)
at ie.deri.backend.daoImpl.SensorDAOImpl.find(SensorDAOImpl.java:59)
at ie.deri.backend.serviceImpl.SensorServiceImpl.find(SensorServiceImpl.java:45)
at ie.dri.backend.serverpush.DataService.getDataFromSensors(DataService.java:33)
at ie.dri.backend.serverpush.DataService.main(DataService.java:93)
Exception in thread "main" java.lang.NullPointerException
at ie.deri.backend.daoImpl.SensorDAOImpl.find(SensorDAOImpl.java:64)
at ie.deri.backend.serviceImpl.SensorServiceImpl.find(SensorServiceImpl.java:45)
at ie.dri.backend.serverpush.DataService.getDataFromSensors(DataService.java:33)
at ie.dri.backend.serverpush.DataService.main(DataService.java:93)

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2010-03-31 13:56:48 +0800

kal gravatar image kal
21 1 1

This is a ZK forum :)
Anyway, after using tr.commit(), you have to call _session = HibernateUtil.getSessionFactory().getCurrentSession(); again, if you want to begin an other transaction
put this line inside of your find() method before you call session.begintransaction().
I hope it will work, but i can't test it now.

link publish delete flag offensive edit

answered 2010-04-02 06:26:40 +0800

yuanbohan gravatar image yuanbohan
258 2 7

updated 2010-04-02 06:51:40 +0800

Hi kal, Thanks for your help. ^_^
I know it is hibernate concerned, I am sorry for taking your time looking at the codes not related to zk. But there is something related to zk.
I have to say that the result is correct. The data in the database has been successfully presented on the page, but the error appears at the console window. I have added the configuration in the zk.xml, but I don't know if there is no need to use the transaction of hibernate in zk framework ? I don't know why the transaction has not been successfully started. I will mention it again that the data in the database has been successfully presented on the web page.

<listener>
<description>Hibernate Open Session In View life-cycle</description>
<listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class>
</listener>


****************the hibernate codes to get the data from the database is the following (just the same as the above)****************


import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.zkoss.zkplus.hibernate.HibernateUtil;


public class SensorDAOImpl implements SensorDAO {

Session _session;
public SensorDAOImpl(){
_session = HibernateUtil.getSessionFactory().getCurrentSession();
}
@SuppressWarnings("unchecked")
@Override
public List<Sensor> find() {
Transaction tr = null;
_session = HibernateUtil.getSessionFactory().getCurrentSession();
List<Sensor> result = null;
try {
tr = _session.beginTransaction();
result = this._session.createQuery("from Sensor").list();
tr.commit();
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
}
return result;
}
public Session get_session() {
return _session;
}

public void set_session(Session session) {
_session = session;
}
}

The following is the error:


****************************************error information****************************************


INFO: Server startup in 11368 ms
Hibernate: select sensor0_.id as id0_, sensor0_.sensorId as sensorId0_, sensor0_.time as time0_, sensor0_.position as position0_ from sensors sensor0_
02-Apr-2010 12:45:44 org.zkoss.zk.ui.util.Configuration invokeExecutionCleanups:845
SEVERE: Failed to invoke class org.zkoss.zkplus.hibernate.OpenSessionInViewListener
org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:131)
at org.zkoss.zkplus.hibernate.OpenSessionInViewListener.cleanup(OpenSessionInViewListener.java:65)
at org.zkoss.zk.ui.util.Configuration.invokeExecutionCleanups(Configuration.java:842)
at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:449)
at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:299)
at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:229)
at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:165)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)

link publish delete flag offensive edit

answered 2010-04-03 03:33:38 +0800

kal gravatar image kal
21 1 1

Hi yuanbohan,
Check this link (Transaction demarcation with plain JDBC).
Try to use openSession() instead of getCurrentSession(), and flush()+close() on the end.

I don't know why, but calling getcurrentsession again doesn't work for me eighter. I hope, this will work, because I don't have any other idea.
If you find a working method, please let me know. :)

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-03-31 11:46:24 +0800

Seen: 999 times

Last updated: Apr 03 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More