File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes 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 "Locking" Watch "Locking" New topic
Author

Locking

Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Here's what I came up with in respect to locking.

[ Please see this thread for a more complete locking scheme that I finally settled on.]

(By the way, whenever I wrote HashSet, I meant HashMap.)

a) Synchronize the HashSet. (threads may wait on that.)
b) Check for record being locked in it. If yes, wait() (lock on HashSet
released.)
c) When the record is unlocked, lock it again, this time by different Data object using the calling thread (i.e. client) lockCookie value. When checking, Data object acquires lock on HashSet automatically. Release lock on HashSet.
d) Modify the database file. Here I must guarantee that a record is unlocked only after database file is modified.
e) Synchronize the HashSet. Unlock the record I locked. Release the lock on the HashSet.

So in order to accomplish database access safely, three locks are in play:

lock 1 is the lock on the Data object.
lock 2 is the lock on the HashSet.
lock 3 is the lock put in the HashSet. This lock is purely logical: no lock is granted in the sense the synchronized keyword grants it. But this lock is accomplished by putting a key/value pair in the HashSet (record number or nubmer of bytes offset to the record, and lockCookie as the value) so that other threads, checking upon the presence of this record number, would abstain from modifying the record in the database.

That's basically what I came up with in regard to locking.

Does it look like it could work?
[ July 30, 2004: Message edited by: Anton Golovin ]

Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

Hi Anton,

c) When the record is unlocked, lock it again, this time by different Data object using the calling thread (i.e. client) lockCookie value.


I have not understood where your lockCookie is coming from - is this something you are generating based on the thread, or something that is being generated client side, or something else?

d) Modify the database file. Here I must guarantee that a record is unlocked only after database file is modified.


I would have thought that the only thing you must guarantee is that a record must be locked when you attempt to modify it. Why are you forcing some modification to occur after locking?

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
The lockCookie variable will be generated to uniquely identify each client. How to generate it, I have not thought yet. I think in the DataImpl class, I will make a method to create a cookie and return it to the client for the client to use. I will keep a list of clients in the DataImpl static Set structure, as well as the state of any numbering scheme I will be using.

The problem I am thinking about, though, is to make sure that only one of the many Data objects can access the database file. It would be simple with just one Data object, but the delay for the client would be greater. My specifications say I may assume that I would not need to lock the database file itself, but short of locking the file itself, I do not see how I can assure safe reading and writing on the file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Locking
 
Similar Threads
Concurrency issue with create(...)/createRecord(...) [URLyBird]
consumes no CPU cycles?
My Locking...Advice needed...
SCJD - 370 out of 400
Data Locking with a DB File.