I'm implementing Two-Phase Lock using JINI. I've done it by following algorithm definition. And in my implementation I have a few ArrayLists and HashMap to track which participants has committed or aborted transaction.
Every time I do join/commit/abort operations, my ArrayLists and HashMap are empty (no previous participants) and HashCode of my TransactionManager is always different. I spent 2 days looking for a problem and still can't understand why this is happening.
// here is the implementation of join method of my TransactionManager
private HashMap<Long, ArrayList<TransactionParticipant>> _transactions = new HashMap<Long, ArrayList<TransactionParticipant>>();
private ArrayList<TransactionParticipant> _participantTest = new ArrayList<TransactionParticipant>();
@Override
public synchronized void join(long trxId, TransactionParticipant tp, long crashCount) throws UnknownTransactionException, CannotJoinException, CrashCountException, RemoteException {
// add new participant to list of participants that belong to current trxId
List<TransactionParticipant> participants = _transactions.get(trxId);
_participantTest.add(tp);
participants.add(tp);
System.out.println("Test hash code " + this.hashCode());
System.out.println("Number of participants is " + participants.size() + "for Trxid " + trxId);
System.out.println("Number of participants in other is " + _participantTest.size() + "for Trxid " + trxId);
}
The following code is used to "publish" my TransactionManager
String[] groups = { "group" };
ServiceInfo serviceInfo = new ServiceInfo(
"twoPhaseTrxManager",
"a",
"b",
"1.0",
"Join Manager",
"c");
Entry[] entries = new Entry[] { serviceInfo };
LookupDiscoveryManager lookupDiscoveryManager = new LookupDiscoveryManager(groups, null, null);
// "this" (first argument) refers to Current instance of transaction manager (object being published)
new JoinManager(this, entries, (ServiceIDListener)null, lookupDiscoveryManager, new LeaseRenewalManager() );
Any help is truly appreciated.
I was able to solve the problem by changing this:
new JoinManager(this, entries, (ServiceIDListener)null, lookupDiscoveryManager, new LeaseRenewalManager() );
to this:
new JoinManager(RemoteObject.toStub(this), entries, (ServiceIDListener)null, lookupDiscoveryManager, new LeaseRenewalManager() );