aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Max. Denny's DVD Project: DVDDatabase class doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Max. Denny Watch "Max. Denny New topic
Author

Max. Denny's DVD Project: DVDDatabase class doubt

Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hello Ranchers,
I have couple of doubts in the DVDDatabase class from the Max. Denny's DVD sample project. Help is greatly appreciated.
1. The first one is regarding the retrieveDVD methods.
/**
* This method retrieves a DVD object from the database.
* then the DVD will be created.
*
* @param upc The unique ID of the DVD to retrieve.
* @return The requested DVD.
* @throws IOException Indicates there is a problem accessing the data.
* @throws ClassNotFoundException Indicates the DVD class defination cannot be found.
*/
private DVD retrieveDVD(String upc) throws
IOException,
ClassNotFoundException{
return retrieveDVD(upc,recordExtention);
}
/**
* This method retrieves a DVD object from the database.
* then the DVD will be created.
*
* @param upc The unique ID of the DVD to retrieve.
* @param fileExtension the file extension used to store the DVD.
* @return The requested DVD.
* @throws IOException Indicates there is a problem accessing the data.
* @throws ClassNotFoundException Indicates the DVD class defination cannot be found.
*/
private DVD retrieveDVD(String upc, String fileExtension) throws
IOException,
ClassNotFoundException{
DVD retVal = null;
//get the path to the object's serialized state.
String filePath = dbName+"/"+ upc + fileExtension;
FileInputStream fis = new FileInputStream(filePath);
//Read in the data from the object
ObjectInputStream ois = new ObjectInputStream(fis);
retVal = (DVD)ois.readObject();
ois.close();
fis.close();
fis =null;
return retVal;
}
In the above code recordExtention is the static member variable of the class. I do not understand why Max. has used two methods, I mean if we are sending the member variable as an argument to call another retrieve method, why can't the same functionality be achieved in the first retrieve method instead of calling other one. Is there something I am missing here. Please help me in understanding. Thanks.
2. The second one is regarding the rent method.
/**
* Rents a DVD.
*
* @param upc The id of the DVD to rent
* @return Returns true if the DVD was found and rented.
* @throws IOException Indicates there is a problem accessing the data.
* @throws ClassNotFoundException Indicates the DVD class defination
* cannot be found.
* @throws InterruptedException Indicates the DB thread has been
* interrupted.
*/
public synchronized boolean rent(String upc) throws IOException,
ClassNotFoundException,
InterruptedException{
boolean retVal = false;
// This call is a problem
// It does NOT require that a record is locked
// Causes abnormal results
DVD dvd = getDVD(upc);
if (dvd != null){
dvd.setRented(true);
retVal = modifyDVD(dvd);
}
return retVal;
}
This method is throwing three exceptions IOException, ClassNotFoundException, InterruptedException. What I am not able to understand is why this method is throwing the InterruptedException. Actually this method is calling three methods getDVD() & modifyDVD() of DVDDatabase object, setRented() of DVD object which are throwing (IOException, ClassNotFoundException), (IOException) and (no exceptions) respectively. So how is this InterruptedException intrepreted in the picture?
I really appreciate help in understanding. Thanks.
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hello Ranchers, I have one more question to ask. In the rent method Max. has commented three lines as below.
// This call is a problem
// It does NOT require that a record is locked
// Causes abnormal results
Can anyone please clarify this one in detail? Thanks alot.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Satish Avadhanam:
Hello Ranchers,
I have couple of doubts in the DVDDatabase class from the Max. Denny's DVD sample project. Help is greatly appreciated.
1. The first one is regarding the retrieveDVD methods.
/**
* This method retrieves a DVD object from the database.
* then the DVD will be created.
*
* @param upc The unique ID of the DVD to retrieve.
* @return The requested DVD.
* @throws IOException Indicates there is a problem accessing the data.
* @throws ClassNotFoundException Indicates the DVD class defination cannot be found.
*/
private DVD retrieveDVD(String upc) throws
IOException,
ClassNotFoundException{
return retrieveDVD(upc,recordExtention);
}
/**
* This method retrieves a DVD object from the database.
* then the DVD will be created.
*
* @param upc The unique ID of the DVD to retrieve.
* @param fileExtension the file extension used to store the DVD.
* @return The requested DVD.
* @throws IOException Indicates there is a problem accessing the data.
* @throws ClassNotFoundException Indicates the DVD class defination cannot be found.
*/
private DVD retrieveDVD(String upc, String fileExtension) throws
IOException,
ClassNotFoundException{
DVD retVal = null;
//get the path to the object's serialized state.
String filePath = dbName+"/"+ upc + fileExtension;
FileInputStream fis = new FileInputStream(filePath);
//Read in the data from the object
ObjectInputStream ois = new ObjectInputStream(fis);
retVal = (DVD)ois.readObject();
ois.close();
fis.close();
fis =null;
return retVal;
}
In the above code recordExtention is the static member variable of the class. I do not understand why Max. has used two methods, I mean if we are sending the member variable as an argument to call another retrieve method, why can't the same functionality be achieved in the first retrieve method instead of calling other one. Is there something I am missing here. Please help me in understanding. Thanks.

I don't have a copy of the book handy, but it's fairly common practice to have overloaded convenience methods in classes. I'm guessing that we either had, or anticipated, an other usage which would have required searching for another extension.
Remember, this is just a sample project: not all the pieces will apply to the SCJD, or would it be appropriate if we had.

