-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate Problem: Session is Closed!
PostPosted: Tue Oct 12, 2004 11:17 am 
Newbie

Joined: Mon Oct 11, 2004 8:21 am
Posts: 1
Hibernate version:
Hibernate 2.1
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Mon Oct 11 13:48:22 GMT+10:00 2004 -->
<hibernate-mapping package="com.siliconmemory.hibernate">

<class name="Vipdata" table="vipdata">
<id name="vipId" column="vip_id" type="java.lang.Integer">
<generator class="native"/>
</id>

<property name="vipName" column="vip_name" type="java.lang.String" not-null="true" />
<property name="vipTitle" column="vip_title" type="java.lang.String" not-null="true" />
</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
/**
* getItemList() returns list of all <code>Item</code> objects stored in the database.
*
* @return <code>List</code> of <code>Item</code> objects.
*/
public List getVipdataList()
{
/*
* Use the ConnectionFactory to retrieve an open
* Hibernate Session.
*
*/
Session session = null;

try
{
session = SessionFactory.currentSession();
/*
* Build HQL (Hibernate Query Language) query to retrieve a list
* of all the items currently stored by Hibernate.
*/
Query query =
session.createQuery(
"select Vipdata from com.siliconmemory.hibernate.Vipdata Vipdata order by Vipdata.vipName");
return query.list();

}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
/*
* Regardless of whether the above processing resulted in an Exception
* or proceeded normally, we want to close the Hibernate session. When
* closing the session, we must allow for the possibility of a Hibernate
* Exception.
*
*/

finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}

}
}

}
Full stack trace of any exception that occurs:
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)

Name and version of the database you are using:
MySql

The applications I build using Hibernate 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.


Top
 Profile  
 
 Post subject: You are looking in the wrong method
PostPosted: Tue Oct 12, 2004 1:08 pm 
Newbie

Joined: Fri Oct 08, 2004 11:59 am
Posts: 16
Location: Helsinki, Finland
Method "public List getVipdataList() ", which you posted, is not the method where the error happens.
That method does not show in the stack trace you posted at all.

Look in method
"com.siliconmemory.hibernate.VipService.addVipdata(VipService.java:260)" instead.

The error probably happens because you call Session.connection() on a session which is already closed.

t: Ville Peurala


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 9:48 am 
Newbie

Joined: Mon Oct 18, 2004 11:27 am
Posts: 4
I got the same problem. I see the topicstarter is using the hibernate tutorial provided on the myeclipseide.com website.
So am I.
Has anyone had a solution for this yet?


Top
 Profile  
 
 Post subject: Solution
PostPosted: Wed Sep 07, 2005 5:11 am 
Newbie

Joined: Wed Sep 07, 2005 4:54 am
Posts: 4
RUFFIE wrote:
I got the same problem. I see the topicstarter is using the hibernate tutorial provided on the myeclipseide.com website.
So am I.
Has anyone had a solution for this yet?


You should adapt the SessionFactory (seen at laliluna)

Code:
  public static Session currentSession() throws HibernateException {
      Session session = (Session) threadLocal.get();
      if (session != null && !session.isOpen())
         session = null;
      if (session == null) {
         if (sessionFactory == null) {
            try {
               cfg.configure(CONFIG_FILE_LOCATION);
               sessionFactory = cfg.buildSessionFactory();
            } catch (Exception e) {
               System.err
                     .println("%%%% Error Creating HibernateSessionFactory %%%%");
               e.printStackTrace();
            }
         }
         session = sessionFactory.openSession();
         threadLocal.set(session);
      }
      return session;

   }


then it should work without beeing closed all the time

greetings Martin


Top
 Profile  
 
 Post subject: Re: Hibernate Problem: Session is Closed!
PostPosted: Wed Sep 07, 2005 6:51 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
symantec wrote:

finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}

}
}

}

Do close session if you need it to be open.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.