-->
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: openSession() or getCurrentSession()?
PostPosted: Thu Mar 01, 2007 11:52 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 11:39 am
Posts: 24
I would like to ask a question about Session. For example in a DAO method I start using session like that:


Code:
session = HibernateUtil.getSessionFactory().openSession();


When I did not use the above and used:

Code:
session = HibernateUtil.getSessionFactory().getCurrentSession()


I had problems.But I guess using the openSession() everytime is not correct right?

My hibernate Util is:

Code:
public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from 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 SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}



I guess I should add a method like getSession() that will open a session
(openSession()) only if getCurrentSession is not null?

Please some suggestions..

...Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 5:52 am 
Newbie

Joined: Fri Nov 03, 2006 5:21 am
Posts: 14
Hi,

I'd use getCurrentSession() as this method opens a new session if no session exists so war but it uses an existing session if there already exists one.

I assume that you only want to have one session in your application.

Another advantage of getCurrentSession() is that it manages it sessions with a transaction manager in a J2EE application. I.e. the session is automatically bound to the transaction and is fushed before the transaction commits. and if a parallel transaction is opened you automatically get a new session.

In case of a standalone application that doesn't use a transaction manager, make sure that you bind your session to the thread using the property
<property name="current_session_context_class">thread</property>
in your hibernate.cfg.xml.

Regards,
Joern


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 6:14 am 
Beginner
Beginner

Joined: Wed Jan 31, 2007 11:39 am
Posts: 24
I am using Hibernate 3.2 with Tomcat. I am having the JTATransactionManager since Tomcat does not have one, and the transaction.manager_lookup_class is the org.hibernate.transaction.JOTMTransactionManagerLookup.

I read that if I use Hibernate 3.x (a more advanced than 3.0) the sessionFactory.getCurrentSession() is all I need for session.
But I have a null pointer exception.The method works only if I use the openSession().

Here is my code in my UserDao:

Code:
session = HibernateUtil.getSessionFactory().getCurrentSession();
Context ctx = new InitialContext();
tx =         (UserTransaction)ctx.lookup("java:comp/env/UserTransaction");          
tx.begin();       
session.get(User.class, new Long(1));
tx.commit();      
session.close()


Is the order of Session and transaction correct?
If I open first the transaction and then get session I have :

Code:
org.hibernate.HibernateException: get is not valid without active transaction



Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 10:01 am 
Newbie

Joined: Fri Nov 03, 2006 5:21 am
Posts: 14
Hi,

have you set the configuration parameter
Code:
<property name="hibernate.current_session_context_class">jta</property>


If yes, you should first start the UserTransaction and afterwards call the getCurrentSession() method.

Hope this helps,
Joern[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 10:02 am 
Newbie

Joined: Fri Nov 03, 2006 5:21 am
Posts: 14
Hi,

have you set the configuration parameter
Code:
<property name="hibernate.current_session_context_class">jta</property>


If yes, you should first start the UserTransaction and afterwards call the getCurrentSession() method.

Hope this helps,
Joern[/quote]


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.