-->
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.  [ 4 posts ] 
Author Message
 Post subject: use Hibernate and HttpSession together
PostPosted: Tue Sep 07, 2004 1:13 am 
Newbie

Joined: Wed Sep 01, 2004 5:49 am
Posts: 6
Location: Stavropol city
Hi, all

I'm using hibernate in my web app on jboss application server.
To avoid initialization delay (configuring, opening session, ect) on each jsp I save the object which encapsulated all hibernate data (HibernateLibBean) in HttpSession.

Code:
    public HibernateLibBean getHib(HttpSession http_ses, String str_connector, String str_ip) throws Exception
    {
        HibernateLibBean hib = (HibernateLibBean) http_ses.getAttribute(HIB_PROP_NAME);
       
        try
        {
            if( hib == null )
            {
                        //this code executed only once
              hib = new HibernateLibBean();
              hib.sys_init(str_connector, str_ip);
              http_ses.setAttribute(HIB_PROP_NAME, hib);
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
       
        return hib;       
    }


and later on each jsp

Code:
HibernateLibBean hib = hib_loader.getHib(session,"connector_test.jar", request.getRemoteAddr() );


This brings me a very fast initialization.

HibernateLibBean has method finalize(). The purpose of this method is to close all hibernate connection with database.
It works, But i want to hear your comment.
Maybe exist a better solution for this problem.

Thanks for your comments.


Hibernate version: 2.1.4
Name and version of the database you are using: postgresql 7.3.6 RH


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 3:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
To avoid initialization delay (configuring, opening session, ect)

only building the sessionFactory is expensive, not getting a new hibernate session. SessionFactory must be a singleton... take a look at int() servlet filter for example.

2- HttpSession is not threadsafe, so if your client is hitting F5 many times under IE5, many thread will hit the hibernate session (stored in the httpSession) and it can be VERY dangerous.

Furthermore, with your strategy where is the sessionFactory, i just don't understand what is exactly done...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 7:09 am 
Newbie

Joined: Wed Sep 01, 2004 5:49 am
Posts: 6
Location: Stavropol city
Thanks for your reply.

Can i solve that problem by mark HibernateLibBean's method as synchronized?
I think that makes all calling to hibernate session method - threadsafe. And only one copy of HibernateLibBean will be used in one HttpSession.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 7:19 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Do not do that, store hibernate session in java.lang.ThreadLocal instance.


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