dubbo+zookeeper分布式框架如何加入事务控制(入坑篇)

1.需要在启动类上配置开启事务注解

@EnableTransactionManagement(proxyTargetClass = true)

2.在需要添加事务控制的类上面,添加注解(一定要这个事务名字,不要用默认的)

@Transactional(value = "myTransactionManager")

3.配置事务控制的配置类,注入到容器中

@Configuration
public class MyTransaction {

    @Value("root")
    private String username;
    @Value("123")
    private String password;
    @Value("jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8")
    private String url;
    @Value("com.mysql.jdbc.Driver")
    private String driver;

    //向ioc中注入数据源
    @Bean
    public DataSource getDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setUrl(url);
        druidDataSource.setDriverClassName(driver);
        return druidDataSource;
    }


    @Bean(value = "myTransactionManager")
    public TransactionManager getTransactionManager(DataSource dataSource){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }


}

你可能感兴趣的:(springboot,zookeeper,mybatis,分布式,zookeeper,spring,boot)