org.springframework.jdbc.datasource.DataSourceTransactionManager Java Examples

The following examples show how to use org.springframework.jdbc.datasource.DataSourceTransactionManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JDBCTemplateTest.java    From tddl with Apache License 2.0 8 votes vote down vote up
@Test
public void tractionRollBackTest() {
    JdbcTemplate andorJT = new JdbcTemplate(us);
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(us);
    TransactionStatus ts = transactionManager.getTransaction(def);

    try {
        sql = String.format("insert into %s (pk,name) values(?,?)", normaltblTableName);
        andorJT.update(sql, new Object[] { RANDOM_ID, name });
        sql = String.format("select * from %s where pk= ?", normaltblTableName);
        Map re = andorJT.queryForMap(sql, new Object[] { RANDOM_ID });
        Assert.assertEquals(name, String.valueOf(re.get("NAME")));
        // 回滚
        transactionManager.rollback(ts);
    } catch (DataAccessException ex) {
        transactionManager.rollback(ts);
        throw ex;
    } finally {
    }
    // 验证查询不到数据
    sql = String.format("select * from %s where pk= ?", normaltblTableName);
    List le = andorJT.queryForList(sql, new Object[] { RANDOM_ID });
    Assert.assertEquals(0, le.size());
}
 
Example #2
Source File: JDBCTemplateTest.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
/**
 * traction RollBack
 * 
 * @author zhuoxue
 * @since 5.0.1
 */
@Test
public void tractionRollBackTest() {
    JdbcTemplate andorJT = new JdbcTemplate(tddlDatasource);
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(tddlDatasource);
    TransactionStatus ts = transactionManager.getTransaction(def);

    try {
        sql = String.format("insert into %s (pk,name) values(?,?)", normaltblTableName);
        andorJT.update(sql, new Object[] { RANDOM_ID, name });
        sql = String.format("select * from %s where pk= ?", normaltblTableName);
        Map re = andorJT.queryForMap(sql, new Object[] { RANDOM_ID });
        Assert.assertEquals(name, String.valueOf(re.get("name")));
        // 回滚
        transactionManager.rollback(ts);
    } catch (DataAccessException ex) {
        transactionManager.rollback(ts);
        throw ex;
    } finally {
    }
    // 验证查询不到数据
    sql = String.format("select * from %s where pk= ?", normaltblTableName);
    List le = andorJT.queryForList(sql, new Object[] { RANDOM_ID });
    Assert.assertEquals(0, le.size());
}
 
Example #3
Source File: LocDataSourceAutoConfiguration.java    From loc-framework with MIT License 6 votes vote down vote up
private void createBean(ConfigurableListableBeanFactory configurableListableBeanFactory,
    String prefixName, JdbcProperties jdbcProperties) {
  String jdbcUrl = jdbcProperties.getJdbcUrl();
  checkArgument(!Strings.isNullOrEmpty(jdbcUrl), prefixName + " url is null or empty");
  log.info("prefixName is {}, jdbc properties is {}", prefixName, jdbcProperties);

  HikariDataSource hikariDataSource = createHikariDataSource(jdbcProperties);
  DataSourceSpy dataSource = new DataSourceSpy(hikariDataSource);

  DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
  AnnotationTransactionAspect.aspectOf().setTransactionManager(transactionManager);

  JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

  register(configurableListableBeanFactory, dataSource, prefixName + "DataSource",
      prefixName + "Ds");
  register(configurableListableBeanFactory, jdbcTemplate, prefixName + "JdbcTemplate",
      prefixName + "Jt");
  register(configurableListableBeanFactory, transactionManager, prefixName + "TransactionManager",
      prefixName + "Tx");
}
 
Example #4
Source File: JdbcAuditor.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
public void logEvent(final AuditEvent event) {
	TransactionTemplate transactionTemplate = new TransactionTemplate();
    transactionTemplate.setTransactionManager(new DataSourceTransactionManager(getDataSource()));
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
    	
    	@Override
           protected void doInTransactionWithoutResult(TransactionStatus status) {
       	    int id = getNextEventId();

       		Object[] eventData = new Object[8];
       	    eventData[0] = id;
       		eventData[1] = event.getDate();
       		eventData[2] = event.getUsername();
       		eventData[3] = event.getAction();
       		eventData[4] = event.getSession();
       		eventData[5] = event.getIp();
       		eventData[6] = event.getLevel();
       		eventData[7] = event.getErrorMessage();
       		
       	    getJdbcTemplate().update(INSERT_EVENT_SQL, eventData);
       	    
       	    Map<String, Object> context = event.getContext();
       	    for (String name : context.keySet()) {
           		Object[] contextData = new Object[3];
           		contextData[0] = id;
           		contextData[1] = name;
           		contextData[2] = context.get(name);

       	    	getJdbcTemplate().update(INSERT_CONTEXT_SQL, contextData);
       	    }
           }
           
    });
}
 
Example #5
Source File: MybatisConfiguration.java    From saluki with Apache License 2.0 5 votes vote down vote up
@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
    try {
        return new DataSourceTransactionManager(datasource());
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #6
Source File: JobExecutionsDocumentation.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private void initialize() throws Exception {
	JobRepositoryFactoryBean repositoryFactoryBean = new JobRepositoryFactoryBean();
	repositoryFactoryBean.setDataSource(this.dataSource);
	repositoryFactoryBean.setTransactionManager(new DataSourceTransactionManager(this.dataSource));
	this.jobRepository = repositoryFactoryBean.getObject();
	this.dao = (new TaskExecutionDaoFactoryBean(this.dataSource)).getObject();
	this.taskBatchDao = new JdbcTaskBatchDao(this.dataSource);
}
 
Example #7
Source File: DataSourceTransactionManagerConfiguration.java    From dk-foundation with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 自定义事务
 * MyBatis自动参与到spring事务管理中,无需额外配置,只要org.mybatis.spring.SqlSessionFactoryBean引用的数据源与DataSourceTransactionManager引用的数据源一致即可,否则事务管理会不起作用。
 * @return
 */
@Primary
@Bean(name = "myTransactionManager")
@Resource
public DataSourceTransactionManager transactionManagers(DataSource myBatisDataSource) {
    logger.info("-------------------- transactionManager init ---------------------");
    return new DynamicDataSourceTransactionManager(myBatisDataSource);
}
 
Example #8
Source File: TestConfiguration.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public PlatformTransactionManager transactionManager() {
	if (this.dataSource == null) {
		return new ResourcelessTransactionManager();
	}
	else {
		return new DataSourceTransactionManager(this.dataSource);
	}
}
 
Example #9
Source File: TransactionPolicyNestedTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Override
protected CamelContext createCamelContext() throws Exception {
    SimpleRegistry registry = new SimpleRegistry();
    dataSource = EmbeddedDataSourceFactory.getDataSource("sql/schema.sql");

    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
    registry.put("transactionManager", transactionManager);

    {
        SpringTransactionPolicy propagationRequired = new SpringTransactionPolicy();
        propagationRequired.setTransactionManager(transactionManager);
        propagationRequired.setPropagationBehaviorName("PROPAGATION_REQUIRED");
        registry.put("PROPAGATION_REQUIRED", propagationRequired);
    }

    {
        SpringTransactionPolicy propagationNotSupported = new SpringTransactionPolicy();
        propagationNotSupported.setTransactionManager(transactionManager);
        propagationNotSupported.setPropagationBehaviorName("PROPAGATION_NOT_SUPPORTED");
        registry.put("PROPAGATION_NOT_SUPPORTED", propagationNotSupported);
    }

    auditLogDao = new AuditLogDao(dataSource);
    messageDao = new MessageDao(dataSource);

    CamelContext camelContext = new DefaultCamelContext(registry);

    SqlComponent sqlComponent = new SqlComponent();
    sqlComponent.setDataSource(dataSource);
    camelContext.addComponent("sql", sqlComponent);

    return camelContext;
}
 
Example #10
Source File: ProgrammaticTxMgmtTestNGTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Bean
public PlatformTransactionManager transactionManager() {
	return new DataSourceTransactionManager(dataSource());
}
 
Example #11
Source File: Read2DruidDataSourceConfig.java    From SpringBoot-Study with Apache License 2.0 5 votes vote down vote up
/**
 * 配置事物管理器
 *
 * @return
 */
@Bean(name = "read2TransactionManager")
public DataSourceTransactionManager read2TransactionManager(
        @Qualifier("read2DataSource") DataSource dataSource
) {
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
    dataSourceTransactionManager.setDataSource(dataSource);
    return dataSourceTransactionManager;
}
 
Example #12
Source File: SpringJdbcTransactionOperations.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param transactionManager The transaction manager
 */
protected SpringJdbcTransactionOperations(
        DataSourceTransactionManager transactionManager) {
    this.dataSource = transactionManager.getDataSource();
    this.transactionManager = transactionManager;
    this.writeTransactionTemplate = new TransactionTemplate(transactionManager);
    DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
    transactionDefinition.setReadOnly(true);
    this.readTransactionTemplate = new TransactionTemplate(transactionManager, transactionDefinition);
}
 
Example #13
Source File: JooqPersistenceModule.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
  bind(DataSource.class).toProvider(DefaultDataSourceProvider.class)
      .in(Singleton.class);

  TransactionalMethodInterceptor interceptor =
      new TransactionalMethodInterceptor(
          getProvider(DataSourceTransactionManager.class));

  bindInterceptor(annotatedWith(Transactional.class), any(), interceptor);
  bindInterceptor(any(), annotatedWith(Transactional.class), interceptor);
  System.setProperty("org.jooq.no-logo", "true");
}
 
Example #14
Source File: DataSourceConfig.java    From x7 with Apache License 2.0 5 votes vote down vote up
@Bean
public PlatformTransactionManagerCustomizer platformTransactionManagerCustomizer(AbstractRoutingDataSource abstractRoutingDataSource){
    return transactionManager -> {
        DataSourceTransactionManager dataSourceTransactionManager = (DataSourceTransactionManager) transactionManager;
        dataSourceTransactionManager.setDataSource(abstractRoutingDataSource);
    };
}
 
Example #15
Source File: DatabaseConfiguration.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
@Inject
@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource, ApplicationConfiguration applicationConfiguration) {
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
    dataSourceTransactionManager.setDefaultTimeout(applicationConfiguration.getDbTransactionTimeoutInSeconds());
    return dataSourceTransactionManager;
}
 
Example #16
Source File: TransactionConfig.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}
 
Example #17
Source File: DataSourceConfig.java    From momo-cloud-permission with Apache License 2.0 4 votes vote down vote up
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager() {
    return new DataSourceTransactionManager(this.dataSource());
}
 
Example #18
Source File: EmbeddedPersonDatabaseTestsConfig.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Bean
public PlatformTransactionManager txMgr() {
	return new DataSourceTransactionManager(dataSource());
}
 
Example #19
Source File: DruidDataSourceConfig.java    From xxpay-master with MIT License 4 votes vote down vote up
@Bean
public PlatformTransactionManager transactionManager() throws SQLException {
    return new DataSourceTransactionManager(dataSource());
}
 
Example #20
Source File: ProgrammaticTransactionConfig.java    From spring-boot-cookbook with Apache License 2.0 4 votes vote down vote up
@Bean(name = "transactionManager")
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
    return new DataSourceTransactionManager(dataSource);
}
 
Example #21
Source File: FailingBeforeAndAfterMethodsSpringExtensionTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Bean
PlatformTransactionManager transactionManager() {
	return new DataSourceTransactionManager(dataSource());
}
 
Example #22
Source File: AccountCenterDataSourceConfig.java    From light-reading-cloud with MIT License 4 votes vote down vote up
/** 事务管理器 */
@Bean(name = "accountCenterTransactionManager")
public DataSourceTransactionManager setTransactionManager(@Qualifier("accountCenterDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}
 
Example #23
Source File: EngineConfiguration.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Bean(name = "transactionManager")
public PlatformTransactionManager annotationDrivenTransactionManager(DataSource dataSource) {
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
    transactionManager.setDataSource(dataSource);
    return transactionManager;
}
 
Example #24
Source File: DataConfig.java    From Project with Apache License 2.0 4 votes vote down vote up
public PlatformTransactionManager annotationDrivenTransactionManager() {
  return new DataSourceTransactionManager(dataSource());
}
 
Example #25
Source File: Transactioner.java    From cjs_ssms with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 初始化事务对象并开启事务
 * @param transactionManager
 */
public Transactioner(DataSourceTransactionManager transactionManager) {
  this.transactionManager = transactionManager;
  start(transactionManager);
}
 
Example #26
Source File: PersistHelper.java    From bulbasaur with Apache License 2.0 4 votes vote down vote up
@PostConstruct
protected void init() {
	transactionManager = new DataSourceTransactionManager(PersistModule.getInstance().getDataSource());
}
 
Example #27
Source File: Spring_2_IT_Configuration.java    From spring-test-examples with Apache License 2.0 4 votes vote down vote up
@Bean
public PlatformTransactionManager transactionManager() {
  return new DataSourceTransactionManager(dataSource());
}
 
Example #28
Source File: MultipleDataSourcesAndTransactionManagersSqlScriptsTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Bean
public PlatformTransactionManager txMgr2() {
	return new DataSourceTransactionManager(dataSource2());
}
 
Example #29
Source File: InferredDataSourceSqlScriptsTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Bean
public PlatformTransactionManager txMgr2() {
	return new DataSourceTransactionManager(dataSource2());
}
 
Example #30
Source File: DruidDataSourceConfig.java    From xxpay-master with MIT License 4 votes vote down vote up
@Bean
public PlatformTransactionManager transactionManager() throws SQLException {
    return new DataSourceTransactionManager(dataSource());
}