-->
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.  [ 15 posts ] 
Author Message
 Post subject: SessionFactory in HibernateUtil or HibernateFilter
PostPosted: Mon Jul 25, 2005 3:57 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Hi,

I previously based my hibernate code on another example but found CaveatEmptor more clean. However, in my earlier code the SessionFactory was created in the Filter's init method. In CaveatEmptor I find that you have this initialization as static code in HibernateUtil.

Is this just a matter of choice or are there some benefits/drawbacks with either alternative?

Best regards

Lasse Bergström


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 5:25 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The static block works everywhere, not only in a servlet container.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 5:47 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
I got it. Thx a lot Christian.


Top
 Profile  
 
 Post subject: HibernateUtil
PostPosted: Sat Jul 30, 2005 4:49 pm 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Hi,

I have 2 questions regarding class HibernateUtil:

1) The configuration and sessionFactory objects are created in the static code. How about cleanup of this two objects. When are they null:ed? I am thinking of something similar to a "destroy"-method.

2) There are 2 methods rebuildSessionFactory. When are they used? I can't find any usage of these 2 in the application.

Best regards

Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 6:08 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
1) Depends, maybe in a context listener for redeployment? Call sessionFactory.close() when your application "ends".

2) I use it, you don't have to.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 31, 2005 3:40 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
christian wrote:
1) Depends, maybe in a context listener for redeployment? Call sessionFactory.close() when your application "ends".

2) I use it, you don't have to.


Just interested, when do you use rebuildSessionFactory?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 31, 2005 7:23 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
When I need to rebuild the SessionFactory after I changed the Configuration.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 31, 2005 7:31 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
So you do change the Configuration programmatically?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 31, 2005 7:33 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why not?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 31, 2005 7:37 am 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
christian wrote:
Why not?


Well I was thinking that normally u add objects manually and the configuration as well. But maybe u are building some kind of interface to edit Hibernate configuration?

Best regards

Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 31, 2005 8:01 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, but I like to change Configuration settings programmatically if I have to.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 31, 2005 7:02 pm 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Hi,

HibernateUtil.commitTransaction() is called from HibernateFilter. If I want to be able to handle a StaleObjectStateException in my web application and there present a nice message for the user, do I then have to have a HibernateUtil.commitTransaction in DAO-classes as well. Or does the session.saveOrUpdate() method signal a StaleObjectStateException?

The way I do it right now is:

1) In my DAO-class I call HibernateUtil.getSession().saveOrUpdate()
2) In the HibernateFilter the call is made to Hibernate.commitTransaction()

Is this the right way in a web application to be able to get StaleObjectStateException? This way I don't seem to get the optimistic locking strategy to work. My mapping file have this part:



<version name="updateVersion"
column="updateVersion"
type="long"/>



Best regards

Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 31, 2005 7:02 pm 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Hi,

HibernateUtil.commitTransaction() is called from HibernateFilter. If I want to be able to handle a StaleObjectStateException in my web application and there present a nice message for the user, do I then have to have a HibernateUtil.commitTransaction in DAO-classes as well. Or does the session.saveOrUpdate() method signal a StaleObjectStateException?

The way I do it right now is:

1) In my DAO-class I call HibernateUtil.getSession().saveOrUpdate()
2) In the HibernateFilter the call is made to Hibernate.commitTransaction()

Is this the right way in a web application to be able to get StaleObjectStateException? This way I don't seem to get the optimistic locking strategy to work. My mapping file have this part:



<version name="updateVersion"
column="updateVersion"
type="long"/>



Best regards

Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2005 8:36 pm 
Beginner
Beginner

Joined: Wed Dec 10, 2003 6:58 pm
Posts: 40
Location: Stockholm, Sweden
Hi Christian,

From what I understand class HibernateFilter is not actually used in the example since it's not a web application, am I right?

If I am right on that I have some questions.

The call HibernateUtil.commitTransaction() is made from HibernateFilter. Is the purpose of that call to cover for missed call to the same method in the application code?

So, should I put HibernateUtil.commitTransaction after each update, insert, delete etc?

And if I do, do I need the call to HibernateUtil.commitTransaction() in HibernateFilter?

Best regards

Lasse


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 12:06 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can do that in whatever way you like. The purpose is to show you how interceptors can be used for transaction demarcation (and possibly exception handling if you like). Be creative...


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