• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

lock & unlock

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic