Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lockRecord method help

 
uzma ali
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am stuck in locking for last two weeks and come up with this method finally.

I would appreciate your suggestions



1)If my lock.lock(); statement is actually locking the record?
2) I was thinking to use recNo as the cookie which is unique and there is no point of generating a random cookie while recNo serves the purpose.
3)I am not checking the deleted records as the recNo received is the current one and over there it would be taken care of if the record is deleted or not
4)I am thinking to call this method from updateRecord function to acquire the lock and then finally do update and delete. will that be correct.

Please suggest

Thanks in advance

Uzma
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


1)If my lock.lock(); statement is actually locking the record?

i suggest that you test the method and see if lock.lock() locks the record or not, use intensive logging.


I was thinking to use recNo as the cookie which is unique and there is no point of generating a random cookie while recNo serves the purpose.

cookie is used to identify CLIENTS, recno can't be used to identify clients since there would be more than one client trying to access the same record, you need some way to identify clients.


3)I am not checking the deleted records as the recNo received is the current one and over there it would be taken care of if the record is deleted or not

in my case, if the record is deleted before locking i throw RecordNotFound. otherwise the user will get this exception later on when the OTHER client unlocks the record after deleteing it.


4)I am thinking to call this method from updateRecord function to acquire the lock and then finally do update and delete. will that be correct.

I believe that's how it is should be done.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Uzma

I've got a few ideas regarding this because I myself had some trouble while working on it.

I can see you are using a some sort of collection to hold all record locks. Nevertheless, I can see you are not synchronizing the access to your lockRecord() method. You know that while you are trying to lock a record, other clients might be unlocking other, so your collection may receive concurrent accesses and modifications. It is my understanding that, anyway, the lack of a synchronization block will make your wait() statement fail.

Therefore, I would suggest that you synchronize the access to your record lock collection as the first step in improving your algorithm.

Another possible bug in your algorithm is that the current client may have already requested a lock on this record. That would the client requesting a lock twice, therefore, if your record is lock by the current client, this method will wait forever, because you say it must wait while the record is contained in the list of locks, and it will never be unlocked, because the current client is waiting for lock to be release, and he already has the lock.

Therefore, I would suggest that you improve your method by means of checking if the current client has the lock before attempting to acquire the lock again.

And finally, as suggested by someone else before, I recommend you the use of a logger (java.util.logging) to keep track of what your locking mechanism is doing.

I would suggest somewhat like this:

 
uzma ali
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Musab Al-Rawi, Edwin Dalorzo for taking out time to reply me

I can see you are using a some sort of collection to hold all record locks. Nevertheless, I can see you are not synchronizing the access to your lockRecord() method


I actually used Vector as my collection so I don't think there is need to use synchronized. what do you say?

regards

Uzma
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the Vector is a synchronized collection, so it might work to ensure only one thread at the time is reading from it or writing to it. Nevertheless, you are still required to put the current thread to wait if the lock is currently held by another client, therefore you will still need a synchronized block, because wait() cannot be invoked outside a synchronized block, and your vector will not do that for you.

Another alternative instead of using legacy collections could be to use a synchronized collection. You can turn any collection into a synchronized collection using the Collections.syncrhonizedList() method.

Just out of curiosity, what are you storing in your Vector?

I ask this question because I actually use a Map to hold the record locks: a record number mapped to a cookie ID.

For instance:

record 1 is locked by client 10092121456
record 2 is locked by client 29602423654

And so on, and so on.

Since vector does not offer a mapping mechanism I was wondering what type of object you are storing in it. Perhaps some kind of class that holds the record number and the lock id? As I told you, this is just out of curiosity, because I had already implemented my lock mechanism long ago and it works pretty well, but I would like to understand you design better, because while working on the lock mechanism I tested many ideas, some with more or less success and my experience in that field might help you if you are also testing one those that I had.
[ September 20, 2007: Message edited by: Edwin Dalorzo ]
 
uzma ali
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses

Edwin you are true. I was trying to use only recNo as the locking record Id but now I am clear that as recordCookie is required too so now I am using the Map.

Now what I did is:

the following method is in data.java which implements DBAccess interface

code:
--------------------------------------------------------------------------------

public void updateRecord(long recNo, String[] data, long lockCookie)throws RecordNotFoundException, SecurityException { lockCookie = lockRecord(recNo); database.updateRecord(recNo,data,lockCookie); unlock(recNo,lockCookie); }

--------------------------------------------------------------------------------



Then I have a lockManager class which returns a cookie to be used in the above method

code:
--------------------------------------------------------------------------------

public long lockRecord(long recNo) { lock.lock();return cookie;}

--------------------------------------------------------------------------------


I have another class DataFileAccess which gives the detail methods of Data.java


code:
--------------------------------------------------------------------------------

public void updateRecord(long recNo, String[] data, long lockCookie)throws RecordNotFoundException, SecurityException {//finally updates the database}

--------------------------------------------------------------------------------



If this is correct to call lock,update and then unloack from updateRecord method from Data.java

Please comment and any help is appreciated

uzma

Can you suggest me now if this is the path I should be following. I am pretty new in Java and might be I am just asking very trivial things.

any help is appreciated

Uzma
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic