File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How can I test a record which is already locked by another client? 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 "How can I test a record which is already locked by another client?" Watch "How can I test a record which is already locked by another client?" New topic
Author

How can I test a record which is already locked by another client?

long lingyu
Ranch Hand

Joined: Feb 15, 2004
Posts: 51
How can I test a record which is already locked by another client?
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hi Long
Originally posted by long lingyu:
How can I test a record which is already locked by another client?

I will try to explain one way of doing it, to say, the way I did it
I used a static HashMap called lockedRecords to hold the cookie, recNo values as key, value pairs. So whenever a thread wants to lock in the lock method, it synchronizes on this lockedReocrds HashMap, checks if the record is already locked using lockedRecords.contains(). If the record is already locked, it waits on the record using wait() method. I think, there are many different ways of doing this for example using lock managers. You can search this forum on locking issues. Here is a thread where locking is discussed in very great detail.
Topic: Locking - Notification issues
Good Luck.
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
Hi Liqun,
Originally posted by long lingyu:
How can I test a record which is already locked by another client?

This depends on how you designed your system. If you use pessimistic booking you can test a record locked by another client by starting two network client instances of your application and trying to book the same record. By pessimistic booking I mean that you acquire the lock before you ask the user to type in the customer ID. By contrast, in optimistic booking you make the user enter the customer ID before you acquire the lock. If you support pessimistic booking you can test your application as follows (you can do everything on the same machine if you want):
Start three instances of your application. First, one running in server mode, and then two different clients each running in network client mode. Note: that you will have to configure the network clients to use localhost as the database server.

1) In client 1, highlight record number 5, press the book button.
2) Client 1 allows the user to enter a customer ID this point, but don't enter one yet, you want to keep this record locked for the test.
3) In client 2, highlight record number 5, press the book button.
4) Client 1 will not allow the user to enter a customer ID until client 1 has released the lock on record number 5.
5) Enter a customer ID for client 1.
6) Client 2 should now acquire the lock for record number 5 and display the latest customer ID for record number 5 which should be the one entered by client 1.
7) Client 2 can now overwrite the customer ID and press enter, or decide not to overwrite the customer ID and press cancel.
If you use optimistic booking then you can test the application using the testing harness I described here:Topic: All of URLy Bird 1.1.3 about Junit test
You can add System.out.println's to the output of the test harness that shows when a particular thread requests a lock. Then when you look at the output, you will see, for example, a thread request and receive a lock on recNo=5, then you'll see another thread request a lock on recNo=5, but you will notice that this second thread doesn't get the lock until the first thread unlocks recNo=5.
Of course, you can and should use the testing harness to test your application whether you use optimistic or pessimistic booking. It's just that because pessimistic booking allows the user to hold a lock for an indefinite period of time it's possible to test locking while running your actual system. This sort of testing is much more difficult to do in pessimistic booking because you don't have any real control over how long a record can be locked. Note that what I'm describing as an advantage for testing your locking using the pessimistic scheme is exactly what many people regard as a very negative characteristic of the pessimistic scheme (potentially long periods during which the record is locked). Nevertheless it's kind of nice to be able to test your locking scheme using your client GUI in this way.


Regards, George
SCJP, SCJD, SCWCD, SCBCD
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
There's a mistake in my last paragraph of my previous post, but editing the previous post is not a good option due to the editing bug.
The last paragraph should read:
Originally posted by George Marinkovich:
Of course, you can and should use the testing harness to test your application whether you use optimistic or pessimistic booking. It's just that because pessimistic booking allows the user to hold a lock for an indefinite period of time it's possible to test locking while running your actual system. This sort of testing is much more difficult to do in optimistic booking because you don't have any real control over how long a record can be locked. Note that what I'm describing as an advantage for testing your locking using the pessimistic scheme is exactly what many people regard as a very negative characteristic of the pessimistic scheme (potentially long periods during which the record is locked). Nevertheless it's kind of nice to be able to test your locking scheme using your client GUI in this way.
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
I had to edit/delete my previous post, because of the reply-quotes bug.
Originally posted by George: /

1) In client 1, highlight record number 5, press the book button.
2) Client 1 allows the user to enter a customer ID this point, but don't enter one yet, you want to keep this record locked for the test.
3) In client 2, highlight record number 5, press the book button.
4) Client 2 will not allow the user to enter a customer ID until client 1 has released the lock on record number 5.
5) Enter a customer ID for client 1.
6) Client 2 should now acquire the lock for record number 5 and display the latest customer ID for record number 5 which should be the one entered by client 1.
7) Client 2 can now overwrite the customer ID and press enter, or decide not to overwrite the customer ID and press cancel.

[ March 17, 2004: Message edited by: Vish Kumar ]
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
Thanks Vish,
You're quite right with your correction (too many 1's and 2's floating around).
The reply-quote bug is quite annoying...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I test a record which is already locked by another client?
 
Similar Threads
How to get feedback from lock()
How to test Network mode?
3rd level of locking
update and delete SecurityException question.
Lock question!