This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes lock & unlock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "lock & unlock" Watch "lock & unlock" New topic
Author

lock & unlock

ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
I am using the lock & unlock method as follows :
manager.lockRecord(recNum);
server.modify(dataRecord);
manager.unlock(recNum);
Is my approach correct ?
How could I test whether my lock/unlock is working succesfully as these operations are executed instantly.
Thanks
Ravi


SCEA, SCBCD, SCWCD, SCJD, SCJP
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

After you lock the record, won't you have to read it to make sure no other user has changed it since you first searched on it some time ago?
as in lock-read-modify-unlock sequence
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

How could I test whether my lock/unlock is working succesfully as these operations are executed instantly.

Start the server, start 2 clients, and from each of the client, have your code book a particular flight 1000 times in sequence.
Better yet, write a test class (outside of the application), that starts 50 threads each connecting to db.db on the server, and each booking 1000 seats (one at a time) on a single flight. That will actually take a few minutes to precess. After that, make sure that your seat count for that flight decreases by exactly 50000.
Eugene Kononov.
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Have a configuration file Lock.txt which contains recNo,NoOfSeats as follows
10,10
10,20
10,10
10,1
10,1
Lets assume there are 41 seats are there for RecNo 10
Now write a Thread based TestClient which will read the above conf and span that much threads by having the recNo, seatNo. Inside the run method call lock(recNo), modify(seatNo), unLock(recNo) dont forget to use yeild() inside ur run().
In ur DataImpl's modifyRec() have a sleep(2000) as the first statement(For testing purpose alone).
Once the above program is compleated start the client program.
Here my modifyRec will return true(boolean var)
once the seat is booked else false. So for the above test case only the last should return false. if it happen u can be sure about ur locking procedure.
Guys & Girls please give ur valid feedback about my test case.

regards,
-rameshkumar
ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
Hi Mark
I have included the following lines of code hope to check whether the available seats have changed from the time one read last time and if it changed create a new DataInfo object to modify the DB.
//read
int seats = new Integer(server.getRecord
(recNum).getValues()[columnTitles.length - 1]).intValue();
if ( ! ( seats== availableSeats ) ) {
seats = seats - booking;
modifyData[columnTitles.length-1] = new
Integer(availableSeats).toString();
dataRecord = new DataInfo(recNum,
description, modifyData);
}
Thanks
Ravi
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

Ok ravindra. Do you have a Facade class for your client to access your Data interfaces? Maybe this code is in that class.
if ( ! ( seats== availableSeats ) )
So this only says if the seats don't equal the available seats. I am getting confused here.
I think what you are doing is you have the available seats variable already filled that you used to check to make sure they didn't try to book more seats than are available, then check if anything has changed, if nothing has changed then book.
What about if it has changed, but they are still trying to book seats that are less than what's available? For example
20 seats available
I try to book 5 seats.
The available seats changed since the last time I read. so now there are only 15 seats available, well I should still be able to book my 5 seats.
Mark
ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
Hi Mark
Say there are 20 available seats and I tried to book i.e
available seats = 20
booking = 5
Before modifying , I get the latest available seats , I found that now the available seats have changed to 15 because someone booked 5 seats now
seats = 15 ( latest available seats )
available seats = 20 ( previous available seats )
so I determine that !(seats == available seats) gives true.
so I reduce 5 from the current # of available seats i.e
seats = seats - booking i.e
seats = 15 - 5 = 10
and assign this number to the latest available seats in the DB.
if the available seats has not changed from the last read then I proceed as usual.
Thanks
Ravindra
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

I'm sorry, but it still doesn't make sense. I suggest writing down all the different possiblities, and making sure that your code will work.
example

Now in those four cases will you code work?
Mark
ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
Hi Mark
I have the following if clause to check whether the # of seats required is more than the # of seats available.
// if available seats is less than the seats required then do nothing
if ( availableSeats < booking ) {
System.out.println("Sorry, You can't book more than what is available");
return;
}
I hope now I am on right track.
Thanks
Ravindra
 
Don't get me started about those stupid light bulbs.
 
subject: lock & unlock