Search in sources :

Example 6 with TransactionException

use of org.springframework.transaction.TransactionException in project grails-core by grails.

the class ChainedTransactionManager method getTransaction.

/*
	 * (non-Javadoc)
	 * @see org.springframework.transaction.PlatformTransactionManager#getTransaction(org.springframework.transaction.TransactionDefinition)
	 */
public MultiTransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
    MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0));
    if (!synchronizationManager.isSynchronizationActive() && canCreateTransaction(definition)) {
        synchronizationManager.initSynchronization();
        mts.setNewSynchronization();
    }
    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.isNewSynchronization()) {
            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 7 with TransactionException

use of org.springframework.transaction.TransactionException in project grails-core by grails.

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.isNewSynchronization()) {
        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 8 with TransactionException

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

the class SchedulerAccessor method registerJobsAndTriggers.

/**
 * Register jobs and triggers (within a transaction, if possible).
 */
protected void registerJobsAndTriggers() throws SchedulerException {
    TransactionStatus transactionStatus = null;
    if (this.transactionManager != null) {
        transactionStatus = this.transactionManager.getTransaction(TransactionDefinition.withDefaults());
    }
    try {
        if (this.jobSchedulingDataLocations != null) {
            ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(this.resourceLoader);
            clh.initialize();
            XMLSchedulingDataProcessor dataProcessor = new XMLSchedulingDataProcessor(clh);
            for (String location : this.jobSchedulingDataLocations) {
                dataProcessor.processFileAndScheduleJobs(location, getScheduler());
            }
        }
        // Register JobDetails.
        if (this.jobDetails != null) {
            for (JobDetail jobDetail : this.jobDetails) {
                addJobToScheduler(jobDetail);
            }
        } else {
            // Create empty list for easier checks when registering triggers.
            this.jobDetails = new ArrayList<>();
        }
        // Register Calendars.
        if (this.calendars != null) {
            for (String calendarName : this.calendars.keySet()) {
                Calendar calendar = this.calendars.get(calendarName);
                getScheduler().addCalendar(calendarName, calendar, true, true);
            }
        }
        // Register Triggers.
        if (this.triggers != null) {
            for (Trigger trigger : this.triggers) {
                addTriggerToScheduler(trigger);
            }
        }
    } catch (Throwable ex) {
        if (transactionStatus != null) {
            try {
                this.transactionManager.rollback(transactionStatus);
            } catch (TransactionException tex) {
                logger.error("Job registration exception overridden by rollback exception", ex);
                throw tex;
            }
        }
        if (ex instanceof SchedulerException) {
            throw (SchedulerException) ex;
        }
        if (ex instanceof Exception) {
            throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage(), ex);
        }
        throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage());
    }
    if (transactionStatus != null) {
        this.transactionManager.commit(transactionStatus);
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) XMLSchedulingDataProcessor(org.quartz.xml.XMLSchedulingDataProcessor) Calendar(org.quartz.Calendar) TransactionStatus(org.springframework.transaction.TransactionStatus) ClassLoadHelper(org.quartz.spi.ClassLoadHelper) SchedulerException(org.quartz.SchedulerException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException) TransactionException(org.springframework.transaction.TransactionException) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) TransactionException(org.springframework.transaction.TransactionException)

Example 9 with TransactionException

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

