Forums Register Login

B&S: Locking

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Guys,

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
+Pie Number of slices to send: Send
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.

M
+Pie Number of slices to send: Send
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.



and




Greetings,

TK
[ October 23, 2004: Message edited by: Thomas Kijftenbelt ]
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 735 times.
Similar Threads
Locking and File IO
lock and synchronized method
NX: Bodgitt and Scarper File Access/Locking
Multi-threading programming: we do not need lock() unlock() at all
SCJD Exam with J2SE book : DvdFileAccess Constructor
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:40:44.