Java Code Examples for org.springframework.jdbc.datasource.DataSourceTransactionManager

The following examples show how to use org.springframework.jdbc.datasource.DataSourceTransactionManager. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: loc-framework   Source File: LocDataSourceAutoConfiguration.java    License: 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 2
Source Project: tddl   Source File: JDBCTemplateTest.java    License: Apache License 2.0 6 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 3
Source Project: tddl5   Source File: JDBCTemplateTest.java    License: 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 4
Source Project: hadoop-ozone   Source File: JooqPersistenceModule.java    License: 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 5
@Inject
@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource, ApplicationConfiguration applicationConfiguration) {
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
    dataSourceTransactionManager.setDefaultTimeout(applicationConfiguration.getDbTransactionTimeoutInSeconds());
    return dataSourceTransactionManager;
}
 
Example 6
Source Project: x7   Source File: DataSourceConfig.java    License: 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 7
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 8
Source Project: spring-cloud-task   Source File: TestConfiguration.java    License: 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
/**
 * 自定义事务
 * 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 10
@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 11
Source Project: nextreports-server   Source File: JdbcAuditor.java    License: 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 12
/**
 * 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 Project: saluki   Source File: MybatisConfiguration.java    License: 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 14
/**
 * 配置事物管理器
 *
 * @return
 */
@Bean(name = "read2TransactionManager")
public DataSourceTransactionManager read2TransactionManager(
        @Qualifier("read2DataSource") DataSource dataSource
) {
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
    dataSourceTransactionManager.setDataSource(dataSource);
    return dataSourceTransactionManager;
}
 
Example 15
@Bean(name = "lightminServerSchedulerTransactionManager")
public PlatformTransactionManager lightminServerSchedulerTransactionManager(
        final ServerSchedulerJdbcConfigurationProperties properties) {
    final DataSource dataSource = getDataSource(properties);
    return new DataSourceTransactionManager(dataSource);
}
 
Example 16
public void addDataSource(DataSource ds) {
    this.txnManagersForEachDataSource.add(new DataSourceTransactionManager(ds));
}
 
Example 17
Source Project: maintain   Source File: GjentDatabaseConfig.java    License: MIT License 4 votes vote down vote up
@Bean(name = "gjentTxMan")
public PlatformTransactionManager secondTransactionManager() {
	logger.info("gjent dataSource--" + this.gjentDataSource().hashCode());
	return new DataSourceTransactionManager(this.gjentDataSource());
}
 
Example 18
Source Project: JGiven   Source File: ProxyTestConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}
 
Example 19
Source Project: Resource   Source File: DataSource1Config.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Primary
@Bean(name = "db1Manager")
public PlatformTransactionManager txManager(@Qualifier("db1DataSource") DataSource dataSource)
{
    return new DataSourceTransactionManager(dataSource);
}
 
Example 20
@Bean
PlatformTransactionManager txManager(DataSource dataSource) {
	return new DataSourceTransactionManager(dataSource);
}
 
Example 21
@Bean
PlatformTransactionManager transactionManager(DataSource dataSource) {
	return new DataSourceTransactionManager(dataSource);
}
 
Example 22
@Bean
public PlatformTransactionManager txMgr2() {
	return new DataSourceTransactionManager(dataSource2());
}
 
Example 23
@Bean
PlatformTransactionManager transactionManager(DataSource dataSource) {
	return new DataSourceTransactionManager(dataSource);
}
 
Example 24
Source Project: spring4-understanding   Source File: EmptyDatabaseConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public PlatformTransactionManager transactionManager() {
	return new DataSourceTransactionManager(dataSource());
}
 
Example 25
@Bean
public PlatformTransactionManager transactionManager() {
	return new DataSourceTransactionManager(dataSource());
}
 
Example 26
Source Project: EasyReport   Source File: ReadWriteDataSourceConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(name = "readwriteTransactionTemplate")
public TransactionTemplate transactionTemplate(
    @Qualifier("readwriteTransactionManager") final DataSourceTransactionManager transactionManager)
    throws Exception {
    return this.createTransactionTemplate(transactionManager);
}
 
Example 27
@Bean
public DataSourceTransactionManager noticeExpressTransactionManager(DataSource noticeExpress){
	return new DataSourceTransactionManager(noticeExpress);
}
 
Example 28
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}
 
Example 29
Source Project: taoshop   Source File: MybatisConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean(name = MYBATIS_TRANSACTION_MANAGER)
public DataSourceTransactionManager transactionManager(@Qualifier(DATA_SOURCE_NAME)DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}
 
Example 30
@Bean(name = "clusterTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() {
    return new DataSourceTransactionManager(clusterDataSource());
}