I'm following Habibi's book pretty closely, and admittedly synchronization is not my strongest point. My question has to do with the purpose of synchronizing the entire method (let's say the update method, for example) of the database class, and then having clients lock before calling update and unlocking afterwards.
The difference between my implementation and the book's is that my database is a singleton, whereas in the book there can be multiple database objects for the same DB. But I'm still not entirely clear why both the method is synchronized and lock/unlocks are placed around the method. It seems to me that only one of those is necessary.
If two update threads are hitting the DB, isn't it true that if update is synchronized, one thread will have to complete before the other one starts? Therefore, what is the purpose of locking a record before updating it?
I am guessing that there IS a purpose and I just don't see it, so hopefully someone can enlighten me =) Thanks!