wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Data class test for B&S - do I have a problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Data class test for B&S - do I have a problem?" Watch "Data class test for B&S - do I have a problem?" New topic
Author

Data class test for B&S - do I have a problem?

John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Hi all,

firstly big thanks to Roberto Perillo for the excellent DataClassTest. I've slightly modified this for B&S but I have two queries;

1) Occasionally I get a RNFE when the DeletingRecord1Thread gets executed before the UpdatingRecord1Thread. This seems correct to me, is this what others have found?

2) If I up the loop counter to anything more than 1 I will get DuplicateKeyExceptions thrown - which I would expect because I'm re-creating the same record over and over again in CreatingRecordThread. Again this seems correct to me, what have others found? I do get into deadlock in this situation but I can't assess what this means when so many other DKEs are happening.

Other than that, I suppose I'm at the horrible stage of thinking that the Data class is complete, but scared of automatic failure due to some unforseen locking error on my part...

Regards,

John

PS

I'll happily post the updated test code if an admin can confirm I won't be breaking any rules by doing so. I think not, it's not part of the submission.


Passed: SCJP 6 (90%), SCJD 6
Other: Spring training, extensive Swing experience
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Slight update;

I changed the CreatingRecordThread and UpdatingRandomRecordThread to generate records which have different database keys each time so as to avoid DuplicateKeyExceptions. Now I still have the problem with RecordNotFoundExceptions when I run the test 10 times. Here is the relevant output;

16 locked record #1 on DeletingRecord1Thread
16 trying to delete record #1 on DeletingRecord1Thread
38 updated record #23 on UpdatingRandomRecordThread
38 trying to unlock record #23 on UpdatingRandomRecordThread
16 deleted record #1 on DeletingRecord1Thread
16 trying to unlock record #1 on DeletingRecord1Thread
38 unlocked record #23 on UpdatingRandomRecordThread
16 unlocked record #1 on DeletingRecord1Thread
56 caught RecordNotFoundException for record #1 in DeleteingRecord1Thread
31 caught RecordNotFoundException for record #1 in DeleteingRecord1Thread
34 caught RecordNotFoundException while updating record #1 in UpdatingRecord1Thread
19 caught RecordNotFoundException while updating record #1 in UpdatingRecord1Thread
26 caught RecordNotFoundException for record #1 in DeleteingRecord1Thread
24 caught RecordNotFoundException while updating record #1 in UpdatingRecord1Thread
46 caught RecordNotFoundException for record #1 in DeleteingRecord1Thread
41 caught RecordNotFoundException for record #1 in DeleteingRecord1Thread
54 caught RecordNotFoundException while updating record #1 in UpdatingRecord1Thread
49 caught RecordNotFoundException while updating record #1 in UpdatingRecord1Thread
21 caught RecordNotFoundException for record #1 in DeleteingRecord1Thread
11 caught RecordNotFoundException for record #1 in DeleteingRecord1Thread
36 caught RecordNotFoundException for record #1 in DeleteingRecord1Thread
39 caught RecordNotFoundException while updating record #1 in UpdatingRecord1Thread
29 caught RecordNotFoundException while updating record #1 in UpdatingRecord1Thread
14 caught RecordNotFoundException while updating record #1 in UpdatingRecord1Thread
51 caught RecordNotFoundException for record #1 in DeleteingRecord1Thread

Notice 16 correclty locks, deletes then unlocks record 1. Afterwards numerous threads find they cannot update nor delete record one. This seems right to me, does it to anyone else?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Hi John,

1/ When a record is deleted, a thread should not gain the lock on that record again. So the lock-method should throw the RNFE, not the update/delete method

2/ When you have B&S you can throw a DKE, but your code should not deadlock of course when such exceptions are being thrown!

I'll happily post the updated test code if an admin can confirm I won't be breaking any rules by doing so. I think not, it's not part of the submission.
You won't break any rules, just go for it! I would add the updated code to the thread of the data class test (and not this one)

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Thanks Roel,

yes it is the lock method that throws the RNFE, so maybe that area is working. Clearly I need to look at the times a DKE is thrown and ensure that it does not deadlock.

I'll post the code to that forum once I know both my Data class and the test class are working as they should be.

Thanks again,

John
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

John McParland wrote:I'll post the code to that forum once I know both my Data class and the test class are working as they should be.


That will be appreciated, champion!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

So I got it working (I hope!) I investigated where the exceptions were being thrown and in those cases, printed to the console that this is what happened. Sometimes I executed a check to see that the exception was expected (e.g. in UpdatingRandomRecordThread if it tried to update record 1 I might expect a RecordNotFoundException) and I haven't seen any unexpected exceptions or deadlock since.

One minor thing that's bugging me, my Data class follows the facade pattern, calling methods in my "Database" class. A lot of other folks needed to synchronized the Data class methods, I only did my Database methods. I guess the end result is the same (the threads gets the object lock on one object) but still...

The code is now on the other thread.

Regards,

John
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

John McParland wrote:A lot of other folks needed to synchronized the Data class methods, I only did my Database methods. I guess the end result is the same (the threads gets the object lock on one object) but still...


I am one of these people. This is just to be sure that the Data class is Thread-safe. It's sort of hard to say without looking at the code, but I also think it's pretty much the same. If you didn't get deadlocks, then it a-ok!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data class test for B&S - do I have a problem?