Fast-family framework fast-family-data source multiple data sources and integration xa distributed transaction use case

Keywords: Python JDBC MySQL Druid github

introduce

Fast-family-data source is a multi-data source processing module in fast-family framework. It provides XA distributed transaction processing, annotations specify data sources, and simple configuration file pointing can complete multi-data sources and transaction management.

Note: For the time being, only druid data sources are supported

Use cases

Introducing modules:

<dependency>
   <groupId>com.github.fast-family</groupId>
   <artifactId>fast-family-datasource</artifactId>
   <version>0.1.0</version>
</dependency>

Note: It has not been released to the central warehouse for the time being. You need to download the code.

No XA transaction configuration:

fast:
  family:
    type: com.alibaba.druid.pool.DruidDataSource
    datasource:
      master:
        druid:
          username: root
          password: root
          url: jdbc:mysql://localhost:3306/master-db?useUnicode=true&characterEncoding=utf8
          driverClassName: com.mysql.jdbc.Driver
        atomikos:
          uniqueResourceName: master-db
      slave_0:
        druid:
          username: root
          password: root
          url: jdbc:mysql://localhost:3306/slave-db?useUnicode=true&characterEncoding=utf8
          driverClassName: com.mysql.jdbc.Driver
        atomikos:
          uniqueResourceName: slave-db

Carry XA transaction configuration:

fast:
  family:
    type: com.alibaba.druid.pool.xa.DruidXADataSource
    datasource:
      master:
        druid:
          username: root
          password: root
          url: jdbc:mysql://localhost:3306/master-db?useUnicode=true&characterEncoding=utf8
          driverClassName: com.mysql.jdbc.Driver
        atomikos:
          uniqueResourceName: master-db
      slave_0:
        druid:
          username: root
          password: root
          url: jdbc:mysql://localhost:3306/slave-db?useUnicode=true&characterEncoding=utf8
          driverClassName: com.mysql.jdbc.Driver
        atomikos:
          uniqueResourceName: slave-db

Sample code:

Specify the corresponding data source name by using the DataSource Annotation annotation

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @DataSourceAnnotation(name = "slave_0")
    @Override
    public List<User> selectAlldb1() {
        return userDao.selectAll();
    }

    @DataSourceAnnotation(name = "master")
    @Override
    public List<User> selectAlldb2() {
        return userDao.selectAll();
    }

    @DataSourceAnnotation(name = "slave_0")
    @Override
    public void savedb1() {
        if (true){
            throw new RuntimeException("Abnormal.");
        }
    }

    @DataSourceAnnotation(name = "master")
    @Override
    public void savedb2() {
        User user = new User();
        user.setPassword("1231");
        user.setUsername("1231232");
        userDao.insert(user);
    }
}

Test code:

@RunWith(SpringRunner.class)
@SpringBootTest
public class FastFamilyDatasourceExampleApplicationTests {
    

    @Autowired
    private UserService userService;

    @Test
    public void contextLoads() {****
        System.out.println("Query data size:"+userService.selectAlldb1().size());
        System.out.println("Query data size:"+userService.selectAlldb2().size());
    }


    @Transactional
    @Test
    public void saveTest(){
        userService.savedb2();
        userService.savedb1();
    }

}

Test results:

No business:

With XA transactions:

Last:

If you are interested in this framework or want to get the latest progress information, you can use qq q group: 390295286
github address: https://github.com/fast-famil... Welcome to star fork
github example address: https://github.com/fast-famil...

Posted by freakstyle on Sat, 02 Feb 2019 18:36:15 -0800