return;
}
// Implementation note: the work listener is notified prior to release
// the start lock. This behavior is intentional and seems to be the
// more conservative.
workListener.workStarted(new WorkEvent(this, WorkEvent.WORK_STARTED, adaptee, null));
startLatch.release();
//Implementation note: we assume this is being called without an interesting TransactionContext,
//and ignore/replace whatever is associated with the current thread.
try {
if (executionContext == null || executionContext.getXid() == null) {
TransactionContext context = transactionContextManager.newUnspecifiedTransactionContext();
try {
adaptee.run();
} finally {
TransactionContext returningContext = transactionContextManager.getContext();
transactionContextManager.setContext(null);
if (context != returningContext) {
throw new WorkCompletedException("Wrong TransactionContext on return from work done");
}
}
//TODO should we commit the txContext to flush any leftover state???
} else {
try {
long transactionTimeout = executionContext.getTransactionTimeout();
//translate -1 value to 0 to indicate default transaction timeout.
transactionContextManager.begin(executionContext.getXid(), transactionTimeout == -1 ? 0 : transactionTimeout);
} catch (XAException e) {
throw new WorkCompletedException("Transaction import failed for xid " + executionContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
} catch (InvalidTransactionException e) {
throw new WorkCompletedException("Transaction import failed for xid " + executionContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
} catch (SystemException e) {
throw new WorkCompletedException("Transaction import failed for xid " + executionContext.getXid(), WorkCompletedException.TX_RECREATE_FAILED).initCause(e);
} catch (ImportedTransactionActiveException e) {
throw new WorkCompletedException("Transaction already active for xid " + executionContext.getXid(), WorkCompletedException.TX_CONCURRENT_WORK_DISALLOWED);
}
try {
adaptee.run();
} finally {
transactionContextManager.end(executionContext.getXid());
}
}
workListener.workCompleted(new WorkEvent(this, WorkEvent.WORK_COMPLETED, adaptee, null));
} catch (Throwable e) {
workException = (WorkException) (e instanceof WorkCompletedException ? e : new WorkCompletedException("Unknown error", WorkCompletedException.UNDEFINED).initCause(e));
workListener.workCompleted(new WorkEvent(this, WorkEvent.WORK_REJECTED, adaptee,
workException));
} finally {
endLatch.release();
}
}