Example usage for org.springframework.transaction TransactionDefinition TIMEOUT_DEFAULT

List of usage examples for org.springframework.transaction TransactionDefinition TIMEOUT_DEFAULT

Introduction

In this page you can find the example usage for org.springframework.transaction TransactionDefinition TIMEOUT_DEFAULT.

Prototype

int TIMEOUT_DEFAULT

To view the source code for org.springframework.transaction TransactionDefinition TIMEOUT_DEFAULT.

Click Source Link

Document

Use the default timeout of the underlying transaction system, or none if timeouts are not supported.

Usage

From source file:com._4dconcept.springframework.data.marklogic.datasource.ContentSourceTransactionManager.java

/**
 * This implementation sets the isolation level but ignores the timeout.
 *//* ww  w. j  a  v  a  2s.co m*/
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    ContentSourceTransactionObject txObject = (ContentSourceTransactionObject) transaction;
    Session ses = null;

    try {
        if (txObject.getSessionHolder() == null
                || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
            Session newSes = this.contentSource.newSession();
            if (logger.isDebugEnabled()) {
                logger.debug("Acquired Session [" + newSes + "] for XDBC transaction");
            }
            txObject.setSessionHolder(new SessionHolder(newSes), true);
        }

        txObject.getSessionHolder().setSynchronizedWithTransaction(true);
        ses = txObject.getSessionHolder().getSession();

        Integer previousIsolationLevel = ContentSourceUtils.prepareSessionForTransaction(ses, definition);
        txObject.setPreviousIsolationLevel(previousIsolationLevel);

        txObject.getSessionHolder().setTransactionActive(true);

        int timeout = determineTimeout(definition);
        if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
            txObject.getSessionHolder().setTimeoutInSeconds(timeout);
        }

        // Bind the session holder to the thread.
        if (txObject.isNewSessionHolder()) {
            TransactionSynchronizationManager.bindResource(getContentSource(), txObject.getSessionHolder());
        }
    } catch (Throwable ex) {
        if (txObject.isNewSessionHolder()) {
            ContentSourceUtils.releaseSession(ses, this.contentSource);
            txObject.setSessionHolder(null, false);
        }
        throw new CannotCreateTransactionException("Could not open XDBC Session for transaction", ex);
    }
}

From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManager.java

protected void setSlim3AsnyncTimeout(final int timeoutToUse) throws TransactionException {
    if (timeoutToUse == TransactionDefinition.TIMEOUT_DEFAULT) {
        Datastore.deadline(null);/*from  w  ww.j a  va  2s.  c  o m*/
    } else if ((timeoutToUse > 0) || (timeoutToUse < GAEJ_REQUEST_DEADLINE)) {
        Datastore.deadline((new Integer(timeoutToUse)).doubleValue());
    } else {
        throw new InvalidTimeoutException(
                String.format(
                        "The specified time-out value for the transaction is not valid; "
                                + "it should be bigger than 0[sec] and less than %1$d[sec].",
                        GAEJ_REQUEST_DEADLINE),
                timeoutToUse);
    }
}

From source file:jp.terasoluna.fw.batch.util.BatchUtilTest.java

/**
 * testGetTransactionDefinition02/*  ww  w .ja  va 2s  .c  o m*/
 * @throws Exception
 */
@Test
public void testGetTransactionDefinition02() throws Exception {

    // 
    TransactionDefinition result = BatchUtil.getTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.ISOLATION_DEFAULT,
            TransactionDefinition.TIMEOUT_DEFAULT, false);

    // ?
    assertNotNull(result);
}

From source file:jp.terasoluna.fw.batch.util.BatchUtilTest.java

/**
 * testStartTransaction04/* w  ww.j  a  v  a 2  s  .c om*/
 * @throws Exception
 */
@Test
public void testStartTransaction04() throws Exception {
    // 
    PlatformTransactionManager tran = new PlatformTransactionManagerStub();

    // 
    TransactionStatus result = BatchUtil.startTransaction(tran, TransactionDefinition.PROPAGATION_REQUIRED,
            TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.TIMEOUT_DEFAULT, false);

    // ?
    assertNotNull(result);
}

From source file:jp.terasoluna.fw.batch.util.BatchUtilTest.java

/**
 * testStartTransaction05/*from  ww w.j  a  v a  2s . c  o m*/
 * @throws Exception
 */
@Test
public void testStartTransaction05() throws Exception {
    // 
    PlatformTransactionManager tran = new PlatformTransactionManagerStub();

    // 
    TransactionStatus result = BatchUtil.startTransaction(tran, TransactionDefinition.PROPAGATION_REQUIRED,
            TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.TIMEOUT_DEFAULT, false, log);

    // ?
    assertNotNull(result);
}

From source file:org.alfresco.repo.transaction.TransactionServiceImpl.java

/**
 * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRED
 *//*from   www  . ja va2s.c om*/
public UserTransaction getUserTransaction(boolean readOnly, boolean ignoreSystemReadOnly) {
    if (ignoreSystemReadOnly) {
        SpringAwareUserTransaction txn = new SpringAwareUserTransaction(transactionManager, (readOnly),
                TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.PROPAGATION_REQUIRED,
                TransactionDefinition.TIMEOUT_DEFAULT);
        return txn;
    } else {
        SpringAwareUserTransaction txn = new SpringAwareUserTransaction(transactionManager,
                (readOnly | isReadOnly()), TransactionDefinition.ISOLATION_DEFAULT,
                TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.TIMEOUT_DEFAULT);
        return txn;
    }
}

From source file:org.alfresco.repo.transaction.TransactionServiceImpl.java

/**
 * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRES_NEW
 *///from w ww  .j a v a 2s.c  om
