This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes DeadLock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "DeadLock" Watch "DeadLock" New topic
Author

DeadLock

John Winstanley
Greenhorn

Joined: Apr 08, 2003
Posts: 14
Hello All,

I'm looking at ways to prevent deadlock, this is my lock method. Do you think it is ok to throw an unchecked exception if it takes the client too long to obtain a lock.



I think it is ok to do this becuase I state that anyone using my database API should hold a lock for the minimum amount of time possible. And in the cercumstance where a client is holding a lock for too long other clients will just have to retry later.


John
Dieskun Koper
Ranch Hand

Joined: Aug 15, 2004
Posts: 85
My instructions say the following about the lock method:

'the current thread gives up the CPU and consumes no CPU cycles until the record is unlocked'

Using your method, the current thread gives up the CPU and consumes no CPU cycles until 10 seconds have passed or the record is unlocked.
I would not risk auto-failure here, especially as there are other ways to deal with deadlocks.

And I think it is bad design to throw an unchecked exception for this. Please read up on the difference between runtime and checked exceptions to understand why.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi John,

I suspect that your solution will not meet the requirements. Check the wording of the API documentation you were provided for the lock method - does it imply that you can have a timeout at all? If you can have a timeout, do you think having the timeout on the thread attempting to aquire the lock is correct?

You also try to aquire the lock before checking whether the record is valid or not. You might want to consider having a separate (private) method to check validity of the record, in which case your code could be reduced to:

The recordIsValid() method can throw the RecordNotFoundException directly.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Andrew Monkhouse:

You also try to aquire the lock before checking whether the record is valid or not.


Andrew,

can you explain why checking for record validity before locking is of any benefit. I view the resource being locked as a simple numeric token, my LockManager doesn't know about the I/O at all. I do this to avoid nested synchronization problems. After the Data instance aquires the lock on the token, it checks if the token corresponds to a valid record and unlocks if it doesn't.

Any checking done before aquiring the lock must be redone after aquiring it. This checking might save an occasional lock attempt, but would add I/O overhead.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Peter,

In retrospect I am sorry I suggested this. With a cached database my solution might catch an attempt to lock an invalid record slightly quicker than your / John's solution - but only slightly quicker.

Please ignore my earlier suggestion.

Regards, Andrew
GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
I am using the leasing solution deadlocks or orphan locks.
Have a dameon thread running in the background to check if a locked record has been locked for certain time limit. Call unlock and notifyAll() if the lease has expired for the locked record.

However, the client is never informed by the server when the locked record has been unlocked. Not taking this into consideration.

Any feedback?
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Although it might not hurt anything, I would avoid using a deamon thread if possible. Could you not check for an orphaned lock in the lock method itself (e.g. in the isValid() method mentioned above)?


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DeadLock
 
Similar Threads
My Locking...Advice needed...
some ideas about detecting the lock timeouts
B&S 2.2.1 howto prevent nested locking
Locking and remoteException
Passed SCJD