• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bookFlight

 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
There is something wrong with the implementation of my bookFlight method.
The DBTestRunner creates six different threads each of which book a specific number of seats. Now before running the
test the total # of seats on the flight xyz ( record # 14 )
was 48. After running the test, the total # of seats should have been reduced to 40 but I find that the total # of available seats on the
flight is 45 thus 5 bookings on this flight have failed.
Please look at the code for DBTestRunner and bookFlight method.


Any help is appreciated.
Thanks
Ravi
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I really can't tell.
But why are you assigning the modifyData array value for availableSeats as a String. Do you really want an int or Integer in that array element?
Mark
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark
The record is in the form of String[] .
So, I have it as a String but convert it into int or Integer for manipulation purpose.
Is my implementation of bookFlight method okay? I am doing a lock-read-modify-unlock. Should I need to change it. Is there some problem with it.
Thanks
Ravi
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark
If you look at the method in DataInfo class

What I am doing is directly modifying the seats field in the record and using that record to overwrite the previous record.
I hope what I did was correct. I have finished my submission however on running the test client , I found the problem area. I am sure that my submission will fail if I don't identify the problem area which could be either in lock-unlock or in read-modify.
Thanks
Ravi
Thanks
Ravi
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still wondering whether this problem is because of some thing wrong with my implementation of lock and unlock. Can you verify ?

Thanks
Ravi
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the requirements does it have the signature of lock to throw an InterruptedException? I'd suggest placing a try catch in the lock method aounr the wait() call, then catch the Interrupted Exception there, and outside the while do your add() and remove the notifyAll().
And what happens in the unlcok method if by accident a record number is passed that is not locked?
Other than those two suggestions, Yuo lock and unlock look exactly like mine do.
Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the bookFlight method you have seems to be ok. the logic is very similar to mine. The only difference is that I broke it apart into a few methods instead of just one.
Mark
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark
In the requirements does it have the signature of lock to throw an InterruptedException? I'd suggest placing a try catch in the lock method aounr the wait() call, then catch the Interrupted Exception there, and outside the while do your add() and remove the notifyAll().

I removed the InterruptedException in the method signature, I have used a try-catch and that solved all the problem. Now the booking is working perfectly. Thank you very much.
Why do I need to remove the notifyAll since after locking a record the waiting threads must be notified that the dbLock is free?

And what happens in the unlock method if by accident a record number is passed that is not locked?

To address this issue I have slightly modified the code to remove the lock on a record only after a check as given:

I hope this should fix the issue.
Thanks
Ravi
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do I need to remove the notifyAll since after locking a record the waiting threads must be notified that the dbLock is free?

Call it wierd, but I am thinking that when you lock a record, the other threads don't need to know. The ones that are waiting are waiting to see if they can get a record that is currently locked.
However, I am not sure if my thinking is entirely correct.
Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic