-->
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: To flush or not
PostPosted: Mon Dec 20, 2004 11:39 am 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
I'm using the technique where you open a transaction....and then close it with an app filter. I'm using the example code from the Caveat emptor app.


Code:
   public String makePersistent(Metric metric) throws InfrastructureException
   {
      try {
         HibernateUtil.getSession().saveOrUpdate(metric);
         HibernateUtil.getSession().flush();
      } catch (HibernateException ex) {
         throw new InfrastructureException(ex);
      } catch (Exception e) {
         throw new InfrastructureException(e);
      }
      
      return metric.getMetricId().toString();
   }


The problem I ran into (before I added the flush statement) was that database errors seemed to be ignore. If a user tried to add a duplicate record....the makePersistent method does not catch the error.

I solved this by adding the flush()...but I'm wondering if I've correctly solved the problem.

In other words...until you close the transaction (which I'm doing with an app filter) you don't get your error. Since you don't close the transaction in the "makePersistent" -- you don't catch the error until much later.

By adding "flush" I then get my error trapped they way I expect, but I'm wondering if I'm breaking something else -- something that I don't understand and will haunt me later.


Thanks,

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 9:59 am 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
No opinions on this? Surely I'm not the only one to run into this problem as I'm using code from the Caveat Emptor application.

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 10:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can flush anytime you want.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 11:09 am 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
michael wrote:
You can flush anytime you want.


Thanks...wanted to be sure I wasn't breaking the "lazy" feature, where I can still access related values that haven't yet been queried from my jsp's. (not that I've used this feature yet).

Now, as far as technique -- is the flush() the right approach? Or should the same "app filter" that does the commit....somehow do the error handling?

Typically I try to handle errors where they occur...but with Hibernate not performing the sql until it absolutely has to, you are no longer in the code where the error occurs.

Lee


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.