• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about lock/unlock sequence

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen lots of people have method like this:
My question is what if the code is broken after data.lock()? So you don't have a chance to unlock the record.
If we modify this method to one of the two ways:

or

To make sure the lock-read-modify-unlock sequence is correct.
Your comments are appreciated!
Thanks,
Kevin
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first modification doesn't do anything. (I certainly hope you don't plan to implement this method on the server!!!) The second is great -- very perceptive, you identified a genuine issue that you need to address.
- Peter
 
Kevin Cao
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, Peter.
The first one really looks wired, all I want to do is make the method atomic, so the lock-read-modify-unlock will not be broken. I am new to thread, I couldn't convince myself why it is not right even nested synchrinization is never a good thing.
-Kevin
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin -
Some more food for thought - take your example one step further into the locking mechanism itself and think about what happens if a client dies after locking a record and finally and therefore unlock never gets called.
-BJ
[ February 21, 2003: Message edited by: BJ Grau ]
 
Kevin Cao
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BJ-
As I understand, the only time finally statement would not be executed is if the System.exit()method terminates the program. So, data.unlock() will execute no matter what.
-Kevin
 
BJ Grau
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Cao:
BJ-
As I understand, the only time finally statement would not be executed is if the System.exit()method terminates the program. So, data.unlock() will execute no matter what.
-Kevin


You are right, what I meant by a client dying is the network connection to the server goes away. In general, my point is what if unlock fails for some reason? For example, you have exposed the methods of Data to the client through some remote object. You have a client business class with a method such as bookFlight that calls lock read modify unlock against the remote object that exposes Data to the client. Suddenly, the client's network connection fails, and the call to unlock on the remote Data never makes it to the server. Now you have a record locked and the owner is gone.
[ February 21, 2003: Message edited by: BJ Grau ]
 
Kevin Cao
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BJ-
Thanks for our response. I think the argument here is whether this method an atomic operation, or whether lock-read-modify-unlock will be broken. I can careless if unlock fails.
-Kevin
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic