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 bookFlight Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "bookFlight" Watch "bookFlight" New topic
Author

bookFlight

ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
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


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

Joined: Feb 05, 2001
Posts: 17249
    
    6


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


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

Joined: Nov 04, 2000
Posts: 389
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

Joined: Nov 04, 2000
Posts: 389
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

Joined: Nov 04, 2000
Posts: 389
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Nov 04, 2000
Posts: 389
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
 
 
subject: bookFlight
 
Similar Threads
Testing - Max's Book
DBTester and DBTestRunner in Max's book
Help needed JVM crashes while running the following program
lock & unlock
Preliminary test fails! Threads problem