yes. But why do you like to use the hashCode()?
You could use the DBServerImpl instance itself as the key in the Collection.
The object itself guarantees a unique client.
LockManager handles all the locking for all the clients, but the DBServer keeps track of records that that client has locked, so that when you call unlock, you can check it to make sure that it has the lock in the first place.
synchronized public void unlock(int record) {
RecordLockManager.unlock(record, this);
//System.out.println("unlock() called from " + this.hashCode());
}
public class RecordLockManager {
private static HashMap locks = new HashMap();
public static void lock(int recNum, Object client) throws InterruptedException {
synchronized(locks) {
Integer record = new Integer(recNum);
while (locks.containsKey(record))
locks.wait();
locks.put(record, client);
//System.out.println("client " + client + " locked record " + recNum);
}
}
public static void unlock(int recNum, Object client) {
synchronized(locks) {
Integer record = new Integer(recNum);
if (locks.containsKey(record) && locks.get(record) == client) {
locks.remove(new Integer(recNum));
System.out.println("client " + client + " unlocked record " + recNum);
} else {
System.out.println("****** client " + client + " tried to unlock record " + recNum);
}
locks.notifyAll();
}
}
}
In the LockManager you are passing a reference to the Connection Object, which is fine.
If you are passing the Data reference it would be the same as all the other clients. Each client does not have it's own Data Object, but a reference to the one and only instance of Data.
SCJP, SCJD, SCWCD<br />"Meekness is not weakness, but power under control"
Maybe I am reading the whole thing wrong, but my understanding is that if every client has its own DBServer there will never be any contention between clients.
SCJP, SCJD, SCWCD<br />"Meekness is not weakness, but power under control"
SCJP, SCJD, SCWCD<br />"Meekness is not weakness, but power under control"
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|