Search in sources :

Example 46 with TransactionTemplate

use of org.springframework.transaction.support.TransactionTemplate in project molgenis by molgenis.

the class PlatformBootstrapper method bootstrap.

public void bootstrap(ContextRefreshedEvent event) {
    TransactionTemplate transactionTemplate = new TransactionTemplate();
    transactionTemplate.setTransactionManager(transactionManager);
    transactionTemplate.execute((action) -> {
        try {
            RunAsSystemAspect.runAsSystem(() -> {
                LOG.info("Bootstrapping registries ...");
                bootstrappingEventPublisher.publishBootstrappingStartedEvent();
                LOG.trace("Populating data source with ACL tables ...");
                dataSourceAclTablesPopulator.populate();
                LOG.debug("Populated data source with ACL tables");
                LOG.trace("Registering repository collections ...");
                repoCollectionBootstrapper.bootstrap(event, POSTGRESQL);
                LOG.trace("Registered repository collections");
                LOG.trace("Registering system entity meta data ...");
                systemEntityTypeRegistrar.register(event);
                LOG.trace("Registered system entity meta data");
                LOG.trace("Registering system packages ...");
                systemPackageRegistrar.register(event);
                LOG.trace("Registered system packages");
                LOG.trace("Registering entity factories ...");
                entityFactoryRegistrar.register(event);
                LOG.trace("Registered entity factories");
                LOG.trace("Registering entity factories ...");
                systemRepositoryDecoratorFactoryRegistrar.register(event);
                LOG.trace("Registered entity factories");
                LOG.debug("Bootstrapped registries");
                LOG.trace("Registering dynamic decorator factories ...");
                dynamicRepositoryDecoratorFactoryRegistrar.register(event.getApplicationContext());
                LOG.trace("Registered dynamic repository decorator factories");
                LOG.trace("Bootstrapping system entity types ...");
                systemEntityTypeBootstrapper.bootstrap(event);
                LOG.debug("Bootstrapped system entity types");
                LOG.trace("Registering job factories ...");
                jobFactoryRegistrar.register(event);
                LOG.trace("Registered job factories");
                event.getApplicationContext().getBean(EntityTypeRegistryPopulator.class).populate();
                event.getApplicationContext().getBean(DynamicDecoratorPopulator.class).populate();
                bootstrappingEventPublisher.publishBootstrappingFinishedEvent();
            });
        } catch (Exception unexpected) {
            LOG.error("Error bootstrapping tests!", unexpected);
            throw new RuntimeException(unexpected);
        }
        return (Void) null;
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) EntityTypeRegistryPopulator(org.molgenis.data.postgresql.identifier.EntityTypeRegistryPopulator) DynamicDecoratorPopulator(org.molgenis.data.decorator.meta.DynamicDecoratorPopulator)

Example 47 with TransactionTemplate

use of org.springframework.transaction.support.TransactionTemplate in project Activiti by Activiti.

the class SpringTransactionInterceptor method execute.

public <T> T execute(final CommandConfig config, final Command<T> command) {
    LOGGER.debug("Running command with propagation {}", config.getTransactionPropagation());
    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.setPropagationBehavior(getPropagation(config));
    T result = transactionTemplate.execute(new TransactionCallback<T>() {

        public T doInTransaction(TransactionStatus status) {
            return next.execute(config, command);
        }
    });
    return result;
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 48 with TransactionTemplate

use of org.springframework.transaction.support.TransactionTemplate in project spring-boot by spring-projects.

the class TransactionAutoConfigurationTests method whenThereAreBothReactiveAndPlatformTransactionManagersATemplateAndAnOperatorAreAutoConfigured.

@Test
void whenThereAreBothReactiveAndPlatformTransactionManagersATemplateAndAnOperatorAreAutoConfigured() {
    this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class)).withUserConfiguration(SinglePlatformTransactionManagerConfiguration.class, SingleReactiveTransactionManagerConfiguration.class).withPropertyValues("spring.datasource.url:jdbc:h2:mem:" + UUID.randomUUID()).run((context) -> {
        PlatformTransactionManager platformTransactionManager = context.getBean(PlatformTransactionManager.class);
        TransactionTemplate transactionTemplate = context.getBean(TransactionTemplate.class);
        assertThat(transactionTemplate.getTransactionManager()).isSameAs(platformTransactionManager);
        ReactiveTransactionManager reactiveTransactionManager = context.getBean(ReactiveTransactionManager.class);
        TransactionalOperator transactionalOperator = context.getBean(TransactionalOperator.class);
        assertThat(transactionalOperator).extracting("transactionManager").isSameAs(reactiveTransactionManager);
    });
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 49 with TransactionTemplate

use of org.springframework.transaction.support.TransactionTemplate in project spring-boot by spring-projects.

the class TransactionAutoConfigurationTests method whenThereIsASinglePlatformTransactionManagerATransactionTemplateIsAutoConfigured.

@Test
void whenThereIsASinglePlatformTransactionManagerATransactionTemplateIsAutoConfigured() {
    this.contextRunner.withUserConfiguration(SinglePlatformTransactionManagerConfiguration.class).run((context) -> {
        PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
        TransactionTemplate transactionTemplate = context.getBean(TransactionTemplate.class);
        assertThat(transactionTemplate.getTransactionManager()).isSameAs(transactionManager);
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Example 50 with TransactionTemplate

use of org.springframework.transaction.support.TransactionTemplate in project otter by alibaba.

the class DbDialectTest method test_mysql.

@Test(expectedExceptions = RuntimeException.class)
public void test_mysql() {
    DbDataMedia media = getMysqlMedia();
    final DbDialect dbDialect = dbDialectFactory.getDbDialect(2L, media.getSource());
    want.object(dbDialect).clazIs(MysqlDialect.class);
    final SqlTemplate sqlTemplate = dbDialect.getSqlTemplate();
    final JdbcTemplate jdbcTemplate = dbDialect.getJdbcTemplate();
    final TransactionTemplate transactionTemplate = dbDialect.getTransactionTemplate();
    final int[] pkColumnTypes = { Types.INTEGER, Types.VARCHAR };
    final int[] columnTypes = { Types.CHAR, Types.DECIMAL, Types.BLOB, Types.CLOB, Types.DATE, Types.TIMESTAMP, Types.TIMESTAMP };
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            int affect = 0;
            String sql = null;
            // 执行insert
            sql = sqlTemplate.getInsertSql(MYSQL_SCHEMA_NAME, TABLE_NAME, pkColumns, columns);
            System.out.println(sql);
            affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {

                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
                    return ps.executeUpdate();
                }
            });
            want.number(affect).isEqualTo(1);
            // 执行update
            sql = sqlTemplate.getUpdateSql(MYSQL_SCHEMA_NAME, TABLE_NAME, pkColumns, columns, true, null);
            System.out.println(sql);
            affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {

                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
                    return ps.executeUpdate();
                }
            });
            want.number(affect).isEqualTo(1);
            // 执行deleate
            sql = sqlTemplate.getDeleteSql(MYSQL_SCHEMA_NAME, TABLE_NAME, pkColumns);
            System.out.println(sql);
            affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {

                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    doPreparedStatement(ps, dbDialect, toTypes(pkColumnTypes), toValues(pkColumnValues));
                    return ps.executeUpdate();
                }
            });
            want.number(affect).isEqualTo(1);
            // 执行merge
            sql = sqlTemplate.getMergeSql(MYSQL_SCHEMA_NAME, TABLE_NAME, pkColumns, columns, null, true, null);
            System.out.println(sql);
            affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {

                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
                    return ps.executeUpdate();
                }
            });
            want.number(affect).isEqualTo(1);
            throw new RuntimeException("rollback");
        }
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatementCallback(org.springframework.jdbc.core.PreparedStatementCallback) PreparedStatement(java.sql.PreparedStatement) SqlTemplate(com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) TransactionCallback(org.springframework.transaction.support.TransactionCallback) DbDialect(com.alibaba.otter.node.etl.common.db.dialect.DbDialect) DbDataMedia(com.alibaba.otter.shared.common.model.config.data.db.DbDataMedia) Test(org.testng.annotations.Test) BaseDbTest(com.alibaba.otter.node.etl.BaseDbTest)

Aggregations

TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)287 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)178 TransactionStatus (org.springframework.transaction.TransactionStatus)158 Test (org.junit.jupiter.api.Test)139 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)70 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)56 UserTransaction (jakarta.transaction.UserTransaction)43 SQLException (java.sql.SQLException)36 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)29 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)25 Connection (java.sql.Connection)23 Test (org.junit.Test)22 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)22 InOrder (org.mockito.InOrder)21 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)18 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)17 DataSource (javax.sql.DataSource)16 DatabaseMetaData (java.sql.DatabaseMetaData)12 PreparedStatement (java.sql.PreparedStatement)12 Savepoint (java.sql.Savepoint)12