Java Code Examples for javax.transaction.TransactionManager#setTransactionTimeout()

The following examples show how to use javax.transaction.TransactionManager#setTransactionTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JtaTransactionManager.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Transaction createTransaction(@Nullable String name, int timeout) throws NotSupportedException, SystemException {
	TransactionManager tm = getTransactionManager();
	Assert.state(tm != null, "No JTA TransactionManager available");
	if (timeout >= 0) {
		tm.setTransactionTimeout(timeout);
	}
	tm.begin();
	return new ManagedTransactionAdapter(tm);
}
 
Example 2
Source File: JtaTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Transaction createTransaction(@Nullable String name, int timeout) throws NotSupportedException, SystemException {
	TransactionManager tm = getTransactionManager();
	Assert.state(tm != null, "No JTA TransactionManager available");
	if (timeout >= 0) {
		tm.setTransactionTimeout(timeout);
	}
	tm.begin();
	return new ManagedTransactionAdapter(tm);
}
 
Example 3
Source File: JtaTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Transaction createTransaction(String name, int timeout) throws NotSupportedException, SystemException {
	TransactionManager tm = getTransactionManager();
	Assert.state(tm != null, "No JTA TransactionManager available");
	if (timeout >= 0) {
		tm.setTransactionTimeout(timeout);
	}
	tm.begin();
	return new ManagedTransactionAdapter(tm);
}
 
Example 4
Source File: JtaTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Transaction createTransaction(String name, int timeout) throws NotSupportedException, SystemException {
	TransactionManager tm = getTransactionManager();
	Assert.state(tm != null, "No JTA TransactionManager available");
	if (timeout >= 0) {
		tm.setTransactionTimeout(timeout);
	}
	tm.begin();
	return new ManagedTransactionAdapter(tm);
}
 
Example 5
Source File: TransactionalInterceptorBase.java    From quarkus with Apache License 2.0 4 votes vote down vote up
protected Object invokeInOurTx(InvocationContext ic, TransactionManager tm, RunnableWithException afterEndTransaction)
        throws Exception {

    TransactionConfiguration configAnnotation = getTransactionConfiguration(ic);
    int currentTmTimeout = ((CDIDelegatingTransactionManager) transactionManager).getTransactionTimeout();
    if (configAnnotation != null && configAnnotation.timeout() != TransactionConfiguration.UNSET_TIMEOUT) {
        tm.setTransactionTimeout(configAnnotation.timeout());
    }
    Transaction tx;
    try {
        tm.begin();
        tx = tm.getTransaction();
    } finally {
        if (configAnnotation != null && configAnnotation.timeout() != TransactionConfiguration.UNSET_TIMEOUT) {
            //restore the default behaviour
            tm.setTransactionTimeout(currentTmTimeout);
        }
    }

    boolean throwing = false;
    Object ret = null;

    try {
        ret = ic.proceed();
    } catch (Exception e) {
        throwing = true;
        handleException(ic, e, tx);
    } finally {
        // handle asynchronously if not throwing
        if (!throwing && ret != null) {
            ReactiveTypeConverter<Object> converter = null;
            if (ret instanceof CompletionStage == false
                    && (ret instanceof Publisher == false || ic.getMethod().getReturnType() != Publisher.class)) {
                @SuppressWarnings({ "rawtypes", "unchecked" })
                Optional<ReactiveTypeConverter<Object>> lookup = Registry.lookup((Class) ret.getClass());
                if (lookup.isPresent()) {
                    converter = lookup.get();
                    if (converter.emitAtMostOneItem()) {
                        ret = converter.toCompletionStage(ret);
                    } else {
                        ret = converter.toRSPublisher(ret);
                    }
                }
            }
            if (ret instanceof CompletionStage) {
                ret = handleAsync(tm, tx, ic, ret, afterEndTransaction);
                // convert back
                if (converter != null)
                    ret = converter.fromCompletionStage((CompletionStage<?>) ret);
            } else if (ret instanceof Publisher) {
                ret = handleAsync(tm, tx, ic, ret, afterEndTransaction);
                // convert back
                if (converter != null)
                    ret = converter.fromPublisher((Publisher<?>) ret);
            } else {
                // not async: handle synchronously
                endTransaction(tm, tx, afterEndTransaction);
            }
        } else {
            // throwing or null: handle synchronously
            endTransaction(tm, tx, afterEndTransaction);
        }
    }
    return ret;
}