Example usage for org.springframework.transaction.support TransactionTemplate setTimeout

List of usage examples for org.springframework.transaction.support TransactionTemplate setTimeout

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionTemplate setTimeout.

Prototype

public final void setTimeout(int timeout) 

Source Link

Document

Set the timeout to apply, as number of seconds.

Usage

From source file:fi.hsl.parkandride.back.LockDaoTest.java

private <T> Future<T> runTxInOtherThread(TransactionCallback<T> transactionCallback) {
    return Executors.newSingleThreadExecutor().submit(() -> {
        TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
        txTemplate.setTimeout(1);
        return txTemplate.execute(transactionCallback);
    });//w ww.  ja va 2 s  .c  o  m
}

From source file:org.ambraproject.struts2.TransactionInterceptor.java

public String intercept(final ActionInvocation actionInvocation) throws Exception {

    final Action action = (Action) actionInvocation.getAction();
    final ActionProxy actionProxy = actionInvocation.getProxy();
    final String methodName = actionProxy.getMethod();

    if (getAnnotation(action.getClass(), methodName, ManualTransactionManagement.class) != null) {
        //Method is annotated tellling us not to manage a transaction for it
        log.debug(//  w  w w  . j  a v a  2  s .  c o m
                "Not managing transaction for " + action.getClass().getSimpleName() + "." + methodName + "()");
        return actionInvocation.invoke();
    }

    if (log.isDebugEnabled()) {
        log.debug("Intercepted " + action.getClass().getSimpleName() + "." + methodName + "()");
    }

    final Transactional transactionalAnnotation = getAnnotation(action.getClass(), methodName,
            Transactional.class);
    TransactionTemplate txTemplate = new TransactionTemplate(transactionManager);
    if (transactionalAnnotation != null) {
        txTemplate.setReadOnly(transactionalAnnotation.readOnly());
        txTemplate.setTimeout(transactionalAnnotation.timeout());
        txTemplate.setIsolationLevel(transactionalAnnotation.isolation().value());
        txTemplate.setPropagationBehavior(transactionalAnnotation.propagation().value());
    }

    CallbackResult callbackResult = (CallbackResult) txTemplate.execute(new TransactionCallback() {
        public CallbackResult doInTransaction(TransactionStatus transactionStatus) {
            CallbackResult result = new CallbackResult();
            try {
                String actionResult = actionInvocation.invoke();
                result.setResult(actionResult);
                //Rollback for Action responses indicating failure
                for (String response : new String[] { Action.ERROR, Action.INPUT, Action.LOGIN }) {
                    if (response.equalsIgnoreCase(actionResult) && !transactionStatus.isRollbackOnly()) {
                        log.debug("Rolling back action " + action.getClass().getSimpleName()
                                + " due to result: " + actionResult);
                        transactionStatus.setRollbackOnly();
                        break;
                    }
                }
            } catch (Exception e) {
                /*
                * Callback does not throw exception. We need to pass Exception object in the return
                * parameter so we can throw it in the calling method.
                */
                boolean noRollback = false;

                if (transactionalAnnotation != null && transactionalAnnotation.noRollbackFor() != null) {
                    for (Class<? extends Throwable> exception : transactionalAnnotation.noRollbackFor()) {
                        if (exception.isInstance(e)) {
                            noRollback = true;
                            break;
                        }
                    }
                }

                if (!noRollback && transactionalAnnotation != null
                        && transactionalAnnotation.rollbackFor() != null) {
                    for (Class<? extends Throwable> exception : transactionalAnnotation.rollbackFor()) {
                        if (exception.isInstance(e)) {
                            log.debug("Caught exception, rolling back action invocation "
                                    + action.getClass().getSimpleName());
                            transactionStatus.setRollbackOnly();
                            break;
                        }
                    }
                }
                result.setException(e);
            }
            return result;
        }
    });

    if (callbackResult.getException() != null)
        throw callbackResult.getException();

    return callbackResult.getResult();
}