2. The second one is regarding the rent method.
/**
* Rents a DVD.
*
* @param upc The id of the DVD to rent
* @return Returns true if the DVD was found and rented.
* @throws IOException Indicates there is a problem accessing the data.
* @throws ClassNotFoundException Indicates the DVD class defination
* cannot be found.
* @throws InterruptedException Indicates the DB thread has been
* interrupted.
*/
public synchronized boolean rent(String upc) throws IOException,
ClassNotFoundException,
InterruptedException{
boolean retVal = false;
// This call is a problem
// It does NOT require that a record is locked
// Causes abnormal results
DVD dvd = getDVD(upc);
if (dvd != null){
dvd.setRented(true);
retVal = modifyDVD(dvd);
}
return retVal;
}
This method is throwing three exceptions IOException, ClassNotFoundException, InterruptedException. What I am not able to understand is why this method is throwing the InterruptedException. Actually this method is calling three methods getDVD() & modifyDVD() of DVDDatabase object, setRented() of DVD object which are throwing (IOException, ClassNotFoundException), (IOException) and (no exceptions) respectively. So how is this InterruptedException intrepreted in the picture?
I really appreciate help in understanding. Thanks.

Hmm...isn't there some locking going on there that might throw an InterruptedException? Like the actual locking?
M


Java Regular Expressions
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hello Max,
Thanks for the reply. I am sorry to ask like this, but I really could not figure it out of how the locking works there. I mean which method call is doing the locking there? Can you please explain in a couple of more sentences verbally?
Appreciate your patience, thanks.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Hm...I'm guessing Modify is doing the locking. Look into it, and let me know what's going on in there.
M
[ February 11, 2004: Message edited by: Max Habibi ]
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Thanks Max. Here is the code for modifyDVD().
public synchronized boolean modifyDVD(DVD dvd) throws IOException{
return persistDVD(dvd);
}
This is calling persistDVD(), and the code is
private boolean persistDVD(DVD dvd) throws IOException{
boolean retVal=false;
//open a FileInputStream associated with the data
//notice that if the DVD does not already exist,
//it will be created.
String filePath = dbName+"/"+ dvd.getUPC() + recordExtention;
FileOutputStream fos = new FileOutputStream(filePath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
//Read in the data from the object
oos.writeObject(dvd);
//close all references
oos.close();
fos.close();
fos =null;
oos = null;
retVal = true;
return retVal;
}
--------------
If modifyDVD() is locking then we are calling it in other methods which are not throwing InterruptedException. Please clarify.
Thanks for your help, Max.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Is this the before or after version of the code?
M
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Max, this is version 2. The final sample project code.
Thanks.
Raj Shekhar
Greenhorn

Joined: Jan 26, 2004
Posts: 25
Max,
The signature of the method in version1 of the project also has the InterruptedException in it and that could be the reason for the confusion.
Raj
Raj Shekhar
Greenhorn

Joined: Jan 26, 2004
Posts: 25
Satish,
The following is the code of the version2 of the project. I think the relaseDVD method has a wait() method in it, which throws the Interrupted Exception.
HTH
Raj
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hey Raj, thanks man.
Here is my version 2 code for rent method.
public boolean rent(String upc) throws IOException,
ClassNotFoundException,
InterruptedException{
boolean retVal = false;
DVD dvd = getDVD(upc);
if (dvd != null){
dvd.setRented(true);
retVal = modifyDVD(dvd);
}
return retVal;
}
I don't understand why there is a variation in the code for the same version.
Max, sorry for the confusion. I think its because of the variations in the same versions of code. Can you help me why there is change in the code for the same versions, please? However, I will download the latest version and work on it.
And Raj, from your code, it is quite appropriate and also the rent method must throw the InterruptedException. Thanks alot for your help.
Thanks once again.
Raj Shekhar
Greenhorn

Joined: Jan 26, 2004
Posts: 25
Satish,
I checked again and I think this is what is happenning.
There are two rent methods:
i. One in DVDDatabse and the other one in
ii. DVDDBAdapter.
The locking is handled in DVDDbAdapter class and hence the rent method there throws the Interrupted Exception. This is the code I pasted. You are checking the rent method in the DVDDatabase class which does not handle the locking and hence the InterruptedException is redundant here. Max, your thoughts on this??
Raj
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hey Raj,
that's right. But if we consider the rent method alone in DVDDatabase class, why is it throwing the InterruptedException? I mean as far as I understood there is no locking going on in the rent method of the DVDDatabase class, right? And so it should not throw InterruptedException, atleast that's what I understood.
Please correct me if I'm wrong. Thanks.
And Raj, would you also look at my other post on GUIController please. Thanks.
[ February 11, 2004: Message edited by: Satish Avadhanam ]
[ February 11, 2004: Message edited by: Satish Avadhanam ]
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
I've have to actually go home and look over this stuff now<groan/>. I'll try and get something up by 2/12 evening.
M
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Thanks Max. Will be waiting for your response
Thanks.
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Max, is it possible to explain the reason now?
Appreaciate your help, thanks.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Hi Satish,
Thanks for being patient: I've just been swamped. To answer your question: this was part of the teaching process of the project. That's why there's a comment in the method that states

// This call is a problem
// It does NOT require that a record is locked
// Causes abnormal results

The fact that the method throws an InterruptedException is a hint on how to fix the problem.
HTH,
M
[ February 16, 2004: Message edited by: Max Habibi ]
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Thanks Max. Appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Max. Denny's DVD Project: DVDDatabase class doubt
 
Similar Threads
locking in Denny's DVDs example
Max, please comment your book.
Max. Denny's DVD Project: GUIController class doubt
Locking issues in Max's book sample project - Denny's DVD store
Can't declare methods to throw java.rmi.RemoteException