Methods Summary |
---|
public void | endInvocation(javax.transaction.Transaction thetx)
if (log.isTraceEnabled())
log.trace("endInvocation: miTx=" + thetx + " " + toString());
|
public void | endTransaction()
boolean trace = log.isTraceEnabled();
if (trace)
log.trace("endTransaction: " + toString());
nextTransaction(trace);
|
public java.lang.Object | getResourceHolder()
return tx;
|
public javax.transaction.Transaction | getTransaction()
return tx;
|
protected org.jboss.aspects.txlock.QueuedTxLock$TxLock | getTxLock(javax.transaction.Transaction miTx)
TxLock lock = null;
TxLock key = new TxLock(miTx);
lock = (TxLock) txLocks.get(key);
if (lock == null)
{
txLocks.put(key, key);
txWaitQueue.addLast(key);
lock = key;
}
return lock;
|
protected boolean | isTxExpired(javax.transaction.Transaction miTx)
if (miTx != null && miTx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
{
return true;
}
return false;
|
public boolean | lockNoWait(javax.transaction.Transaction transaction)
this.sync();
if (log.isTraceEnabled())
log.trace("lockNoWait tx=" + transaction + " " + toString());
try
{
// And are we trying to enter with another transaction?
if (getTransaction() != null && !getTransaction().equals(transaction))
{
return false;
}
setTransaction(transaction);
return true;
}
finally
{
this.releaseSync();
}
|
protected void | nextTransaction(boolean trace)
if (!synched)
{
throw new IllegalStateException("do not call nextTransaction while not synched!");
}
setTransaction(null);
// is there a waiting list?
TxLock thelock = null;
if (!txWaitQueue.isEmpty())
{
thelock = (TxLock) txWaitQueue.removeFirst();
txLocks.remove(thelock);
thelock.isQueued = false;
// The new transaction is the next one, important to set it up to avoid race with
// new incoming calls
if (thelock.waitingTx != null)
DeadlockDetector.singleton.removeWaiting(thelock.waitingTx);
setTransaction(thelock.waitingTx);
synchronized (thelock)
{
// notify All threads waiting on this transaction.
// They will enter the methodLock wait loop.
thelock.notifyAll();
}
}
if (trace)
log.trace("nextTransaction: " + thelock + " " + toString());
|
public void | releaseSync()
synchronized (this)
{
synched = false;
this.notify();
}
|
public void | schedule(javax.transaction.Transaction miTx, org.jboss.aop.joinpoint.Invocation mi)doSchedule(Invocation)
doSchedule implements a particular policy for scheduling the threads coming in.
There is always the spec required "serialization" but we can add custom scheduling in here
Synchronizing on lock: a failure to get scheduled must result in a wait() call and a
release of the lock. Schedulation must return with lock.
boolean trace = log.isTraceEnabled();
this.sync();
try
{
if (trace) log.trace("Begin schedule");
if (isTxExpired(miTx))
{
log.error("Saw rolled back tx=" + miTx);
throw new RuntimeException("Transaction marked for rollback, possibly a timeout");
}
//Next test is independent of whether the context is locked or not, it is purely transactional
// Is the instance involved with another transaction? if so we implement pessimistic locking
waitForTx(mi, miTx, trace);
if (!isSynchronized)
{
isSynchronized = true;
miTx.registerSynchronization(new TxLockSynchronization());
}
}
finally
{
this.releaseSync();
}
|
public void | setTransaction(javax.transaction.Transaction tx)The setTransaction associates a transaction with the lock.
The current transaction is associated by the schedule call.
this.tx = tx;
|
public void | sync()
synchronized (this)
{
while (synched)
{
this.wait();
}
synched = true;
}
|
public java.lang.String | toString()
StringBuffer buffer = new StringBuffer(100);
buffer.append(" hash=").append(hashCode());
buffer.append(" tx=").append(getTransaction());
buffer.append(" synched=").append(synched);
buffer.append(" queue=").append(txWaitQueue);
return buffer.toString();
|
protected void | waitForTx(org.jboss.aop.joinpoint.Invocation mi, javax.transaction.Transaction miTx, boolean trace)Wait until no other transaction is running with this lock.
boolean wasScheduled = false;
// Do we have a running transaction with the context?
// We loop here until either until success or until transaction timeout
// If we get out of the loop successfully, we can successfully
// set the transaction on this puppy.
TxLock txLock = null;
while (getTransaction() != null &&
// And are we trying to enter with another transaction?
!getTransaction().equals(miTx))
{
// Check for a deadlock on every cycle
try
{
DeadlockDetector.singleton.deadlockDetection(miTx, this);
}
catch (Exception e)
{
// We were queued, not any more
if (txLock != null && txLock.isQueued)
{
txLocks.remove(txLock);
txWaitQueue.remove(txLock);
}
throw e;
}
wasScheduled = true;
// That's no good, only one transaction per context
// Let's put the thread to sleep the transaction demarcation will wake them up
if (trace) log.trace("Transactional contention on context miTx=" + miTx + " " + toString());
if (txLock == null)
txLock = getTxLock(miTx);
if (trace) log.trace("Begin wait on " + txLock + " " + toString());
// And lock the threads on the lock corresponding to the Tx in MI
synchronized (txLock)
{
releaseSync();
try
{
int txTimeout = 0;
Integer timeout = (Integer) mi.getMetaData(TXLOCK, TIMEOUT);
if (timeout != null) txTimeout = timeout.intValue();
txLock.wait(txTimeout);
}
catch (InterruptedException ignored)
{
}
} // end synchronized(txLock)
this.sync();
if (trace) log.trace("End wait on " + txLock + " " + toString());
if (isTxExpired(miTx))
{
log.error(Thread.currentThread() + "Saw rolled back tx=" + miTx + " waiting for txLock");
if (txLock.isQueued)
{
// Remove the TxLock from the queue because this thread is exiting.
// Don't worry about notifying other threads that share the same transaction.
// They will timeout and throw the below RuntimeException
txLocks.remove(txLock);
txWaitQueue.remove(txLock);
}
else if (getTransaction() != null && getTransaction().equals(miTx))
{
// We're not qu
nextTransaction(trace);
}
if (miTx != null)
{
DeadlockDetector.singleton.removeWaiting(miTx);
}
throw new RuntimeException("Transaction marked for rollback, possibly a timeout");
}
} // end while(tx!=miTx)
// If we get here, this means that we have the txlock
if (!wasScheduled) setTransaction(miTx);
return;
|