Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Locking/Unlocking code

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am doing following assumption:
Following way client code will be executed:
a. DbServer dbServer = getAccessToDBServer();
b. Kookie kookie = dbServer.lockRecord(recordNos);
c. dbServer.updateRecord(Data,recordNos,kookie);//update/delete
d. dbServer.unlockRecord(recordNos,kookie);

In other words client needs to call lock/unlock exclusively before performing mutable operations.

Here is the code:

!!!DBServer api's are not synchronized!!!

/**
* Executes record locking protocol. It maintains locking information on record.
*/
public class LockManager {
private HashMap<Long,Kookie> map;
public LockManager() {
}
/**
* Acquires lock over record. If record is not available for locking executing
* thread gives up the CPU till record is available for lock.
* @param recordNos
* @param clientKookie
*/
public void lock(long recordNos,Kookie clientKookie){
Kookie kookie = null;
while(true){//need to wait till lock is acquired
synchronized(map){
if ((kookie = map.get(recordNos)) == null){
clientKookie.setKookieBusy(true);
map.put(recordNos,clientKookie);
return;
}
}
synchronized(kookie){
try {
kookie.wait();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}

/**
* Releases lock acquired on passed record.
* @param recordNos
*/
public void unlock(long recordNos){
Kookie kookie = null;
synchronized(map){
kookie = map.get(recordNos);
map.remove(recordNos);
}
synchronized(kookie){
try {
kookie.notifyAll();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

/**
* Checks if record is locked by passed kookie;
* @param recordNos
* @param aKookie
* @return
*/
public boolean isRecordLocked(int recordNos,Kookie aKookie){
synchronized(map){
Kookie kookie = map.get(recordNos);
return aKookie.equals(kookie);
}
}
}

public class DBServer{
...
.
.
public long lockRecord(long recordNos) throws RecordNotFoundException {
Kookie kookie = KookieHolder.reserve(recordNos);
lockManager.acquireLock(recordNos,kookie);
return kookie.kookieValue();
}

public void unlock(long recordNos, long kookie) throws SecurityException {
KookieHolder.unreserve(recordNos,new Kookie(kookie));
lockManager.releaseLock(recordNos);
}
}

'Kookie' is a object on which threads are synchronized. Kookie has longvalue which actually is the kookie value & returned to client.

Friends let me know your thoughts on it.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic