-->
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.  [ 8 posts ] 
Author Message
 Post subject: Websphere with CMT, getcurrentsession() is thread safe?
PostPosted: Fri Jun 09, 2006 4:05 am 
Newbie

Joined: Tue Apr 11, 2006 4:57 am
Posts: 17
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1.3

I'm using Hibernate with Websphere Application Server 6.0 with CMT.
I have configured

hibernate.transaction.factory_class=org.hibernate.transaction.CMTTransactionFactory
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.WebSphereExtendedJTATransactionLookup

My question is: is this thread safe in this enviroment:

Code:
public static Object getById(Session aSession, Long aId, Class aClass,
            boolean lock) throws HibernateException, DBONotFoundException {     
        Object back = null;       
        if (lock) {
            back = aSession.get(aClass, aId, LockMode.UPGRADE);
        } else {
            back = aSession.get(aClass, aId);
        }
        if (back == null) {
            throw new DBONotFoundException("Data Object not found", aClass, aId);
        }
        return back;       
    }
[/code][/u][/i]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 7:15 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Yes, it is thread safe code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 7:28 am 
Newbie

Joined: Tue Apr 11, 2006 4:57 am
Posts: 17
Thanks, and what if I put the getCurrentSession() into the static method? Will it be thread safe too?


Code:
public static Object getById(Long aId, Class aClass,
            boolean lock) throws HibernateException, DBONotFoundException {   

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Object back = null;       
        if (lock) {
            back = session.get(aClass, aId, LockMode.UPGRADE);
        } else {
            back = session.get(aClass, aId);
        }
        if (back == null) {
            throw new DBONotFoundException("Data Object not found", aClass, aId);
        }
        return back;       
    }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 9:11 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Yes, it is thread safe too (it returns session per thread / transaction).


Top
 Profile  
 
 Post subject: thanks
PostPosted: Fri Jun 09, 2006 10:15 am 
Newbie

Joined: Tue Apr 11, 2006 4:57 am
Posts: 17
Thanks Baliukas!

One more thing.... :)
I thought that I have to program the thread handling for myself (because the Sesssion object is not thread safe), and I saw a lot of example about thread-safe HibernateUtil and session handling, but some people said that it's not a good idea to use a ThreadLocal in Websphere Enviroment.
So the CMT and the JTA does it for me?
So if multiple threads call my static finder method everyone gets its own session object? (Or how does it works? I haven't found anything about this...)

thx: Peter


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 11:10 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Session object is not thread safe, but I see you do not share. It is recommended to use transaction scope sessions with JTA (it is the mater of configuration) Thread locals is not so bad idea too, but you do not need to code this stuff, probably you saw "old" examples. Just use "factory.getCurrentSession()", it is safe, session factory is thread safe too and you can share it as static field.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 11:16 am 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
If you are operating within a J2EE compliant EJB container, you aren't even allowed to manipulate threads. I'm not sure an error would be thrown by websphere, but it violates the contract with the container. Hibernate is designed to operate correctly within an EJB container, so if you find yourself thinking that you need to do something special with regard to threading, you are probably misunderstanding the situation.

In order for the session to work correctly, it has to be associated with a single transaction and thread, so Hibernate will have to provide you with a separate session instance for each thread. So it is "threadsafe" in that sense.

I would have to question your use of a static method, assuming that you are using it in conjunction with stateless session beans. It is somewhat at odds with the J2EE architecture. But without seeing your entire design, I can't be sure. It's something to think about.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 3:08 pm 
Newbie

Joined: Tue Apr 11, 2006 4:57 am
Posts: 17
We are working on a layered architecture, and the static method is in the DAO layer (in a DAO object (POJO)). This entire layer is in a JAR (in an EAR), and stateless session beans communicates with this layer.

StatelessSessionBean -> (use) DAO layer -> (use) Hibernate -> (communicate) Database


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