-->
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.  [ 9 posts ] 
Author Message
 Post subject: Session is closed
PostPosted: Thu Mar 16, 2006 7:26 am 
Newbie

Joined: Thu Jan 26, 2006 4:26 am
Posts: 16
Hello i have some doubts about if i m using correctly hibernate. I have created the typical HiibernateUtil for managing the Session. My problem resides that im using AOP for controlling transactions, so before calling the method, first i get the session from ThreadLocal of HibernateUtil and i start the transaction, after all operations inside this transaction has been executed, i make a commit and finally a session.close().

What is my surprise that when i try to use in next operation the hibernate, that my Interceptor throws an exception telling me that session is closed. I just don't understand why happens that thing, because the code is the same like has been called first time. For now, always the session is open (like long conversations approach.

Can anybody tell me if i m OK. In fact caveatemptor example used the last thing i have explain it, opening session one time only and always returning the session of threadlocal. My Interceptor is a copy of its filter and my HibernateUtil is like its application.

Im using Hibernate 3.1 with annotations.

Code:
public static Session currentSession() throws HibernateException {
       
        Session s = session.get();
       
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }       
        return s;
    }


Thank you very much if anybody can clarify to me this doubt.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 8:13 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
If I should guess...

Do you remove your session from the threadlocal at the moment you close your session?

If not you will get your closed session back from the ThreadLocal at the next call to getCurrentSession.

Could you give me a bit more infomartion about your code an the aspects you use?

simon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 9:02 am 
Newbie

Joined: Thu Jan 26, 2006 4:26 am
Posts: 16
Yes i put session to null


Code:
public static void closeSession() throws HibernateException {
        Session s = session.get();
        session.set(null);
        if (s != null)
           s.flush();
            s.close();
    }


it is something strange. If you want more information i can tell you that im using junit for testing all hibernate operations, so the first test is executed perfectly, and it is the second test that throws the exception.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 12:18 pm 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
alex.soto wrote:
it is something strange. If you want more information i can tell you that im using junit for testing all hibernate operations, so the first test is executed perfectly, and it is the second test that throws the exception.


Could you post your test case, especially your setup and teardown mehtodes?

simon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 3:06 am 
Newbie

Joined: Thu Jan 26, 2006 4:26 am
Posts: 16
i have got no startup or teardown method,i only have a static block for initializing hibernate

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

and then i used sessionFactory.openSession() for retrieving the session of hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 1:08 pm 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
is there any problem to post your testmethodes?

simon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 20, 2006 3:35 am 
Newbie

Joined: Thu Jan 26, 2006 4:26 am
Posts: 16
I think i know where are the problem. The problem is that DAOs classes are singleton so when i close the session, the same instance of DAO is returned but with colsed session, i would try it, because im not very sure, and i would tell you something.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 20, 2006 4:17 am 
Newbie

Joined: Thu Jan 26, 2006 4:26 am
Posts: 16
The problem persists :(

I would copy the test method.

Code:
public void testCreateAnalyzer(){
      AnalyzerBO analyzerBO = BOFactory.DEFAULT.getAnalyzerBO();
      
      Analyzer analyzer = new Analyzer();
      HostComponent hc = new HostComponent();
      
      hc.setIp4Address("130.206.42.160");
      analyzer.setHostComponent(hc);
      
      analyzer.setMode(ModeAnalyzerEnum.OFF);
      try {
         analyzer = analyzerBO.createAnalyzer(analyzer);
      } catch (DatabaseException e) {
         fail(e.getMessage());
      }
      
      Analyzer analyzer2 = null;
      try {
         analyzer2 = analyzerBO.findAnalyzerById(analyzer);
      } catch (DatabaseException e) {
         fail(e.getMessage());
      }
      
      assertEquals("130.206.42.160",analyzer2.getHostComponent().getIp4Address());
   }


it crashes when try to execute the findAnalyzer.

Now neither BO nor DAO is singleton, but same thing happens. DAO is the generic DAO explained in http://www.hibernate.org/328.html.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 20, 2006 4:25 am 
Newbie

Joined: Thu Jan 26, 2006 4:26 am
Posts: 16
I think i have find a solution, but now when in test i call analyzer2.getHostComponent(), this expcetion is thrown
Quote:
106 [main] ERROR org.hibernate.LazyInitializationException - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed


I can't understand why this happens because i ve always thought that an object can be used outside scope of session.


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