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;
});
}
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;
}
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);
});
}
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);
});
}
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");
}
});
}
Aggregations