Example usage for org.springframework.transaction PlatformTransactionManager getTransaction

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

Introduction

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

Prototype

TransactionStatus getTransaction(@Nullable TransactionDefinition definition) throws TransactionException;

Source Link

Document

Return a currently active transaction or create a new one, according to the specified propagation behavior.

Usage

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

/**
 * ArchDiff line.//from  ww w  . j ava2 s  .c o  m
 *
 * @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

/**
 * ?// w  w  w. j a  va 2s. co 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 w w w  .  j  a  v a  2 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: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  . j  ava  2s . co  m*/
        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:bigbank.transaction.MultiTransactionStatus.java

public void registerTransactionManager(TransactionDefinition definition,
        PlatformTransactionManager transactionManager) {
    getTransactionStatuses().put(transactionManager, transactionManager.getTransaction(definition));
}

From source file:com.opensymphony.able.filter.TransactionServletFilter.java

public void doFilter(final ServletRequest request, final ServletResponse response,
        final FilterChain filterChain) throws IOException, ServletException {
    // TODO we could get clever and figure out what URIs are read only transactions etc
    TransactionTemplate transactionTemplate = (TransactionTemplate) context.getBean("transactionTemplate");
    transactionTemplate.setReadOnly(false);

    if (log.isDebugEnabled()) {
        log.debug("Starting a transaction");
    }//from www . jav a2s .  c  o m

    PlatformTransactionManager transactionManager = transactionTemplate.getTransactionManager();
    TransactionStatus status = transactionManager.getTransaction(transactionTemplate);
    TransactionOutcome outcome = new TransactionOutcome(status, transactionTemplate);
    request.setAttribute(TRANSACTION_OUTCOME, outcome);

    Exception exception = null;
    try {
        filterChain.doFilter(request, response);
        status = transactionManager.getTransaction(transactionTemplate);
    } catch (Exception e) {
        exception = e;
        log.warn("Caught: " + e, e);
    }

    if (outcome.isRollbackOnly() || exception != null) {
        status.setRollbackOnly();
    }

    try {
        if (status.isRollbackOnly()) {
            log.debug("Rolling back transaction");
            transactionManager.rollback(status);
        } else {
            log.debug("Committing transaction");
            transactionManager.commit(status);
        }
    } catch (TransactionException e) {
        if (exception == null) {
            exception = e;
        } else {
            log.debug("Failed to rollback transaction: " + e, e);
        }
    }

    if (exception instanceof IOException) {
        throw (IOException) exception;
    } else if (exception instanceof ServletException) {
        throw (ServletException) exception;
    } else if (exception != null) {
        throw new ServletException(exception);
    }
}

From source file:cn.uncode.dal.internal.shards.transaction.MultiDataSourcesTransactionManager.java

@Override
public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {

    MultiDataSourcesTransactionStatus transactionStatus = new MultiDataSourcesTransactionStatus();

    log.debug("Operation '" + definition.getName() + "' starting transaction.");

    for (DataSource dataSource : dataSources) {
        DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(
                definition);/* ww  w.j  ava2  s .co  m*/
        defaultTransactionDefinition.setName(definition.getName());

        PlatformTransactionManager txManager = this.transactionManagers.get(dataSource);
        TransactionStatus status = txManager.getTransaction(defaultTransactionDefinition);

        TransactionSynchronizationManager.setCurrentTransactionName(defaultTransactionDefinition.getName());

        transactionStatus.put(dataSource, status);
    }

    return transactionStatus;

}

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

/**
 * We need to disable transaction synchronization so that the shared
 * transaction synchronization state will not collide with each other. BUT,
 * for LOB creators to use, we have to pay attention here:
 * <ul>/*from   w w w .j  av  a2  s  .com*/
 * <li>if the LOB creator use standard preparedStatement methods, this
 * transaction synchronization setting is OK;</li>
 * <li>if the LOB creator don't use standard PS methods, you have to find
 * other way to make sure the resources your LOB creator used should be
 * cleaned up after the transaction.</li>
 * </ul>
 */
@Override
protected void doBegin(Object transactionObject, TransactionDefinition transactionDefinition)
        throws TransactionException {
    @SuppressWarnings("unchecked")
    List<DefaultTransactionStatus> list = (List<DefaultTransactionStatus>) transactionObject;
    for (PlatformTransactionManager transactionManager : transactionManagers) {
        DefaultTransactionStatus element = (DefaultTransactionStatus) transactionManager
                .getTransaction(transactionDefinition);
        list.add(element);
    }
}

From source file:com.zhengmo.data.transaction.MultiTransactionStatus.java

/**
 * .//from w w  w  .j av a 2  s .com
 * @param definition 
 * @param transactionManager ?
 */
public void registerTransactionManager(TransactionDefinition definition,
        PlatformTransactionManager transactionManager) {
    getTransactionStatuses().put(transactionManager, transactionManager.getTransaction(definition));
}

From source file:org.openvpms.maven.archetype.ArchetypeLoadMojo.java

/**
 * Execute the plugin.//from   w  w  w .ja v  a2 s .c om
 *
 * @throws MojoExecutionException if an unexpected problem occurs
 * @throws MojoFailureException   if an expected problem (such as a compilation failure) occurs
 */
protected void doExecute() throws MojoExecutionException, MojoFailureException {
    if (dir == null || !dir.exists()) {
        throw new MojoExecutionException("Directory not found: " + dir);
    }
    if (!dir.isDirectory()) {
        throw new MojoExecutionException("Not a directory: " + dir);
    }
    ApplicationContext context = getContext();
    IArchetypeService service = (IArchetypeService) context.getBean("archetypeService");
    ArchetypeLoader loader = new ArchetypeLoader(service);
    loader.setOverwrite(overwrite);
    loader.setVerbose(verbose);

    PlatformTransactionManager mgr;
    mgr = (PlatformTransactionManager) context.getBean("txnManager");
    TransactionStatus status = mgr.getTransaction(new DefaultTransactionDefinition());
    File mappingFile = (assertionTypes != null) ? assertionTypes : new File(dir, "assertionTypes.xml");
    try {
        if (mappingFile.exists()) {
            loader.loadAssertions(mappingFile.getPath());
        }
        loader.loadArchetypes(dir.getPath(), true);
        mgr.commit(status);
    } catch (Throwable throwable) {
        mgr.rollback(status);
        throw new MojoExecutionException("Failed to load archetypes", throwable);
    }
}