-->
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.  [ 2 posts ] 
Author Message
 Post subject: Is it right nested getCurrentSession?
PostPosted: Thu Jan 04, 2007 10:26 pm 
Newbie

Joined: Tue Feb 21, 2006 2:00 am
Posts: 14
Location: Seoul, Korea
first, sorry for my poor english^^;

I am building multi threaded java applications without web server.

some configuration properties of my hibernate.cfg.xml are like this.
Code:
...
...
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
...
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="hibernate.max_fetch_depth">1</property>
        <property name="hibernate.show_sql">true</property>


one of my app, I run 5 thread(same java class) concurrently and they get their work repeatedly from one shared queue.

But, I doubt this is right coding...

I will show you seudo code..

Code:
try{
    sess = HibernateUtil.getSessionFactory()getCurrentSession();
     tx = sess.beginTransaction();
     objList = sess.createQuery(.......).list();
     ...
     ...
     new SomeNonThreadClass(objList.get(0),...).callSomeMethod();
     ...
     tx.commit();
} catch(HibernateException e){
    if( tx != null) tx.rollback();
    ....
}


HibernateUtil use singleton pattern for session factory.

In code above, SomeNonThreadClass is plain pojo didn't extends Thread feature.

And SomeNonThreadClass is using HibernateUtil and Session.

My question is..

1) Is it problem using getCurrentSession method in SomeNonThreadClass.

2) I using now OpenSession and session.close in SomeNonThreadClass. Is it more right for this case?

3) singletoned HibernateUtil is right usage in multi thread env?

I will appreciate all your advice.

Thanks in advance^^


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 4:44 am 
Beginner
Beginner

Joined: Wed Aug 31, 2005 3:54 am
Posts: 45
HibernateUtil is a singleton and enable to create an other singleton : SessionFactory, so for that no problem.

Session is not threadsafe so it must be created for each thread localy.
To do that, you must defined the configuration :
<property name="current_session_context_class">thread</property>
With that the session is attached to current running thread.
The sessions generated here are unusable until after Session.beginTransaction() has been called. If close() is called on a session managed by this class, it will be automatically unbound.

Don't forget to rate ;-)


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