File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lock and unlock

 
ravi janap
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am in the design phase of FBN application.
This is what I have in my mind as regards lock / unlock of record.
When I start my server for the first time , I read all the record numbers into a collection. I will mark all these records as "UNLOCK" .
If I a client request to modify a record then the client marks the record as "LOCK" so that if a client request comes to modify this record and the client finds the record as "LOCK" then he would wait in a while loop . As soon as the client who has locked the record has finished with the job then he would release the lock and , mark the record as "UNLOCK" and call notify method so that the client in the queue would now try to make the request for the record. Now he finds the record as "UNLOCK" and he goes ahead with his job.
How does it sound?
I would also like to know whether we need to implement the lock / unlock in local mode?
 
Kalichar Rangantittu
Ranch Hand
Posts: 240
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I start my server for the first time , I read all the record numbers into a collection. I will mark all these records as "UNLOCK" .
IMHO: You need to ask yourself some question in order to decide the choices you make for example:
1. Do I really need to read in all the records?
2. What if some records are never user?
3. What if the database gets larger will it affect the performance?
4. What am I gaining by reading in all records?
5. If I do read in all records, where will they be stored?
6. Do I need to read in all records or can I simply put a lock on the record number in some sort of map?
Once you examine your questions, the answers will bring forth your design choices and confidence in your solution. Good luck
 
Rajesh Matti
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
janapareddy- For a small project like this, I think your choice isn't bad. If the database gets big, then the current design of the Sun's database isn't efficient anyway. But, as Kalichar mentioned, you may want to base the locks on the clients' request rather than on the number of records in the database, there by delaying the allocation of memory for the lock till there is actually a request (kind of lazy initialization).
Hope that helps,
-Rajesh
 
ravi janap
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code for lock and unlock is as follows :
public class DataServer extends UnicastRemoteObject implements RemoteDataInterface
{
private ArrayList records = new ArrayList();
public void lock(int record) throws IOException, RemoteException {
try {
String element = ( String ) records.get(record);
if ( element.equals("LOCK") ) {
wait(); // causes the current thread to wait until it is notified
} else {
synchronized ( records ) // lock the record object
{
records.add(record, "LOCK");
}
}
} catch(Exception e) {
System.out.println( e.getMessage() );
}
}
public void unlock(int record) throws RemoteException {
records.remove(record);
synchronized (records) {
// notifyAll unblocks the threads that called wait on this object
// this method can only be called within a synchronized method or block
notifyAll();
}
}
} // end of class
Is my approach correct for locking the current record for manipulation ?
Thanks
Ravindra
 
ravi janap
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have modified my code :
public void lock(int record) throws IOException, RemoteException {
try {
String element = ( String ) records.get(record);
if ( element.equals("LOCK") ) {
suspendRequested = true;

while ( suspendRequested ) {
records.wait(); // causes the current thread to wait until it is notified
suspendRequested = false;
}
records.add(record, "LOCK");
} // end of if clause
else {
synchronized ( records ) // lock the record object
{
records.add(record, "LOCK");
}
} // end of else clause
} catch(Exception e) {
System.out.println( e.getMessage() );
}
} // end of lock
public void unlock(int record) throws RemoteException {
suspendRequested = false;
records.remove(record);
synchronized (records) {
// notifyAll unblocks the threads that called wait on this object
// this method can only be called within a synchronized method or block
records.notifyAll();
}
} // end of unlock
The client calls the lock method before he wants to update the data .
If there is a lock on the record then the thread started by the user is put in a wait state when the lock is removed by the user who has the lock on the record then he would call notify and the client presently in the wait state gets the lock and he can now update the record.
If there is no lock on the record then the user can get a lock on the record and update it and then release the lock.
Any comments ?
Thanks
-- Ravindra
 
ravi janap
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anybody verify my code as I am not very sure about Multi-Threading .
Thanks
-- Ravindra
 
Rajesh Matti
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
janapareddy - I have tried to give you some idea.
You will need to manage client Id (hint: may want to make the 'value' = client id in dbLock.put(key, value); There are other ways to do so, this is one of the ways. As far as client id goes, I settled for a remote data adapter to be client id.
This is not a tested code, so be nice to me !!!.
Good Luck,
-Rajesh
=============================================
Map dbLock = new HashMap();
public synchonized void lock(int record) throws IOException, RemoteException {
if(record == -1) {
lockDatabase();
}
else {
lockRecord(record);
}
}
private boolean existLocks() {
return dbLock.size() != 0;
}
private boolean isRecordLocked(int recordNo) {
return dbLock.contains(new Integer(recordNo)) ;
}
private boolean isDatabaseLocked() {
return isRecordLocked(-1);
}
private void doLockRecord(int recordNo) {
Integer key = new integer(-1) ;
dbLock.put(key, key);
}
public synchonized void lockDatabase() throws IOException, RemoteException {
while(existLocks()) {
wait();
}
doLockRecord(-1);
notifyAll();
}
public synchonized void lockRecord(int recordNumber) throws IOException, RemoteException {
while(isDatabaseLocked() || isRecordLocked(recordNumber)) {
wait();
}
doLockRecord(recordNumber);
notifyAll();
}
=============================================
[ February 27, 2002: Message edited by: Rajesh Matti ]
[ February 27, 2002: Message edited by: Rajesh Matti ]
 
Rajesh Matti
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code above, as is, may cause NullPointerException and ArrayIndexOutOfBoundsExceptions.
 
ravi janap
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh
Thanks for the response.
I shall review my code as per your suggestions.
-- Ravindra
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic