-->
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.  [ 7 posts ] 
Author Message
 Post subject: Can Hibernate be implemented in a multithreading application
PostPosted: Wed Jul 05, 2006 1:55 am 
Newbie

Joined: Wed Jul 05, 2006 1:35 am
Posts: 9
Hi Everyone,

Can Hibe be implement in a multi threading application? If yes
any best practices to do it?

thanks

abe


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 4:23 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
You should have a look at the ref doc :
http://www.hibernate.org/hib_docs/v3/re ... tions.html

Remember that the Session object is not thread-safe, the SessionFactory is. So, take care about synchronizing your session object.

Also remember that the session objects should be very-short lived. In fact, this is a cache, so if you're working with different sessions that, say, have loaded the same objects, you will encounter some risks problems of sync in db (which versions will be persisted).

Moreover, if your environment is highly concurrencial, have also a look at the version field of hibernate (see the ref doc) and maybe also the pessimistic lock instead of the optimistic one.

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


Top
 Profile  
 
 Post subject: threading
PostPosted: Wed Jul 05, 2006 5:02 am 
Newbie

Joined: Wed Jul 05, 2006 1:35 am
Posts: 9
Hi thanks for the reply..

In my implementation of threading.. each thread has its session.. is that right?

abe


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 5:23 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Yes, you must configure the SessionFactory to return a new session for each thread.

You can configure it using this : http://www.hibernate.org/hib_docs/v3/re ... properties

Code:
hibernate.current_session_context_class => thread


But as I said, you must take care that your sessions are short-lived. They're in fact a cache (the session is also called "first-level cache"), so could create some sync issues if not always short-lived.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 5:56 am 
Newbie

Joined: Wed Jul 05, 2006 1:35 am
Posts: 9
Hi Again.. i am using a OpenSession to get a Session from my SessionFactory
is this the correct way to use it in a multi threading application

abe

batmat wrote:
Yes, you must configure the SessionFactory to return a new session for each thread.

You can configure it using this : http://www.hibernate.org/hib_docs/v3/re ... properties

Code:
hibernate.current_session_context_class => thread


But as I said, you must take care that your sessions are short-lived. They're in fact a cache (the session is also called "first-level cache"), so could create some sync issues if not always short-lived.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 7:06 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
great_abe wrote:
Hi Again.. i am using a OpenSession to get a Session from my SessionFactory
is this the correct way to use it in a multi threading application


I don't think so. Please have a look at the reference doc :
http://www.hibernate.org/hib_docs/v3/re ... orial.html

You should prefer retrieving it this way :
Code:
HibernateUtil.getSessionFactory().getCurrentSession();


The HibernateUtil helper class is given in the reference documentation of Hibernate :
http://www.hibernate.org/hib_docs/v3/re ... pp-helpers
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;
    }
}


Then, configure in hibernate.cfg.xml the thing I've spoken about before :
Code:
hibernate.current_session_context_class => thread


As explained in the doc, the meaning of the "current session" is depending on the configuration of this property "current_session_context_class". So give a special attention to it.

To finish, please don't forget to rate my answers by Yes or No it was useful below, as recalled in my signature. Thanks

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 11:49 am 
Newbie

Joined: Tue Nov 15, 2005 8:27 pm
Posts: 19
Location: Columbia, MO USA
batmat wrote:
You should have a look at the ref doc :
http://www.hibernate.org/hib_docs/v3/re ... tions.html

Remember that the Session object is not thread-safe, the SessionFactory is. So, take care about synchronizing your session object.

Also remember that the session objects should be very-short lived. In fact, this is a cache, so if you're working with different sessions that, say, have loaded the same objects, you will encounter some risks problems of sync in db (which versions will be persisted).

Moreover, if your environment is highly concurrencial, have also a look at the version field of hibernate (see the ref doc) and maybe also the pessimistic lock instead of the optimistic one.


I'm using Hibernate in a Swing desktop application. Are the sessions required to be short-lived if you use the session-per-conversation pattern? The problem I see with closing the session is that lazy-loading of related objects and collections won't work if the session is closed. If you close the session, you have to make sure that all the objects that might be displayed are loaded before the session is closed and the objects are detached which means that you might be loading more objects into memory then necessary. I'm thinking that if you have long conversations, it is better to use the open-session-in-view pattern so you don't have to be constantly reloading the same objects into new short-lived sessions. The downside I see is that the application has to be prepared to handle exceptions when Hibernate detects that another user has modified an object while it is in your session. Is this correct?

Bruce


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