Example usage for org.springframework.transaction PlatformTransactionManager rollback

List of usage examples for org.springframework.transaction PlatformTransactionManager rollback

Introduction

In this page you can find the example usage for org.springframework.transaction PlatformTransactionManager rollback.

Prototype

void rollback(TransactionStatus status) throws TransactionException;

Source Link

Document

Perform a rollback of the given transaction.

Usage

From source file:org.openvpms.tools.archetype.loader.ArchetypeLoader.java

/**
 * ArchDiff line./*from  ww  w . j a va  2s  .  c om*/
 *
 * @param args the command line arguments
 */
public static void main(String[] args) {
    BasicConfigurator.configure();

    try {
        JSAP parser = createParser();
        JSAPResult config = parser.parse(args);
        if (!config.success()) {
            displayUsage(parser, config);
        } else {
            String contextPath = config.getString("context");

            ApplicationContext context;
            if (!new File(contextPath).exists()) {
                context = new ClassPathXmlApplicationContext(contextPath);
            } else {
                context = new FileSystemXmlApplicationContext(contextPath);
            }

            IArchetypeService service = (IArchetypeService) context.getBean("archetypeService");
            ArchetypeLoader loader = new ArchetypeLoader(service);
            String file = config.getString("file");
            String dir = config.getString("dir");
            boolean recurse = config.getBoolean("subdir");
            loader.setOverwrite(config.getBoolean("overwrite"));
            loader.setFailOnError(config.getBoolean("failOnError"));
            loader.setVerbose(config.getBoolean("verbose"));
            boolean clean = config.getBoolean("clean");
            String mappingFile = config.getString("mappingFile");
            int processed = 0;

            PlatformTransactionManager mgr;
            mgr = (PlatformTransactionManager) context.getBean("txnManager");
            TransactionStatus status = mgr.getTransaction(new DefaultTransactionDefinition());
            try {
                if (clean) {
                    loader.clean();
                    ++processed;
                }
                if (mappingFile != null) {
                    loader.loadAssertions(mappingFile);
                    ++processed;
                }
                if (file != null) {
                    loader.loadArchetypes(file);
                    ++processed;
                } else if (dir != null) {
                    loader.loadArchetypes(dir, recurse);
                    ++processed;
                }
                mgr.commit(status);
                if (processed == 0) {
                    displayUsage(parser, config);
                }
            } catch (Throwable throwable) {
                log.error(throwable, throwable);
                log.error("Rolling back changes");
                mgr.rollback(status);
            }
        }
    } catch (Throwable throwable) {
        log.error(throwable, throwable);
    }
}

From source file:com.googlecode.starflow.engine.support.TriggerProcessEventUtil.java

/**
 * ?/*from w w w.  j ava  2s  .  c  o  m*/
 * 
 * @param processInstId
 * @param action
 * @return
 */
private static void executeLogicInNewTransaction(ProcessDefine processDefine, ProcessInstance processInstance,
        IAction action) {
    PlatformTransactionManager txManager = ApplicationContextHolder.getBean(PlatformTransactionManager.class);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    TransactionStatus status = txManager.getTransaction(definition);
    try {
        action.execute(processDefine, processInstance);
        txManager.commit(status);
    } catch (Exception e) {
        txManager.rollback(status);
        throw new ProcessEngineException("?", e);
    }
}

From source file:com.googlecode.starflow.engine.support.TriggerActivityEventUtil.java

/**
 * ?/*from ww w . ja  v  a2  s  .  c  o  m*/
 * 
 * @param activityXml
 * @param activityInst
 * @param action
 * @return
 */
private static void executeLogicInNewTransaction(ActivityElement activityXml, ActivityInst activityInst,
        IAction action) {
    PlatformTransactionManager txManager = ApplicationContextHolder.getBean(PlatformTransactionManager.class);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    TransactionStatus status = txManager.getTransaction(definition);
    try {
        action.execute(activityXml, activityInst);
        txManager.commit(status);
    } catch (Exception e) {
        txManager.rollback(status);
        throw new ProcessEngineException("?", e);
    }
}

From source file:bigbank.transaction.MultiTransactionStatus.java

void rollback(PlatformTransactionManager transactionManager) {
    transactionManager.rollback(getTransactionStatus(transactionManager));
}

From source file:com.becool.base.spring.tx.MultiTransactionStatus.java

