Java Reference
In-Depth Information
follows the protocol of reserving a DVD before modifying it), retrieves the DVD (to ensure we
have the latest copy), checks that there are enough DVDs available (and if so it removes one),
and saves the modified DVD back to the database.
private int rentDvd(String upc) throws Exception {
if (db.reserveDVD(upc)) {
try {
DVD dvd = db.getDVD(upc);
int copiesInStock = dvd.getCopy();
if (copiesInStock > 0) {
copiesInStock--;
log.info(getName() +
" -> (Rent) " +
"Copies in stock = " + copiesInStock );
dvd.setCopy(copiesInStock);
db.modifyDVD(dvd);
return RENTAL_SUCCESS;
} else {
log.info(getName() + " 00 (No stock)");
return RENTAL_OUT_OF_STOCK;
}
} finally {
db.releaseDVD(upc);
}
} else {
log.info(getName() + " XX (Timeout)");
return RENTAL_TIMEOUT;
}
}
Similarly the returnDvd method reserves the DVD so no other client can modify it,
retrieves the DVD (to ensure we have the latest copy), increases the number of copies, and
then saves the modified DVD.
private void returnDvd(String upc) throws Exception {
if (db.reserveDVD(upc)) {
try {
DVD dvd = db.getDVD(upc);
int copiesInStock = dvd.getCopy() + 1;
dvd.setCopy(copiesInStock);
log.info(getName() +
" <- (Return) Copies in stock = " +
copiesInStock );
db.modifyDVD(dvd);
} finally {
db.releaseDVD(upc);
}
}
}
Search WWH ::




Custom Search