-->
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: Session is closed!
PostPosted: Wed Oct 20, 2010 3:31 am 
Newbie

Joined: Wed Oct 20, 2010 3:18 am
Posts: 3
Hi everyone

I'm using hibernate with eclipse helios to extract data from a MySQL database. I have done several querys with the same format, but when I do a specific query Hibernate throws a Hibernate exception and with System.out.println(e.getMessage()); I get this: Session is closed!

I have checked before doing the query if the session is open (System.out.println(HibernateUtil.getSessionFactory().getCurrentSession().isOpen());) and it seems to be ok... The exception is thrown when i do the query. This is the first time in the code i'm opening a session btw.

here is the code:

Quote:
{
Transaction tx=null;
try{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
tx=session.beginTransaction();
System.out.println(HibernateUtil.getSessionFactory().getCurrentSession().isOpen());
Query q = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("SELECT r.name, count(log.time) as numeroClicks FROM MdlResource r ,MdlCourseModules cm ,MdlModules modu, MdlLog log WHERE modu.name = 'resource' AND cm.instance=r.id AND modu.id=cm.module AND cm.id=log.cmid AND cmid!=0 GROUP BY r.name ORDER BY r.name");
if(q.list().size()!=0){
tx.commit();
return q.list();
}
else{
tx.commit();
return null;
}
}catch(HibernateException e){
System.out.println(e.getMessage());
e.getStackTrace();
return null;
}


Thanks for helping :D

_________________
Winter is coming...


Top
 Profile  
 
 Post subject: Re: Session is closed!
PostPosted: Wed Oct 20, 2010 12:09 pm 
Newbie

Joined: Wed Oct 20, 2010 12:06 pm
Posts: 3
Harchet,

Please post the Stacktrace for the exception.


Top
 Profile  
 
 Post subject: Re: Session is closed!
PostPosted: Thu Oct 21, 2010 2:40 am 
Newbie

Joined: Wed Oct 20, 2010 3:18 am
Posts: 3
There is no stacktrace... that's the problem. Only prints "session is closed!" because I have add "System.out.println(e.getMessage());"
This function returns a null value, without a stacktrace...

_________________
Winter is coming...


Top
 Profile  
 
 Post subject: Re: Session is closed!
PostPosted: Thu Oct 21, 2010 4:05 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
if(q.list().size()!=0){
tx.commit();
return q.list();
}


I suspect this is the problematic part... First you are calling q.list().size() ...then tx.commit() ... then q.list() again. I suspect that the session get's closed when you call commit and that the exception happens on the second call to g.list().

Quote:
There is no stacktrace...


Yes, there is a stacktrace... call e.printStackTrace() in your exception handler and you will see it.


Top
 Profile  
 
 Post subject: Re: Session is closed!
PostPosted: Fri Oct 22, 2010 2:42 am 
Newbie

Joined: Wed Oct 20, 2010 3:18 am
Posts: 3
in the catch section i have added "e.printStackTrace()", you can check it, and there was no stacktrace, but doesn't matter thanks for helping me! :D

_________________
Winter is coming...


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.