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 DBTester and DBTestRunner in Max's book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "DBTester and DBTestRunner in Max Watch "DBTester and DBTestRunner in Max New topic
Author

DBTester and DBTestRunner in Max's book

Alan Mac
Greenhorn

Joined: Aug 03, 2005
Posts: 9
Hi,

This will probably only make sense if you're using Max'x book, so please bear with me...

I've set up the example project (Denny's DVDs) in Max's book and have compiled and run it successfully (both locally and in RMI mode). I'm currently looking at the testing code in the sampleproject.test package.

I've run the sample test as provided and the results look fine i.e. 5 rentals, 10 returns, number of available copies has increased by 5. I then jacked up the numbers, still fine.

However when I interspersed returns and rentals a la...



I get anomolous results i.e. number of available copies increases? Has anybody noticed this before? Is there a flaw in the multithreading code in Denny's?

Thanks,
A.

[Andrew: removed indentation on source code as it is not needed, and the screen width was annoying me]
[ August 04, 2005: Message edited by: Andrew Monkhouse ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11477
    
  94

Hi Alan,

I believe the problem is that even though you have interspersed your rentals and returns, these are threads at the control of the JVM thread scheduler. So there is no guarantee that a return will happen before a rental.

When I look at the output of a test run with your code, I see:We start of with lines 1,2 and 3 all returning rentals (since copies in stock are incrementing). Line 4 is our first rental, followed by a return, followed by a rental, followed by several returns.

Skipping forward to line 18 we see a return bringing our copies in stock up to 7. We then follow with 13 returns (up to line 31). However the setRented() method of the DVD class will not allow negative rented numbers:With the result that lines 26 - 31 show that copies in stock remain at zero. But with the threads returning the DVDs that previously did not exist the final number of DVDs in stock is very likely to end up greater than the starting value.

A better test would be for DBTester to do both the rental and the return (with a random delay between the rental and the return while holding zero locks), but only returning the DVD if it was allowed to rent in the first place (or alternatively, if it was unable to rent the DVD, wait for a while and try again). DBTestRunner would also have to be modified to accomodate the new DBTester.

I leave these modifications as an exercise for you .

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Alan Mac
Greenhorn

Joined: Aug 03, 2005
Posts: 9
Hi Andrew,

Thanks for the reply.


I believe the problem is that even though you have interspersed your rentals and returns, these are threads at the control of the JVM thread scheduler. So there is no guarantee that a return will happen before a rental.


I realised this, but failed to take account of the setRented() method of the DVD class not allowing negative rented numbers, as you've described.

I made a temporary change to the code to allow negative rented numbers and it now appears fine (I know changing the code to suit the test isn't exactly best practise, but it proves a point!).

Thanks again,
A.
[ August 08, 2005: Message edited by: Alan Mac ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11477
    
  94

Hi Alan
I know changing the code to suit the test isn't exactly best practise...
Well it is best practice if you follow the Extreme Programming Test Driven Development concepts. (However you are supposed to have a valid test that you making your code match )

Regards, Andrew
John Huntingdon
Greenhorn

Joined: Dec 13, 2009
Posts: 3
I get the following error when trying to execute DBTestRunner from "SCJD Exam with J2SE 5 Platform":

Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sampleproject.sockets.DvdSocketClient.initialize(DvdSocketClient.java:300)
at sampleproject.sockets.DvdSocketClient.<init>(DvdSocketClient.java:70)
at sampleproject.sockets.DvdConnector.getRemote(DvdConnector.java:37)
at sampleproject.test.DBTester.<init>(DBTester.java:50)
at sampleproject.test.DBTestRunner.startClients(DBTestRunner.java:54)
at sampleproject.test.DBTestRunner.<init>(DBTestRunner.java:41)
at sampleproject.test.DBTestRunner.main(DBTestRunner.java:29)

Any comments would be appreciated; I have tried a number of things to fix this. I get this running in Eclipse or from the command line.

Thanks in advance!

John
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5402
    
  13

Hi John,

First of all: welcome to the JavaRanch!

This is a thread from more than 4 years ago. Don't wake up a dead (zombie) thread, just create your own thread and ask your question there. Andrew is regularly visiting this forum (if he has already an internet connection after moving ), so he (or some other rancher) will be glad to answer your question.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DBTester and DBTestRunner in Max's book