Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What to do with unlock on exceptions

 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say you have a lock on a record. Then during one of your operations,
lets say book, you run into a "Already booked exception" in your data class, before you throw the exception should you explicitly call unlock from your book function.
If you don't that record will remain locked?
my locking looks something like:

If book throws an exception, getDataObj().unlock(recordNumber,cookie) will never be executed. So the record remains locked.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest putting the unlock() in a finally, to ensure it eventually gets called whether subsequent operations succeed or fail.
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should have thought of that, great idea.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!