-->
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.  [ 1 post ] 
Author Message
 Post subject: gracefully handling ObjectNotFoundException?
PostPosted: Wed Sep 06, 2006 4:29 pm 
Beginner
Beginner

Joined: Sat Aug 19, 2006 8:04 pm
Posts: 30
hi

i'm writing a webapp here that keeps track of inventory items. normally, the id field has no business significance for the user (they don't even know about it), but for this app it's actually a good idea to make the id field important.

the user keeps track of what id numbers they need to be concerned with. on a page i have a text box, Find item by id#: ________

so they would come on and type in 6, a page loads item 6 from the db and shows all the stuff with it. but if they enter 439804343432432 in there, i get an ObjectNotFoundException, no row associated.

so, how do i handle that?

1. in the DAO.read(id) method, before it does the load: do a query, select id from Items where id = enteredId ... and see if that returns something. if so do the load, if not return null or something else. i would probably want to do some locking in here, what if the query says it exists, someone else deletes it real quick before they read it?

or

2. try / catch the exception .. i'd rather prevent the exception proactively, rather than catching it. this does save a trip to the database. also i've read something about the exception doesn't really happen until well after the load. so the DAO.read(id) method returns ok, but when i try to do returnedItem.getSomething() that's when the exception occurs ... that is really lousy.

better yet,

3. is there some sort of exists(id) method?



thoughts? anyone dealt with this before?

thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.