Anton Golovin ([email protected]) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
Originally posted by Anton Golovin:
Good day!
I finally figured out the complete locking system. Locking at the Data class level does not do all the trick. There needs to be supporting locking at the business logic level for certain methods.
If your locking at the Data method is fine, consider this:
in your bookRoom() method in your business logic, do you check that a room is not booked? This functionality properly belongs here. After you check it is not booked and that it is bookable (within 48 hours, say), you book it. But what has happened between your reading the record from your cache/datafile, and your writing the record to your cache/datafile? The record could have been manipulated in a variety of ways, and there is no guarantee that the record you are writing is the same.
So if you do not implement some sort of supporting locking in your business logic, you did not implement locking for a 100% score.
Basically, it is solved by synchronizing on your Data class in your business logic, where appropriate.
Please comment.
Anton Golovin ([email protected]) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
Originally posted by Anton Golovin:
Here's the code I was referring to:
synchronized(data) {
SCJP 1.4, SCJD
I think this is a better solution than locking the entire Data class. That way you can have concurrent reads on different records in the database.An alternative would be to move the read method within the lock/unlock
Can you give me an example of when you would need to do this?but then I also may have to call the read method for things that do not require this type of checking
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Mike:
You have synched on data for the whole book method.
That means no one else can do anything to any other record during the whole of your very time consuming book method.
I'm sure you do not need to nail it down this much.
Peter:
The ability to lock a record at the business level is exactly what the record locking mechanism does. The "book" sequence is {lock, read, compare, update, unlock}.
Anton:
An alternative would be to move the read method within the lock/unlock, but then I also may have to call the read method for things that do not require this type of checking, and then I would need to be sure read fetches current data.
Consider Paul's rocket mass heater. |