-->
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.  [ 6 posts ] 
Author Message
 Post subject: Which is the more suitable session handling mechanism?
PostPosted: Wed Oct 29, 2008 10:02 am 
Newbie

Joined: Tue Sep 23, 2008 1:39 pm
Posts: 8
Hi, i have a problem on finding the more suitable session handling mechanism acording to my needs.

I first created a class that returns a thread-safe hibernate session. It is a copy of JPA PersistenceService class, i'm not using JPA but just hibernate.
But to solve the problem of performance i put my thread-safe session into http-session so i can share the same session across multiple request of the same http-session. I have an extra problem there because that way my hibernate session is now shared among different threads.

After that i found my sessions cache's get out of date because they know nothing about other session's transactions, see my previous post here:

http://forum.hibernate.org/viewtopic.php?t=991907

I could solve my problem by creating a new session every time a new request take place but that would have a poor performance.
Also i don't know if a second level cache can solve my problem and how.

So i am very confused.
What i want is to achieve a good performance but to get always my last changes.

I guess it is not a crazy thing what i want, just to have an hibernate session alive along the http-session lifetime, preventing threading problems and having my data always updated from the database.

Do anybody know which is the best way of handling my hibernate sessions?

Thanks.

_________________
Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 7:25 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
But to solve the problem of performance i put my thread-safe session into http-session so i can share the same session across multiple request of the same http-session. I have an extra problem there because that way my hibernate session is now shared among different threads.


I don't know what kind of performance problems you tried to solve, but I think this approach only makes performance worse.

The Hibernate documentation clearly states that a Session is not thread-safe. Don't try to use a single session from multiple threads. Trying to synchronize access to the session will more or less mean that your web app will only be able to handle a single request at a time.

Quote:
After that i found my sessions cache's get out of date


The first level cache that is held inside a session must be manually managed. You can call Session.clear() to empty the cache, Session.evict() to remove a single item from the cache or Session.refresh() to update a single item with new information from the database.

Quote:
I could solve my problem by creating a new session every time a new request take place but that would have a poor performance.


Why do you think so? A lot of users are using Hibernate in exactly this way. If you are worried about the number of database selects the second-level cache can be used to limit this. The use of proxies and lazy collections are also used to limit the number of selects. All of this is documented in the Hibernate documentation. See for example http://www.hibernate.org/hib_docs/v3/re ... mance.html.

If you are still worried about this I really recommend reading the "Java Persistence with Hibernate". It explains a lot of things with Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 9:58 pm 
Newbie

Joined: Tue Sep 23, 2008 1:39 pm
Posts: 8
Hi, first of all thanks for your reply.



But to solve the problem of performance i put my thread-safe session into http-session so i can share the same session across multiple request of the same http-session. I have an extra problem there because that way my hibernate session is now shared among different threads.


I don't know what kind of performance problems you tried to solve, but I think this approach only makes performance worse.

The Hibernate documentation clearly states that a Session is not thread-safe. Don't try to use a single session from multiple threads. Trying to synchronize access to the session will more or less mean that your web app will only be able to handle a single request at a time.


That's why i asked, i know this makes things worse but i didn't know another way of doing it. I'm new with hibernate and that's why i am asking all this. As you say i read that about the nonthread-safe session and i was trying to solve that problem.

After that i found my sessions cache's get out of date


The first level cache that is held inside a session must be manually managed. You can call Session.clear() to empty the cache, Session.evict() to remove a single item from the cache or Session.refresh() to update a single item with new information from the database.

I didn't know about the clear method, if that's how i guess it is the solution to all my problems. Do you say i can call clear after all my operations and that would force the session to fetch all the objects again the next time? That's what i need.

I could solve my problem by creating a new session every time a new request take place but that would have a poor performance.


Why do you think so? A lot of users are using Hibernate in exactly this way. If you are worried about the number of database selects the second-level cache can be used to limit this. The use of proxies and lazy collections are also used to limit the number of selects. All of this is documented in the Hibernate documentation. See for example http://www.hibernate.org/hib_docs/v3/re ... mance.html.

If you are still worried about this I really recommend reading the "Java Persistence with Hibernate". It explains a lot of things with Hibernate.


About the performance my worries came when i discovered that the first request (which creates the session and hold it into http-session) was so slow in comparisson with the comming ones. My client application is quering throw services (similar to ajax) and the response time was so poor at that moment, creating a session at every request. That's why i tried to change it.

I am not worried about the load but about the response time of the requests.

Of course if i would hold an unique session for the entire application i would avoid the overhead of creation, but you say i should not try to share the session among threads, so what's your suggestion? Should i create per thread sessions?

Thanks.

_________________
Sebastian


Top
 Profile  
 
 Post subject: Session Problem
PostPosted: Thu Oct 30, 2008 1:08 am 
Newbie

Joined: Wed Oct 29, 2008 3:00 am
Posts: 4
Location: India
Hi Zeven ,

nordborg is right.You should create a new session on new request.
For improving performance you can try two things :

1)Hibernate connection pooling feature
2)Hibernate caches.

You can read about hibernate cache from:

http://acupof.blogspot.com/2008/01/back ... three.html

_________________
Vikas Jindal


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2008 3:16 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
About the performance my worries came when i discovered that the first request (which creates the session and hold it into http-session) was so slow in comparisson with the comming ones.


Could this be because the first request also built the SessionFactory? This can take a rather long time (in my app it takes 10-20 seconds). But since the SessionFactory is thread-safe you only need to do this once and keep it in some kind of application scope. The simplest solution for this is something like the example near the end of http://www.hibernate.org/42.html.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2008 8:28 am 
Newbie

Joined: Tue Sep 23, 2008 1:39 pm
Posts: 8
Yes, my class is similar to the one at the end of the article you mentioned, but i am storing also the SessionFactory there, now i am sure that was the problem.

I will try by holding an application level SessionFactory and per-request hibernate sessions, that way i will have my request fetching from the database.

Thanks a lot nordborg
and also thanks Vikas for the links.

_________________
Sebastian


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