facebook

Hibernate Problem: Session is closed

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #217463 Reply

    symantec
    Member

    Hi all,
    Have beening playing Hibernate features in MyEclipse for 2 days. One problem drove me crazy 🙂

    The applications I build using Hibernate wizard all come to face a same problem. The application can connect and display the database correctly when the applicaton runs the very first time. The second time to run,
    HibernateException: “Session is closed” will be thrown.
    Even the Hibernate tutorial provided also have the same problem. Here is the stacktrace:

    javax.servlet.ServletException: net.sf.hibernate.HibernateException: Session is closed
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    root cause

    java.lang.RuntimeException: net.sf.hibernate.HibernateException: Session is closed
    com.siliconmemory.hibernate.VipService.addVipdata(VipService.java:260)
    com.siliconmemory.action.AddVipdata.execute(AddVipdata.java:52)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    This problem really confuse me, can someone please help me?

    #217474 Reply

    Riyad Kalla
    Member

    Open up your code in this file:

    com.siliconmemory.hibernate.VipService.addVipdata(VipService.java:260)

    You are most likely trying to open a session that is already closed. This error occurs when you loose track of good session bookeeping (e.g. you forget where you open/close sessions and don’t balance them correctly).

    #217904 Reply

    You are most likely trying to open a session that is already closed.

    I had the same problem, and it turned out to be what was quoted above. With the SessionFactory that is created with the Hibernate wizard, in the currentSession() method, I had to add to the if statement as follows:

    if (session == null || session.isOpen() == false)

    This makes sure that the code is run if the session object is closed as well as null. When you run the application for the first time, your session is null, so one is created. When you continue to use the application, then the object is not null since it has already been created, but it is closed. Checking to see if it is open will show that it is not, then you can just open the session on the already created session factory object. I hope this helps.

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Hibernate Problem: Session is closed

You must be logged in to post in the forum log in