This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
1. Do i need to look after locking on a file access level as well as on a record level?
In the DVDDatabase example in the SCJD Exam book ( Habibi etc ) they implement a seperate DVDDatabase instance per client - in this way I can only see that records get locked ( using a static collection as a monitor and id holder ) not access to the file. Even though all methods in DVDDatabase are syncronized there are multiple instances of DVDDatabase so 2 clients could access two different records and update the database file at the same time. I am assuming that I shouldn't be allowing concurrent writes on the database file??
3. If I implement my database access layer using the Singleton pattern ( and making all methods synchronized ) then I can't see a need to lock individual records as only one thread will be able to access the database at one time.
I think you may want to reconsider the use of a singleton. If I read your post correctly, you will basically be allowing only one client access to the database at any time. Think of how slow this application would run with 20+ clients having to wait in line to get a peek at the database.
In my assignment (B&S 2.2.3), I am implementing a logical record lock, similar in concept to the one in Max's book. This allows concurrent access to the data, but not to individual records.
“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
Joined: Jul 20, 2004
The only part I can find from the assigment that details concurrent request states
" You may assume that at any moment, at most one program is accessing the database file"
I am going to take it that I'm ok for concurrent writes to the database file only when dealing with different records.
Has anyone had any problems with file corruption etc, when perfoming concurrent writes?
Joined: Jul 20, 2004
I just want to clarify what I am attempting to do. Your thoughts would be appreciated.
I am going to implement the following:
1. Each client has its own instance of a DBAdapter class. This class controls locking, access, unlocking. My Data classes methods are synchronized but I have multiple instances of my Data class so concurrent writes will be allowed as I am only locking on a static collection not the database file itself
2. I am using this static Collection to keep track of my locked records.
Now my big question is regarding the writes.
If I am locking at a record level, not a database file level then I will be allowing say 2 clients to update 2 different records at the same time. Is this how it should typically be implemented??
I am just worried about how concurrent writes ( concerning different records ) may affect the integrity of my database file.