Search in sources :

Example 11 with TransactionException

use of org.springframework.transaction.TransactionException in project spring-data-commons by spring-projects.

the class ChainedTransactionManager method rollback.

/*
	 * (non-Javadoc)
	 * @see org.springframework.transaction.PlatformTransactionManager#rollback(org.springframework.transaction.TransactionStatus)
	 */
public void rollback(TransactionStatus status) throws TransactionException {
    Exception rollbackException = null;
    PlatformTransactionManager rollbackExceptionTransactionManager = null;
    MultiTransactionStatus multiTransactionStatus = (MultiTransactionStatus) status;
    for (PlatformTransactionManager transactionManager : reverse(transactionManagers)) {
        try {
            multiTransactionStatus.rollback(transactionManager);
        } catch (Exception ex) {
            if (rollbackException == null) {
                rollbackException = ex;
                rollbackExceptionTransactionManager = transactionManager;
            } else {
                LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex.getMessage(), ex);
            }
        }
    }
    if (multiTransactionStatus.isNewSynchonization()) {
        synchronizationManager.clearSynchronization();
    }
    if (rollbackException != null) {
        throw new UnexpectedRollbackException("Rollback exception, originated at (" + rollbackExceptionTransactionManager + ") " + rollbackException.getMessage(), rollbackException);
    }
}
Also used : UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 12 with TransactionException

use of org.springframework.transaction.TransactionException in project spring-data-commons by spring-projects.

the class ChainedTransactionManager method commit.

/*
	 * (non-Javadoc)
	 * @see org.springframework.transaction.PlatformTransactionManager#commit(org.springframework.transaction.TransactionStatus)
	 */
public void commit(TransactionStatus status) throws TransactionException {
    MultiTransactionStatus multiTransactionStatus = (MultiTransactionStatus) status;
    boolean commit = true;
    Exception commitException = null;
    PlatformTransactionManager commitExceptionTransactionManager = null;
    for (PlatformTransactionManager transactionManager : reverse(transactionManagers)) {
        if (commit) {
            try {
                multiTransactionStatus.commit(transactionManager);
            } catch (Exception ex) {
                commit = false;
                commitException = ex;
                commitExceptionTransactionManager = transactionManager;
            }
        } else {
            try {
                multiTransactionStatus.rollback(transactionManager);
            } catch (Exception ex) {
                LOGGER.warn("Rollback exception (after commit) (" + transactionManager + ") " + ex.getMessage(), ex);
            }
        }
    }
    if (multiTransactionStatus.isNewSynchonization()) {
        synchronizationManager.clearSynchronization();
    }
    if (commitException != null) {
        boolean firstTransactionManagerFailed = commitExceptionTransactionManager == getLastTransactionManager();
        int transactionState = firstTransactionManagerFailed ? HeuristicCompletionException.STATE_ROLLED_BACK : HeuristicCompletionException.STATE_MIXED;
        throw new HeuristicCompletionException(transactionState, commitException);
    }
}
Also used : HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 13 with TransactionException

use of org.springframework.transaction.TransactionException in project spring-data-commons by spring-projects.

the class ChainedTransactionManager method getTransaction.

/*
	 * (non-Javadoc)
	 * @see org.springframework.transaction.PlatformTransactionManager#getTransaction(org.springframework.transaction.TransactionDefinition)
	 */
public MultiTransactionStatus getTransaction(@Nullable TransactionDefinition definition) throws TransactionException {
    MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0));
    if (definition == null) {
        return mts;
    }
    if (!synchronizationManager.isSynchronizationActive()) {
        synchronizationManager.initSynchronization();
        mts.setNewSynchonization();
    }
    try {
        for (PlatformTransactionManager transactionManager : transactionManagers) {
            mts.registerTransactionManager(definition, transactionManager);
        }
    } catch (Exception ex) {
        Map<PlatformTransactionManager, TransactionStatus> transactionStatuses = mts.getTransactionStatuses();
        for (PlatformTransactionManager transactionManager : transactionManagers) {
            try {
                if (transactionStatuses.get(transactionManager) != null) {
                    transactionManager.rollback(transactionStatuses.get(transactionManager));
                }
            } catch (Exception ex2) {
                LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex2.getMessage(), ex2);
            }
        }
        if (mts.isNewSynchonization()) {
            synchronizationManager.clearSynchronization();
        }
        throw new CannotCreateTransactionException(ex.getMessage(), ex);
    }
    return mts;
}
Also used : CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Map(java.util.Map) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException)

Example 14 with TransactionException

use of org.springframework.transaction.TransactionException in project molgenis by molgenis.

the class JobTest method testTransactionTimeout.

@Test
public void testTransactionTimeout() {
    TransactionException transactionException = new TransactionTimedOutException("Transaction timeout test.");
    when(transactionOperations.execute(any())).thenThrow(transactionException);
    try {
        job.call();
        fail("TransactionException should be thrown");
    } catch (TransactionException expected) {
        assertSame(expected, transactionException);
    }
    verify(transactionOperations).execute(any());
    verify(progress).failed(transactionException);
    verifyNoMoreInteractions(callable, progress, transactionOperations);
}
Also used : TransactionTimedOutException(org.springframework.transaction.TransactionTimedOutException) TransactionException(org.springframework.transaction.TransactionException) Test(org.testng.annotations.Test)

Example 15 with TransactionException

use of org.springframework.transaction.TransactionException in project nakadi by zalando.

the class TimelineService method switchTimelines.

private void switchTimelines(final Timeline activeTimeline, final Timeline nextTimeline) throws InconsistentStateException, RepositoryProblemException, TimelineException, ConflictException {
    LOG.info("Switching timelines from {} to {}", activeTimeline, nextTimeline);
    try {
        timelineSync.startTimelineUpdate(activeTimeline.getEventType(), nakadiSettings.getTimelineWaitTimeoutMs());
    } catch (final InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new TimelineException("Failed to switch timeline for: " + activeTimeline.getEventType());
    } catch (final IllegalStateException ie) {
        throw new ConflictException("Timeline is already being created for: " + activeTimeline.getEventType(), ie);
    }
    try {
        transactionTemplate.execute(status -> {
            timelineDbRepository.createTimeline(nextTimeline);
            nextTimeline.setSwitchedAt(new Date());
            final Timeline.StoragePosition sp = topicRepositoryHolder.createStoragePosition(activeTimeline);
            activeTimeline.setLatestPosition(sp);
            scheduleTimelineCleanup(activeTimeline);
            timelineDbRepository.updateTimelime(activeTimeline);
            timelineDbRepository.updateTimelime(nextTimeline);
            return null;
        });
    } catch (final TransactionException tx) {
        LOG.error(tx.getMessage(), tx);
        throw new TimelineException("Failed to create timeline in DB for: " + activeTimeline.getEventType(), tx);
    } finally {
        finishTimelineUpdate(activeTimeline.getEventType());
    }
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TransactionException(org.springframework.transaction.TransactionException) ConflictException(org.zalando.nakadi.exceptions.ConflictException) Date(java.util.Date) TimelineException(org.zalando.nakadi.exceptions.TimelineException) DuplicatedTimelineException(org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException)

Aggregations

TransactionException (org.springframework.transaction.TransactionException)19 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)8 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)7 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)6 TransactionDefinition (org.springframework.transaction.TransactionDefinition)4 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 Serializable (java.io.Serializable)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Predicate (java.util.function.Predicate)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 Nullable (org.springframework.lang.Nullable)2 DataGridUser (com.emc.metalnx.core.domain.entity.DataGridUser)1