-->
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 vs getCurrentSession
PostPosted: Wed Jan 11, 2006 5:44 am 
Newbie

Joined: Wed Jan 11, 2006 5:21 am
Posts: 14
The following is basic structure of my transactions using hibernate.
Code:
public void createUser(User user)
    throws HibernateException, LogConfigurationException
    {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Log log = LogFactory.getLog(Log4JLogger.class);
        try
        {
            log.info("starting transaction");
            session.beginTransaction();
           
            log.info("saving user");
            log.debug(user.toString());
            session.save(user);
            log.info("committing transaction");
            session.getTransaction().commit();
        }
        catch(Exception e)
        {
            log.info("rolling back transaction");
            session.getTransaction().rollback();           
            log.error(e.getMessage(), e);
            throw new HibernateException(e);
        }
    }

If this is run in a web application environment, will getCurrentSession() always create new session? Or will it use an existing session which other users are utilizing?

Is it safe to use openSession instead of getCurrentSession() method of the SessionFactory? I'll appreciate any reply.

Kind regards,
Roy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 6:02 am 
Newbie

Joined: Tue Jan 10, 2006 9:22 am
Posts: 6
This works like this in a connection:
The getCurrentSession, will get the current session in the server (I don't know exactly if it is the same session of other user),probably used by the same user and don't closed until the moment that you call it.
And the openSession will get an empty (closed) connection, and will open it for you to use. Because when you create a sessionfactory, you are openning more than one connection to your database

Is HibernateUtil a class that create a SessionFactory?

_________________
Best regards
Dirceu Semighini Filho


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 6:15 am 
Newbie

Joined: Wed Jan 11, 2006 5:21 am
Posts: 14
Thanks for your comments. Yes HibernateUtil is the one used to get reference to a singleton SessionFactory. I'd like to know if the session object returned by getCurrentSession() is always new or not. If not, my question is will it affect the commit and/or rollback calls that I make in a multi-user environement like web application? If it does, then I think I have to use the following code pattern to ensure that every user's transaction is protected.

Code:
    public void createUser(User user)
    throws HibernateException, LogConfigurationException
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Log log = LogFactory.getLog(Log4JLogger.class);
        try
        {
            log.info("starting transaction");
            session.beginTransaction();
           
            log.info("saving user");
            log.debug(user.toString());
            session.save(user);
            log.info("committing transaction");
            session.getTransaction().commit();
        }
        catch(Exception e)
        {
            log.info("rolling back transaction");
            session.getTransaction().rollback();           
            log.error(e.getMessage(), e);
            throw new HibernateException(e);
        }
        finally
        {
            session.close();
        }
    }


Do you think this is much better?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 6:36 am 
Newbie

Joined: Tue Jan 10, 2006 9:22 am
Posts: 6
Yes, this second piece of code is much better than the first.
When you got an opensession you get a clean object (which you can consider new, but it isn't) and when you got a getCurrentSession, you will get de session that was been used when you call this method.

I work here with a pool of connections, in this case when you got a SessionFactory, hibernate open 10(for example) connections with the database. As you know, openning and closing connections with database are a proccess that needs some proccessing. If you stay opening and closing connections you will need a good hardware to do it.
If you got a pool of connections, you will have permanently open 10 connections with database. And when you close the connection, the hibernate clean in a way that you can reuse it, then when you have an openSession, it will dispose to you some connection (not new) but in an clean state, like a new one. And when you got an currentSession, you will got a session that is been used by the hibernate at that moment.

See the diference?

_________________
Best regards
Dirceu Semighini Filho


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 9:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
getCurrentSession() will return the "current session" as defined by the configured "scoping context". By default this depends upon the transaction management strategy you are using:
1) JTATransactionManagerFactory = current sessions are scoped by the current JTA transaction
2) JDBCTransactionManagerFactory = current sessions are scoped by the current thread of execution.

This behavior is also pluggable by implementing CurrentSessionContext and naming your impl via hibernate.current_session_context_class


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.