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.  [ 13 posts ] 
Author Message
 Post subject: Exception session was already closed
PostPosted: Mon Jul 03, 2006 3:44 am 
Newbie

Joined: Thu May 11, 2006 5:35 am
Posts: 15
Location: clermont ferrand - France
I've got many problems when I try to use Hibernate 3.1 in a J2EE web application.

To solve my problem I wrote a little test on my persistance and when I launch it, this exception is returned :
Code:
Exception in thread "main" org.hibernate.SessionException: Session was already closed
   at org.hibernate.impl.SessionImpl.close(SessionImpl.java:270)
   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:301)
   at $Proxy0.close(Unknown Source)
   at persistance.TestPersist.main(TestPersist.java:46)



There is my simple test :
Code:
public static void main(String[] args) {
      Authentification auth = new Authentification();
      
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      Transaction tx1 = session.beginTransaction();
      
      User us = new User();
      us.setNom("Martin");
      us.setPrenom("Philippe");
      
      us.setAdresse("18, rue des cerisiers");
      us.setCp(63000);
      us.setVille("Clermont Ferrand");
      us.setTel("0473658941");
      us.setMailPerso("lekaille@gmail.com");
      
      auth.setLogin("toto");
      auth.setPass("tintin");
      
      session.persist(auth);
      
      us.setAuthentification(auth);
      
      session.persist(us);
      tx1.commit();
      session.close();
      
   }


My Hibernate configuration :
Code:
hibernate-configuration>
    <session-factory >

      <!-- local connection properties -->
      <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/transfert</property>
      <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
      <property name="hibernate.connection.username">postgres</property>
      <property name="hibernate.connection.password">******</property>
      <!-- property name="hibernate.connection.pool_size"></property -->

      <!-- dialect for PostgreSQL -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
      <property name="hibernate.jdbc.batch_size">0</property>
      
      <property name="hibernate.sql_show">true</property>
      
      <!-- Enable Hibernate's automatic session context management -->
      <property name="current_session_context_class">thread</property>
      
      <mapping resource="persistance/personne.hbm.xml"></mapping>
      <mapping resource="persistance/fichier.hbm.xml"></mapping>
      <mapping resource="persistance/authentification.hbm.xml"></mapping>
      <mapping resource="persistance/mail.hbm.xml"></mapping>


I don't understand why it doesn't work correctly ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 5:15 am 
Beginner
Beginner

Joined: Thu Apr 07, 2005 5:12 pm
Posts: 27
Location: Hamburg
Hi,

Don't exacty know the HibernateUtil class. But why don't you use the SessionFactory to get a new Session?
And then try
Code:
session.save(user);
instead of persist.
Should work then.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 5:30 am 
Newbie

Joined: Thu May 11, 2006 5:35 am
Posts: 15
Location: clermont ferrand - France
I use the session return by the sessionFactory :
Quote:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();


and when I try with session.save() instead of session.persist() I've got the same exception ...

My HibernateUtil :
Code:
public class HibernateUtil {

    public static final SessionFactory sessionFactory;
       static {
           try {
               // Création de la SessionFactory à partir de hibernate.cfg.xml
               sessionFactory = new Configuration().configure().buildSessionFactory();
           } catch (Throwable ex) {
               // Make sure you log the exception, as it might be swallowed
               System.err.println("Initial SessionFactory creation failed." + ex);
               throw new ExceptionInInitializerError(ex);
           }
       }

       public static final ThreadLocal session = new ThreadLocal();

       public static SessionFactory getSessionFactory() {
           return sessionFactory;
       }
    }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 5:49 am 
Beginner
Beginner

Joined: Thu Apr 07, 2005 5:12 pm
Posts: 27
Location: Hamburg
Sorry, I meant SessionFactory.openSession(), not getCurrentSession().


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 6:08 am 
Newbie

Joined: Thu May 11, 2006 5:35 am
Posts: 15
Location: clermont ferrand - France
I use getCurrentSession because I've placed in my configuration file the option
Quote:
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>


In fact my application is a J2EE application, so I've got a thread per request and this configuration permit to get a session distinct per thread.

I tried with openSession(), in y little test and in my application -> it's work

But I need use the getCurrentSession() to have a multithread application.

Do you see where is the problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 7:32 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Have you tried using the original HibernateUtil with session per thread management inside ?

Maybe there's a problem with the session per thread management inside your appserver...

Moreover, if you want to use it's per-thread isolation, then clear your code : in the HibernateUtil class you show, the ThreadLocal is not used, isn't it ?

I'd do that :
1) Try with a personal isolation, using the HibernateUtil class given in the reference doc.
2) Verify that the reference you're receiving by getCurrentSession are not always the same in the same exec context. I'd display the reference of the returned session to see if it is different references for each thread...

Let's know what it gives.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 8:34 am 
Newbie

Joined: Thu May 11, 2006 5:35 am
Posts: 15
Location: clermont ferrand - France
The HibernateUtil that I'm using is the one of the documentation.

When I look at my Session variable after intanciation with HibernateUtil.getSessionFactory().getCurrentSession() I see that :
[URL=http://www.imagup.com]Image

I don't really unterstand the details of this variable, but I see that the operation occur correctly, and the exception is on the s.close() method.

Code:
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
      Transaction tx = s.beginTransaction();
      
      User us = (User) s.get(User.class,idUser);
      
      tx.commit();
      s.close();
      return us;   
[/img]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 9:53 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Sorry, but I can't see the image you seem to trying to show. Can't you just copy the produced code instead of images ?

Just do that to see the reference :
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
System.out.println("Reference to the session ="+session);


Call this code more than once from the same execution context (without restarting anything) : if the displayed value is the same if two different threads, then try why I said (manage the thread isolation yourself first, before trying the automatic one).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 10:27 am 
Newbie

Joined: Thu May 11, 2006 5:35 am
Posts: 15
Location: clermont ferrand - France
If I remove the session.close() at the last of an acces, it's work fine, it is normal ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 11:14 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
K-Kaï wrote:
If I remove the session.close() at the last of an acces, it's work fine, it is normal ?


Maybe it's because the session_per_thread management is not well done. So when you close a session, actually you close the session shared by every threads using it. So the next one calling close() gets the exception.

Maybe that's why when you don't call it anymore, you don't get the error. Nobody tries to close it, so nobody could be punished for trying to do so :-).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 04, 2006 3:47 am 
Newbie

Joined: Thu May 11, 2006 5:35 am
Posts: 15
Location: clermont ferrand - France
I've got this exception even the first time I do the s.close in a single thread environment.

What can I do to solve my problem ?

I really don't understand where is the problem, I've got the good line in the configuration file :
Code:
<!-- Enable Hibernate's automatic session context management -->
      <property name="current_session_context_class">thread</property>


I use the HibernateUtil available with the Hibernate documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 27, 2008 2:33 am 
Newbie

Joined: Fri Jun 27, 2008 1:41 am
Posts: 1
The Session that you obtained with HibernateUtil.getSessionFactory().getCurrentSession() is flushed and closed automatically.

If you decide to use HibernateUtil.getSessionFactory().openSession() and manage the Session yourself, you have to flush() and close() it.

ref : http://www.hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 27, 2008 9:59 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Life will be much easier if you use getSession. The Session will be bound to the current thread, and you won't need to manage it.

You should also investigate using longer sessions, or perhaps even the "open session in view" design pattern to ensure your Hiberante Session is available when needed. A servlet filter is a fine way to implement this type of design.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.