aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Question on deadlock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Question on deadlock" Watch "Question on deadlock" New topic
Author

Question on deadlock

Alan Morgan
Ranch Hand

Joined: Apr 18, 2005
Posts: 113
Hey all,

I'm about to submit and the one concern still outstanding is deadlock.

I've run the test as outlined by John Smith:


Start 50 threads each representing a distinct client and have each of them book the same record 1000 times in a row (by decrementing one of its fields). At the end of the run, the field value should be exactly 50000 below the level at the start of the run. This "meta-test" will test for deadlocks, concurrency, thread starvation, race conditions, and all those evils with a fair degree of certainty.


Test ran fine and 50000 was the value so, so far so good.

I also decided to go for the simplest solution which John also took which is:


In my implementation, the client can only lock one record at a time (the book() method would simply block until the record is booked). Thus, the deadlock you are talking about would not be a possibility.



I am going to note this approach in my choices.txt
I guess my question is have I done enough ?

I would love to hear from people who passed, and especially those who have passed using this approach.

Any help greatly appreciated as this is the last step on my journey (well hopefully)

Thanks,
Alan.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11478
    
  94

Hi Alan,

I also had my booking operation block until a result was received from the server. I think that is a perfectly acceptable way of handling the problems that John was talking about.

Funnily enough, I can remember him popping in to answer the question about testing & deadlocks, and then disappearing back to his favourite forum (MD) - mainly because he used to be such a major contributor here that I was hoping he would stick around. So I remember the post, even though I cannot find it at present. So I know what you are talking about, even though you didn't link to it.

Regards, Andrew


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

Joined: Apr 18, 2005
Posts: 113
Andrew,

Here is the link for the thread I believe you are looking for:
http://www.coderanch.com/t/187162/java-developer-SCJD/certification/Dealing-Deadlocks-Stale-Locks

So thats it then ?.
In my choices.txt deadlock section I say that each user can only lock one record at a time as book is the only method that locks a record and it blocks waiting for a response ?

Anything more need to be documented about deadlock avoidance ?

I'm really hoping that this is it as I've reached certification exhaustion.
Really great to do it but at a certain point you just get sick of looking at it and thinking about it and just want to submit.



Thanks,
Alan.
Alan Mc Kernan
Ranch Hand

Joined: Oct 13, 2005
Posts: 59
Hey Alan,

I don't quiet understand the solution presented. I also have a business layer encapsulating the 48-hour rule, and exact matches on searches etc.

How do you allow a client to book one record at a time? You mention "blocking".. do you mean that your book method is synchronized? I don't really understand why we need to do this. I can't see how any client could book more than one record at a time.

The test that john mentions, I dont understand this either. You create a Thread runner that will get a Service instance form the server and then try to book a specified record ? This thread runner will have a static field, which all of the threads will try to decrement? Or do you mean that you modify your book method to update a decrement a field (integer) in the database? And disable the 48 hour rule/allow the booking to be re-booked? And then fire the 50 threads?

I'd appreciate if you could ellaborate a bit.

Alan
Alan Morgan
Ranch Hand

Joined: Apr 18, 2005
Posts: 113
Originally posted by Alan Mc Kernan:
Hey Alan,

I don't quiet understand the solution presented. I also have a business layer encapsulating the 48-hour rule, and exact matches on searches etc.

How do you allow a client to book one record at a time? You mention "blocking".. do you mean that your book method is synchronized? I don't really understand why we need to do this. I can't see how any client could book more than one record at a time.

The test that john mentions, I dont understand this either. You create a Thread runner that will get a Service instance form the server and then try to book a specified record ? This thread runner will have a static field, which all of the threads will try to decrement? Or do you mean that you modify your book method to update a decrement a field (integer) in the database? And disable the 48 hour rule/allow the booking to be re-booked? And then fire the 50 threads?

I'd appreciate if you could ellaborate a bit.

Alan


No my book method is not synched....what I mean by blocking is that when I call DB.update(..) my client waits for a response before he continues.
More than one record ? - theoretically I could spawn another thread to do the update for me and therefore the UI would still be able to function while the update was occuring. So now the user can choose to update another record and therefore in theory have two records locked at the same time.


The test is something like:



So I am getting 50000 threads (50 clients times 1000 attempts) to change record 1. The outcome should be that the name is 50000 at the end.

The 48 hour rule is irrelevant here.
That rule is business logic and my test is run on the data layer thereby bypassing the business logic layer.

Sorry if I am not explaining this correctly...its been a very long day

Have a look here:

http://www.coderanch.com/t/187926/java-developer-SCJD/certification/Locking-Approach

and the thread I linked to for Andrew in my post above for fuller discussions.

Thanks,
Alan.
Alan Mc Kernan
Ranch Hand

Joined: Oct 13, 2005
Posts: 59
Excellent, I understand now. I ran the same test against my data layer and also receive 50000.

Cheers for taking the time to reply.
Alan Morgan
Ranch Hand

Joined: Apr 18, 2005
Posts: 113
Not a bother Alan.
Glad you got 50000...I was delighted to see that myself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on deadlock