-->
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.  [ 9 posts ] 
Author Message
 Post subject: doubt on use Hibernate with EJB and thread
PostPosted: Wed Jan 28, 2004 8:07 am 
Newbie

Joined: Mon Sep 01, 2003 10:40 am
Posts: 17
I use the below stateless ejb as facade.
I think that with it I haven't any problem with multithreading.
Is correct ?

Code:

//simple ejb facade
public class FacadeBean implements SessionBean
{

private SessionContext sessionContext;
  private SessionFactory sf=null;

 
  public void setSessionContext(SessionContext ctx)
  {
    this.sessionContext = ctx;
    getHibernateSessionFactory();
  }

 
  /**
  * Save a object
  */
  public void saveObject(Object obj, String action) throws HibernateException
    {
       Session se=null;
       Transaction trans=null;
       try{
             se=getHibernateSessionFactory().openSession(); //open session
             trans=se.beginTransaction();
             se.save(obj);
              trans.commit();
        }
        catch(Exception ej){trans.rollback(); ej.printStackTrace();}
        finally{se.close();}
  }
 
  /**
  * Return a SessionFactory
  */

   public SessionFactory getHibernateSessionFactory() {
    String sessionFactoryName = null;
    if(sf==null)
    {
          try{
               InitialContext ctx = new InitialContext();
               sf = (SessionFactory) ctx.lookup("hibernate/SessionFactory");
             }catch(Exception ex){ex.printStackTrace();}


    }
    return sf;
  }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 8:12 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Not 100% sure what you are asking.
The code that you have posted is completely single threaded so there shouldn't be any threading issues from your perspective.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 8:17 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
I agree, the problem set field only in initializer, so perfectly compatible with your EJB container.
But why do you open and close a Session only to save one object ?
Ciao


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2004 12:15 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
l.masini wrote:
I agree, the problem set field only in initializer, so perfectly compatible with your EJB container.
But why do you open and close a Session only to save one object ?
Ciao


How do you save a object without opening a session?


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Jan 28, 2004 12:18 pm 
Newbie

Joined: Mon Sep 01, 2003 10:40 am
Posts: 17
Into any method I make a new Session so I haven't problems with multithreading.

When I use EJBs, the application server make a pool of ejbs, so when a client call a service, the application restore a ejb from the pool and call the method.
So I can't set a session as variable istance, like code below

Code:
public class FacadeBean implements SessionBean
{

private SessionContext sessionContext;
private SessionFactory sf=null; //perform only a session create
private Session session;// <- possible sharing issue


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 1:03 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
SessionFactory is threadsafe. You can save this as an instance variable.
Don't save your Session as an instance variable. From the Session Javadocs:
Quote:
It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 6:47 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
I mean that you should't use a session-per-update pattern !!!
Ciao.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 7:39 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
l.masini meant http://www.hibernate.org/Documentation/SessionAndTransactionScope

This is a brand new and key page, so linking it isn't superfluous.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2004 4:06 am 
Newbie

Joined: Mon Sep 01, 2003 10:40 am
Posts: 17
Thanks for your hints.

But what you change into my code ?


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