//contains locked records private Vector r_lock = new Vector(); //Global declaration public void lock(int record) throws IOException { synchronized(r_lock) { while(r_lock.contains(new Integer(record))) { try { wait(); } catch(InterruptedException e) { throw new IOException("lock failed"); } } r_lock.add(new Integer(record)); }
} /** * Unlock the requested record. Ignored if the caller does not have * a current lock on the requested record.The record that was locked * is removed from the Vector r_lock and all threads are notifyed. */ public void unlock(int record) { synchronized(r_lock) { if(r_lock.contains(new Integer(record))) { r_lock.remove(new Integer(record)); } r_lock.notifyAll(); } } Now My question is how to lock the whole database if -1??? and then how to unlock it Thanks Lisa
Cleland Early
Ranch Hand
Joined: Apr 16, 2001
Posts: 38
posted
0
How about modifying your lock code in this fashion: while((r_lock.contains(new Integer(record))) | | (r_lock.contains(new Integer(-1)))) You shouldn't have to change your unlocking code.
Lisa Foster
Ranch Hand
Joined: Feb 28, 2001
Posts: 116
posted
0
Thanks Alot if I do this as you mention above will it lock the whole Database???
Thanks Lisa
lokesh ks
Greenhorn
Joined: Apr 29, 2001
Posts: 5
posted
0
Hi Lisa, Vectors are by default synchronised objects and again your code synchronises on the vector object is it OK? Is it not a overhead ?? Bye, Lokesh
Cleland Early
Ranch Hand
Joined: Apr 16, 2001
Posts: 38
posted
0
Adding the second condition to your while statement prevents any threads from continuing as long as r_lock contains -1, no matter which record they are trying to lock.
Lisa Foster
Ranch Hand
Joined: Feb 28, 2001
Posts: 116
posted
0
Thanks Very much I see the logic now. If you never unlock -1 no other threads can get in essentially blocking until you reboot. Thanks Lisa
stewchicken
Greenhorn
Joined: May 31, 2001
Posts: 3
posted
0
Originally posted by Lisa Foster: //contains locked records private Vector r_lock = new Vector(); //Global declaration public void lock(int record) throws IOException { synchronized(r_lock) { while(r_lock.contains(new Integer(record))) { try { wait(); } catch(InterruptedException e) { throw new IOException("lock failed"); } } r_lock.add(new Integer(record)); }
} /** * Unlock the requested record. Ignored if the caller does not have * a current lock on the requested record.The record that was locked * is removed from the Vector r_lock and all threads are notifyed. */ public void unlock(int record) { synchronized(r_lock) { if(r_lock.contains(new Integer(record))) { r_lock.remove(new Integer(record)); } r_lock.notifyAll(); } } Now My question is how to lock the whole database if -1??? and then how to unlock it Thanks Lisa
i think should use r_lock.wait() and r_lock.notifyAll() cos r_lock was synchorinized but not this.
Conor Allen
Ranch Hand
Joined: Apr 27, 2001
Posts: 32
posted
0
Have you considered blockinh the thread that gets the 'database lock' until all the threads updating records have finished with their locks? - If you do not then the DB lock thread can potentially corrupt a record to which another thread thinks that it has exclusive access Any thoughts? Conor
Brian Besterman
Greenhorn
Joined: Jun 07, 2001
Posts: 2
posted
0
I agree with Conor. One solution may be to use a flag to indicate that a client wants a full lock, which prevents any new locks. Once all the current locks are released, the client can obtain the full lock.
Conor Allen
Ranch Hand
Joined: Apr 27, 2001
Posts: 32
posted
0
OR .... Once a full db lock is granted then block in the lock method until all outstanding locks have been released ....
subject: Locking the whole Data base if -1 see code????