/**
 * Rolls back the {@link TransactionStatus} registered for the given {@link PlatformTransactionManager}.
 * /*from   w w w  . j a v a  2  s  .  co  m*/
 * @param transactionManager must not be {@literal null}.
 */
public void rollback(PlatformTransactionManager transactionManager) {
    transactionManager.rollback(getTransactionStatus(transactionManager));
}

From source file:org.dbflute.utflute.spring.SpringTransactionResource.java

public void rollback() {
    if (_transactionManagerList.isEmpty()) {
        return;/*from  www .ja v a  2s .c o  m*/
    }
    final OneTransactionElement mainTx = _transactionManagerList.get(0); // because of TransactionSynchronization
    try {
        final PlatformTransactionManager manager = mainTx.getTransactionManager();
        final TransactionStatus status = mainTx.getTransactionStatus();
        manager.rollback(status);
    } catch (RuntimeException e) {
        throw new TransactionFailureException("Failed to roll-back the transaction.", e);
    }
}

From source file:org.jboss.arquillian.transaction.spring.provider.AbstractSpringTransactionProvider.java

/**
 * {@inheritDoc}/*from w  w  w  . j a  v  a2s  . c  o m*/
 */
@Override
public void rollbackTransaction(TransactionalTest transactionalTest) {

    PlatformTransactionManager transactionManager = transactionManagerInstance.get();
    TransactionStatus transactionStatus = transactionStatusInstance.get();

    // rollbacks the transaction
    transactionManager.rollback(transactionStatus);
}

From source file:com.alibaba.cobar.client.transaction.MultipleDataSourcesTransactionManager.java

@Override
protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
    @SuppressWarnings("unchecked")
    List<DefaultTransactionStatus> list = (List<DefaultTransactionStatus>) status.getTransaction();

    logger.info("prepare to rollback transactions on multiple data sources.");
    Validate.isTrue(list.size() <= this.getTransactionManagers().size());

    TransactionException lastException = null;
    for (int i = list.size() - 1; i >= 0; i--) {
        PlatformTransactionManager transactionManager = this.getTransactionManagers().get(i);
        TransactionStatus localTransactionStatus = list.get(i);

        try {/*from  w  w  w .jav  a2  s  . c om*/
            transactionManager.rollback(localTransactionStatus);
        } catch (TransactionException e) {
            // Log exception and try to complete rollback
            lastException = e;
            logger.error("error occured when rolling back the transaction. \n{}", e);
        }
    }

    if (lastException != null) {
        throw lastException;
    }
}

From source file:org.copperengine.spring.SpringTransaction.java

public void run(PlatformTransactionManager transactionManager, DataSource dataSource, TransactionDefinition def)
        throws Exception {
    TransactionStatus txnStatus = transactionManager.getTransaction(def);
    try {//from   w  w  w  .  jav  a 2 s  . c  om
        Connection con = DataSourceUtils.getConnection(dataSource);
        try {
            execute(con);
        } finally {
            DataSourceUtils.releaseConnection(con, dataSource);
        }
    } catch (Exception e) {
        transactionManager.rollback(txnStatus);
        throw e;
    }
    transactionManager.commit(txnStatus);
}

From source file:org.ohmage.query.impl.ImageQueries.java

@Override
public void deleteImage(UUID imageId) throws DataAccessException {
    // Create the transaction.
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("Deleting an image.");

    try {/* w w  w.j a v a2 s.c om*/
        // Begin the transaction.
        PlatformTransactionManager transactionManager = new DataSourceTransactionManager(getDataSource());
        TransactionStatus status = transactionManager.getTransaction(def);

        URL imageUrl = getImageUrl(imageId);

        try {
            getJdbcTemplate().update(SQL_DELETE_IMAGE, new Object[] { imageId.toString() });
        } catch (org.springframework.dao.DataAccessException e) {
            transactionManager.rollback(status);
            throw new DataAccessException(
                    "Error executing SQL '" + SQL_DELETE_IMAGE + "' with parameter: " + imageId, e);
        }

        if (imageUrl != null) {
            deleteImageDiskOnly(imageUrl);
        }

        // Commit the transaction.
        try {
            transactionManager.commit(status);
        } catch (TransactionException e) {
            transactionManager.rollback(status);
            throw new DataAccessException("Error while committing the transaction.", e);
        }
    } catch (TransactionException e) {
        throw new DataAccessException("Error while attempting to rollback the transaction.", e);
    }
}