jQuery in Action, 3rd edition
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S: Locking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "B&S: Locking" Watch "B&S: Locking" New topic

B&S: Locking

Mark Thomson

Joined: Jul 20, 2004
Posts: 15
I have a couple of questions regarding locking

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.

Your thoughts on the above will be appreciated.

Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
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
Mark Thomson

Joined: Jul 20, 2004
Posts: 15
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?
Mark Thomson

Joined: Jul 20, 2004
Posts: 15

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.

Hope this clarifies my last post.

Thanks in advance
Max Habibi
town drunk
( and author)

Joined: Jun 27, 2002
Posts: 4118
Hi Mark,

I'd say test the living heck out of your application, including the use of remote clients. If it works, then you're golden. Remember, performance is a non-issue, but correctness is everything.


Java Regular Expressions
Thomas Kijftenbelt
Ranch Hand

Joined: Feb 13, 2002
Posts: 73
Hi guys,

I actually took the following approach:

- I use a static hashmap to implement the record locking

- I implemented my data class as singleton; in that way I avoid that multiple data instances are trying to manipulate the same file.

Although I am using NIO (FileChannel) I am still a bit worried about dirty reads; therefore I synchronize on the FileChannel when reading / writing, i.e.



[ October 23, 2004: Message edited by: Thomas Kijftenbelt ]
I agree. Here's the link: http://aspose.com/file-tools
subject: B&S: Locking
jQuery in Action, 3rd edition