}
public <T> T execTransaction(Callable<T> transaction, int timeout) throws Exception, ContextException {
TransactionManager txm = _txm;
if( txm == null ) {
throw new ContextException("Cannot locate the transaction manager; the server might be shutting down.");
}
// The value of the timeout is in seconds. If the value is zero, the transaction service restores the default value.
if (timeout < 0) {
throw new IllegalArgumentException("Timeout must be positive, received: "+timeout);
}
boolean existingTransaction = false;
try {
existingTransaction = txm.getTransaction() != null;
} catch (Exception ex) {
String errmsg = "Internal Error, could not get current transaction.";
throw new ContextException(errmsg, ex);
}
// already in transaction, execute and return directly
if (existingTransaction) {
return transaction.call();
}
// run in new transaction
Exception ex = null;
int immediateRetryCount = _immediateTransactionRetryLimit;
_txm.setTransactionTimeout(timeout);
if(__log.isDebugEnabled() && timeout!=0) __log.debug("Custom transaction timeout: "+timeout);
try {
do {
try {
if (__log.isDebugEnabled()) __log.debug("Beginning a new transaction");
txm.begin();
} catch (Exception e) {
String errmsg = "Internal Error, could not begin transaction.";
throw new ContextException(errmsg, e);
}
try {
ex = null;
return transaction.call();