@Override
public UserTransaction getNonPropagatingUserTransaction(boolean readOnly, boolean ignoreSystemReadOnly) {
    if (ignoreSystemReadOnly) {
        SpringAwareUserTransaction txn = new SpringAwareUserTransaction(transactionManager, (readOnly),
                TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.PROPAGATION_REQUIRES_NEW,
                TransactionDefinition.TIMEOUT_DEFAULT);
        return txn;
    } else {
        SpringAwareUserTransaction txn = new SpringAwareUserTransaction(transactionManager,
                (readOnly | isReadOnly()), TransactionDefinition.ISOLATION_DEFAULT,
                TransactionDefinition.PROPAGATION_REQUIRES_NEW, TransactionDefinition.TIMEOUT_DEFAULT);
        return txn;
    }
}

From source file:org.springframework.orm.jpa.vendor.HibernateJpaDialect.java

@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
        throws PersistenceException, SQLException, TransactionException {

    Session session = getSession(entityManager);

    if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
        session.getTransaction().setTimeout(definition.getTimeout());
    }//from   ww w  .  j  a v a 2  s . com

    boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT);
    Integer previousIsolationLevel = null;
    Connection preparedCon = null;

    if (isolationLevelNeeded || definition.isReadOnly()) {
        if (this.prepareConnection) {
            preparedCon = HibernateConnectionHandle.doGetConnection(session);
            previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition);
        } else if (isolationLevelNeeded) {
            throw new InvalidIsolationLevelException(getClass().getSimpleName()
                    + " does not support custom isolation levels since the 'prepareConnection' flag is off.");
        }
    }

    // Standard JPA transaction begin call for full JPA context setup...
    entityManager.getTransaction().begin();

    // Adapt flush mode and store previous isolation level, if any.
    FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly());
    return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel);
}

From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java

/**
 * This implementation handles propagation behavior. Delegates to
 * {@code doGetTransaction}, {@code isExistingTransaction}
 * and {@code doBegin}./* w w  w . j  ava2s . co  m*/
 * @see #doGetTransaction
 * @see #isExistingTransaction
 * @see #doBegin
 */
@Override
public final Mono<ReactiveTransaction> getReactiveTransaction(@Nullable TransactionDefinition definition)
        throws TransactionException {

    // Use defaults if no transaction definition given.
    TransactionDefinition def = (definition != null ? definition : TransactionDefinition.withDefaults());

    return TransactionSynchronizationManager.currentTransaction().flatMap(synchronizationManager -> {

        Object transaction = doGetTransaction(synchronizationManager);

        // Cache debug flag to avoid repeated checks.
        boolean debugEnabled = logger.isDebugEnabled();

        if (isExistingTransaction(transaction)) {
            // Existing transaction found -> check propagation behavior to find out how to behave.
            return handleExistingTransaction(synchronizationManager, def, transaction, debugEnabled);
        }

        // Check definition settings for new transaction.
        if (def.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {
            return Mono.error(new InvalidTimeoutException("Invalid transaction timeout", def.getTimeout()));
        }

        // No existing transaction found -> check propagation behavior to find out how to proceed.
        if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {
            return Mono.error(new IllegalTransactionStateException(
                    "No existing transaction found for transaction marked with propagation 'mandatory'"));
        } else if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED
                || def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW
                || def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {

            return TransactionContextManager.currentContext().map(TransactionSynchronizationManager::new)
                    .flatMap(nestedSynchronizationManager -> suspend(nestedSynchronizationManager, null)
                            .map(Optional::of).defaultIfEmpty(Optional.empty()).flatMap(suspendedResources -> {
                                if (debugEnabled) {
                                    logger.debug("Creating new transaction with name [" + def.getName() + "]: "
                                            + def);
                                }
                                return Mono.defer(() -> {
                                    GenericReactiveTransaction status = newReactiveTransaction(
                                            nestedSynchronizationManager, def, transaction, true, debugEnabled,
                                            suspendedResources.orElse(null));
                                    return doBegin(nestedSynchronizationManager, transaction, def)
                                            .doOnSuccess(ignore -> prepareSynchronization(
                                                    nestedSynchronizationManager, status, def))
                                            .thenReturn(status);
                                }).onErrorResume(ErrorPredicates.RUNTIME_OR_ERROR,
                                        ex -> resume(nestedSynchronizationManager, null,
                                                suspendedResources.orElse(null)).then(Mono.error(ex)));
                            }));
        } else {
            // Create "empty" transaction: no actual transaction, but potentially synchronization.
            if (def.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT && logger.isWarnEnabled()) {
                logger.warn("Custom isolation level specified but no actual transaction initiated; "
                        + "isolation level will effectively be ignored: " + def);
            }
            return Mono.just(
                    prepareReactiveTransaction(synchronizationManager, def, null, true, debugEnabled, null));
        }
    });
}

From source file:org.springframework.transaction.support.AbstractPlatformTransactionManager.java

/**
 * Specify the default timeout that this transaction manager should apply
 * if there is no timeout specified at the transaction level, in seconds.
 * <p>Default is the underlying transaction infrastructure's default timeout,
 * e.g. typically 30 seconds in case of a JTA provider, indicated by the
 * {@code TransactionDefinition.TIMEOUT_DEFAULT} value.
 * @see org.springframework.transaction.TransactionDefinition#TIMEOUT_DEFAULT
 */// w  w  w.  j a  v  a2 s . c  om
public final void setDefaultTimeout(int defaultTimeout) {
    if (defaultTimeout < TransactionDefinition.TIMEOUT_DEFAULT) {
        throw new InvalidTimeoutException("Invalid default timeout", defaultTimeout);
    }
    this.defaultTimeout = defaultTimeout;
}