the class JpaTransactionManager method doBegin.

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
    JpaTransactionObject txObject = (JpaTransactionObject) transaction;
    if (txObject.hasConnectionHolder() && !txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
        throw new IllegalTransactionStateException("Pre-bound JDBC Connection found! JpaTransactionManager does not support " + "running within DataSourceTransactionManager if told to manage the DataSource itself. " + "It is recommended to use a single JpaTransactionManager for all transactions " + "on a single DataSource, no matter whether JPA or JDBC access.");
    }
    try {
        if (txObject.getEntityManagerHolder() == null || txObject.getEntityManagerHolder().isSynchronizedWithTransaction()) {
            EntityManager newEm = createEntityManagerForTransaction();
            if (logger.isDebugEnabled()) {
                logger.debug("Opened new EntityManager [" + newEm + "] for JPA transaction");
            }
            txObject.setEntityManagerHolder(new EntityManagerHolder(newEm), true);
        }
        EntityManager em = txObject.getEntityManagerHolder().getEntityManager();
        // Delegate to JpaDialect for actual transaction begin.
        final int timeoutToUse = determineTimeout(definition);
        Object transactionData = getJpaDialect().beginTransaction(em, new DelegatingTransactionDefinition(definition) {

            @Override
            public int getTimeout() {
                return timeoutToUse;
            }
        });
        txObject.setTransactionData(transactionData);
        // Register transaction timeout.
        if (timeoutToUse != TransactionDefinition.TIMEOUT_DEFAULT) {
            txObject.getEntityManagerHolder().setTimeoutInSeconds(timeoutToUse);
        }
        // Register the JPA EntityManager's JDBC Connection for the DataSource, if set.
        if (getDataSource() != null) {
            ConnectionHandle conHandle = getJpaDialect().getJdbcConnection(em, definition.isReadOnly());
            if (conHandle != null) {
                ConnectionHolder conHolder = new ConnectionHolder(conHandle);
                if (timeoutToUse != TransactionDefinition.TIMEOUT_DEFAULT) {
                    conHolder.setTimeoutInSeconds(timeoutToUse);
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Exposing JPA transaction as JDBC transaction [" + conHolder.getConnectionHandle() + "]");
                }
                TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
                txObject.setConnectionHolder(conHolder);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Not exposing JPA transaction [" + em + "] as JDBC transaction because " + "JpaDialect [" + getJpaDialect() + "] does not support JDBC Connection retrieval");
                }
            }
        }
        // Bind the entity manager holder to the thread.
        if (txObject.isNewEntityManagerHolder()) {
            TransactionSynchronizationManager.bindResource(getEntityManagerFactory(), txObject.getEntityManagerHolder());
        }
        txObject.getEntityManagerHolder().setSynchronizedWithTransaction(true);
    } catch (TransactionException ex) {
        closeEntityManagerAfterFailedBegin(txObject);
        throw ex;
    } catch (Throwable ex) {
        closeEntityManagerAfterFailedBegin(txObject);
        throw new CannotCreateTransactionException("Could not open JPA EntityManager for transaction", ex);
    }
}
Also used : DelegatingTransactionDefinition(org.springframework.transaction.support.DelegatingTransactionDefinition) ConnectionHandle(org.springframework.jdbc.datasource.ConnectionHandle) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) ConnectionHolder(org.springframework.jdbc.datasource.ConnectionHolder) EntityManager(javax.persistence.EntityManager) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) TransactionException(org.springframework.transaction.TransactionException)

Example 10 with TransactionException

use of org.springframework.transaction.TransactionException in project alf.io by alfio-event.

the class WaitingQueueSubscriptionProcessor method handleWaitingTickets.

void handleWaitingTickets() {
    Map<Boolean, List<Event>> activeEvents = eventManager.getActiveEvents().stream().collect(Collectors.partitioningBy(this::isWaitingListFormEnabled));
    activeEvents.get(true).forEach(event -> {
        TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW));
        try {
            ticketReservationManager.revertTicketsToFreeIfAccessRestricted(event.getId());
            distributeAvailableSeats(event);
            transactionManager.commit(transaction);
        } catch (Exception ex) {
            if (!(ex instanceof TransactionException)) {
                transactionManager.rollback(transaction);
            }
            log.error("cannot process waiting queue for event {}", event.getShortName(), ex);
        }
    });
    activeEvents.get(false).forEach(eventManager::resetReleasedTickets);
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionException(org.springframework.transaction.TransactionException) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionException(org.springframework.transaction.TransactionException)

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