【Atomikos】分布式事务简单示例

2 篇文章 0 订阅
1 篇文章 0 订阅

简介

基于Derby数据库,使用Atomikos框架开发一些简单的示例,包括与spring集成。

使用版本

  • JDK 11
  • atomikos 5.0.8
  • Spring 5.3.13

Atomikos + Derby 示例

DirectApplication.java

public class DirectApplication {
    
    private DataSource inventoryDataSource;
    private DataSource orderDataSource;

    public DirectApplication(DataSource inventoryDataSource, DataSource orderDataSource) {
        this.inventoryDataSource = inventoryDataSource;
        this.orderDataSource = orderDataSource;
    }

    public void placeOrder(String productId, int amount) throws Exception {

        UserTransactionImp utx = new UserTransactionImp();
        String orderId = UUID.randomUUID()
            .toString();
        boolean rollback = false;
        try {
            utx.begin();
            Connection inventoryConnection = inventoryDataSource.getConnection();
            Connection orderConnection = orderDataSource.getConnection();
            Statement s1 = inventoryConnection.createStatement();
            String q1 = "update Inventory set balance = balance - " + amount + " where productId ='" + productId + "'";
            s1.executeUpdate(q1);
            s1.close();
            Statement s2 = orderConnection.createStatement();
            String q2 = "insert into Orders values ( '" + orderId + "', '" + productId + "', " + amount + " )";
            s2.executeUpdate(q2);
            s2.close();
            inventoryConnection.close();
            orderConnection.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            rollback = true;
        } finally {
            if (!rollback)
                utx.commit();
            else
                utx.rollback();
        }

    }

}

说明:placeOrder方法中,分别在两个数据源中执行了SQL语句。

测试用例DirectApplicationTest.java

public class DirectApplicationTest {
    
    private static DataSource inventoryDataSource;
    private static DataSource orderDataSource;

    private static String productId = UUID.randomUUID()
            .toString();

    @Test
    public void testPlaceOrderSuccess() throws Exception {
        int amount = 1;
        long initialBalance = getBalance(inventoryDataSource, productId);
        DirectApplication application = new DirectApplication(inventoryDataSource, orderDataSource);
        application.placeOrder(productId, amount);
        long finalBalance = getBalance(inventoryDataSource, productId);
        assertEquals(initialBalance - amount, finalBalance);
    }

    @Test
    public void testPlaceOrderFailure() throws Exception {
        int amount = 10;
        long initialBalance = getBalance(inventoryDataSource, productId);
        DirectApplication application = new DirectApplication(inventoryDataSource, orderDataSource);
        application.placeOrder(productId, amount);
        long finalBalance = getBalance(inventoryDataSource, productId);
        assertEquals(initialBalance, finalBalance);
    }

    @BeforeAll
    public static void setUp() throws SQLException {
        // 设置derby数据目录
        System.setProperty("derby.system.home", Paths.get("target").toAbsolutePath().toString());

        inventoryDataSource = getDataSource("db1");
        orderDataSource = getDataSource("db2");
        Connection inventoryConnection = inventoryDataSource.getConnection();
        Connection orderConnection = orderDataSource.getConnection();
        String createInventoryTable = "create table Inventory ( "
                + " productId VARCHAR ( 100 ) PRIMARY KEY, balance INT )";
        String createInventoryRow = "insert into Inventory values ( '" + productId + "', 10000 )";
        Statement s1 = inventoryConnection.createStatement();
        try {
            s1.executeUpdate(createInventoryTable);
        } catch (Exception e) {
            System.out.println("Inventory table exists");
        }
        try {
            s1.executeUpdate(createInventoryRow);
        } catch (Exception e) {
            System.out.println("Product row exists");
        }
        s1.close();
        String createOrderTable = "create table Orders ( orderId VARCHAR ( 100 ) PRIMARY KEY, productId VARCHAR ( 100 ), amount INT NOT NULL CHECK (amount <= 5) )";
        Statement s2 = orderConnection.createStatement();
        try {
            s2.executeUpdate(createOrderTable);
        } catch (Exception e) {
            System.out.println("Orders table exists");
        }
        s2.close();
        inventoryConnection.close();
        orderConnection.close();
    }

    private static DataSource getDataSource(String db) {

        DataSource ds;
        AtomikosDataSourceBean ads = new AtomikosDataSourceBean();
        ads.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
        Properties properties = new Properties();
        properties.put("databaseName", db);
        properties.put("createDatabase", "create");
        ads.setXaProperties(properties);
        ads.setUniqueResourceName(db);
        ads.setPoolSize(10); // optional
        ads.setBorrowConnectionTimeout(10); // optional
        ds = ads;
        return ds;

    }

    private static long getBalance(DataSource inventoryDataSource, String productId) throws Exception {

        UserTransactionImp utx = new UserTransactionImp();
        utx.begin();
        Connection inventoryConnection = inventoryDataSource.getConnection();
        Statement s1 = inventoryConnection.createStatement();
        String q1 = "select balance from Inventory where productId='" + productId + "'";
        ResultSet rs1 = s1.executeQuery(q1);
        if (rs1 == null || !rs1.next())
            throw new Exception("Product not found: " + productId);
        long balance = rs1.getLong(1);
        inventoryConnection.close();
        utx.commit();
        return balance;

    }

说明:创建数据库时,Orders表的amount字段定义了规则,这个规则可能导致执行SQL语句失败。

Atomikos + Derby + Spring 示例

使用Bean方式定义数据源,代码如下:

    @Configuration
    @EnableTransactionManagement
    public static class Config {
        static {
            // 设置derby数据目录
            System.setProperty("derby.system.home", Paths.get("target").toAbsolutePath().toString());
        }

        /**
         * 库存 数据源
         * @return
         */
        @Bean(initMethod = "init", destroyMethod = "close")
        public AtomikosDataSourceBean inventoryDataSource() {
            AtomikosDataSourceBean dataSource = new AtomikosDataSourceBean();
            dataSource.setLocalTransactionMode(true);
            dataSource.setUniqueResourceName("db1");
            dataSource.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
            Properties xaProperties = new Properties();
            xaProperties.put("databaseName", "db1");
            xaProperties.put("createDatabase", "create");
            dataSource.setXaProperties(xaProperties);
            dataSource.setPoolSize(10);
            return dataSource;
        }

        /**
         * 订单 数据源
         * @return
         */
        @Bean(initMethod = "init", destroyMethod = "close")
        public AtomikosDataSourceBean orderDataSource() {
            AtomikosDataSourceBean dataSource = new AtomikosDataSourceBean();
            dataSource.setLocalTransactionMode(true);
            dataSource.setUniqueResourceName("db2");
            dataSource.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
            Properties xaProperties = new Properties();
            xaProperties.put("databaseName", "db2");
            xaProperties.put("createDatabase", "create");
            dataSource.setXaProperties(xaProperties);
            dataSource.setPoolSize(10);
            return dataSource;
        }

        /**
         * 事务管理器
         * 
         * @return
         * @throws SystemException
         */
        @Bean(initMethod = "init", destroyMethod = "close")
        public UserTransactionManager userTransactionManager() throws SystemException {
            UserTransactionManager userTransactionManager = new UserTransactionManager();
            userTransactionManager.setTransactionTimeout(300);
            userTransactionManager.setForceShutdown(true);
            return userTransactionManager;
        }

        /**
         * JTA事务管理器
         * 
         * @return
         * @throws SystemException
         */
        @Bean
        public JtaTransactionManager jtaTransactionManager() throws SystemException {
            JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
            jtaTransactionManager.setTransactionManager(userTransactionManager());
            jtaTransactionManager.setUserTransaction(userTransactionManager());
            return jtaTransactionManager;
        }

        @Bean
        public SpringApplication application() {
            return new SpringApplication(inventoryDataSource(), orderDataSource());
        }
    }

说明:在Spring上下文中创建了两个数据源,JTA事务管理器等实例。测试用例如下(SpringApplicationTest.java):

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { Config.class })
public class SpringApplicationTest {
    
    private static String productId = UUID.randomUUID()
        .toString();

    @Autowired
    SpringApplication application;

    @Autowired
    DataSource inventoryDataSource;

    @Autowired
    DataSource orderDataSource;

    @Test
    public void testPlaceOrderSuccess() throws Exception {
        int amount = 1;
        long initialBalance = getBalance(inventoryDataSource, productId);
        application.placeOrder(productId, amount);
        long finalBalance = getBalance(inventoryDataSource, productId);
        assertEquals(initialBalance - amount, finalBalance);
    }

    @Test
    public void testPlaceOrderFailure() throws Exception {
        int amount = 10;
        long initialBalance = getBalance(inventoryDataSource, productId);
        try {
            application.placeOrder(productId, amount);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        long finalBalance = getBalance(inventoryDataSource, productId);
        assertEquals(initialBalance, finalBalance);
    }

    @BeforeAll
    public void setUp() throws SQLException {
        Connection inventoryConnection = inventoryDataSource.getConnection();
        Connection orderConnection = orderDataSource.getConnection();
        String createInventoryTable = "create table Inventory ( " + " productId VARCHAR ( 100 ) PRIMARY KEY, balance INT )";
        String createInventoryRow = "insert into Inventory values ( '" + productId + "', 10000 )";
        Statement s1 = inventoryConnection.createStatement();
        try {
            s1.executeUpdate(createInventoryTable);
        } catch (Exception e) {
            System.out.println("Inventory table exists");
        }
        try {
            s1.executeUpdate(createInventoryRow);
        } catch (Exception e) {
            System.out.println("Product row exists");
        }
        s1.close();
        String createOrderTable = "create table Orders ( orderId VARCHAR ( 100 ) PRIMARY KEY, productId VARCHAR ( 100 ), amount INT NOT NULL CHECK (amount <= 5) )";
        Statement s2 = orderConnection.createStatement();
        try {
            s2.executeUpdate(createOrderTable);
        } catch (Exception e) {
            System.out.println("Orders table exists");
        }
        s2.close();
        inventoryConnection.close();
        orderConnection.close();
    }

    private static long getBalance(DataSource inventoryDataSource, String productId) throws Exception {

        Connection inventoryConnection = inventoryDataSource.getConnection();
        Statement s1 = inventoryConnection.createStatement();
        String q1 = "select balance from Inventory where productId='" + productId + "'";
        ResultSet rs1 = s1.executeQuery(q1);
        if (rs1 == null || !rs1.next())
            throw new Exception("Product not found: " + productId);
        long balance = rs1.getLong(1);
        inventoryConnection.close();
        return balance;

    }

Atomikos + Derby + JPA 示例

我们需要创建实体,数据访问接口等类,这不是本文的重点,代码可以到仓库中查看。下面是库存(Inventory)配置类 InventoryConfig.java

@Configuration
@EnableJpaRepositories(basePackages = "io.github.kavahub.learnjava.jpa.inventory", entityManagerFactoryRef = "inventoryEntityManager", transactionManagerRef = "transactionManager")
public class InventoryConfig {
    static {
        // 设置derby数据目录
        System.setProperty("derby.system.home", Paths.get("target").toAbsolutePath().toString());
    }
    
    /**
     * 库存 数据源
     * 
     * @return
     */
    @Bean(initMethod = "init", destroyMethod = "close")
    public AtomikosDataSourceBean inventoryDataSource() {
        AtomikosDataSourceBean dataSource = new AtomikosDataSourceBean();
        dataSource.setLocalTransactionMode(true);
        dataSource.setUniqueResourceName("db1");
        dataSource.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
        Properties xaProperties = new Properties();
        xaProperties.put("databaseName", "db1");
        xaProperties.put("createDatabase", "create");
        dataSource.setXaProperties(xaProperties);
        dataSource.setPoolSize(10);
        return dataSource;
    }

    /**
     * 库存 实体管理器
     * 
     * @return
     */
    @Bean
    public EntityManagerFactory inventoryEntityManager() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("io.github.kavahub.learnjava.jpa.inventory");
        factory.setDataSource(inventoryDataSource());
        Properties jpaProperties = new Properties();
        //jpaProperties.put("hibernate.show_sql", "true");
        //jpaProperties.put("hibernate.format_sql", "true");
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
        jpaProperties.put("hibernate.current_session_context_class", "jta");
        jpaProperties.put("javax.persistence.transactionType", "jta");
        jpaProperties.put("hibernate.transaction.manager_lookup_class", "com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup");
        jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop");
        factory.setJpaProperties(jpaProperties);
        factory.afterPropertiesSet();
        return factory.getObject();
    }

}

订单(Order)配置类 OrderConfig.java代码如下:

@Configuration
@EnableJpaRepositories(basePackages = "io.github.kavahub.learnjava.jpa.order", entityManagerFactoryRef = "orderEntityManager", transactionManagerRef = "transactionManager")
public class OrderConfig {
    static {
        // 设置derby数据目录
        System.setProperty("derby.system.home", Paths.get("target").toAbsolutePath().toString());
    }
    
    /**
     * 订单 数据源
     * @return
     */
    @Bean(initMethod = "init", destroyMethod = "close")
    public AtomikosDataSourceBean orderDataSource() {
        AtomikosDataSourceBean dataSource = new AtomikosDataSourceBean();
        dataSource.setLocalTransactionMode(true);
        dataSource.setUniqueResourceName("db2");
        dataSource.setXaDataSourceClassName("org.apache.derby.jdbc.EmbeddedXADataSource");
        Properties xaProperties = new Properties();
        xaProperties.put("databaseName", "db2");
        xaProperties.put("createDatabase", "create");
        dataSource.setXaProperties(xaProperties);
        dataSource.setPoolSize(10);
        return dataSource;
    }

    /**
     * 订单 实体管理器
     * 
     * @return
     */
    @Bean
    public EntityManagerFactory orderEntityManager() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("io.github.kavahub.learnjava.jpa.order");
        factory.setDataSource(orderDataSource());
        Properties jpaProperties = new Properties();
        //jpaProperties.put("hibernate.show_sql", "true");
        //jpaProperties.put("hibernate.format_sql", "true");
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
        jpaProperties.put("hibernate.current_session_context_class", "jta");
        jpaProperties.put("javax.persistence.transactionType", "jta");
        jpaProperties.put("hibernate.transaction.manager_lookup_class", "com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup");
        jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop");
        factory.setJpaProperties(jpaProperties);
        factory.afterPropertiesSet();
        return factory.getObject();
    }
}

Spring主配置代码:

@Configuration
    @EnableTransactionManagement
    public static class Config {
        /**
         * 配置事务管理器
         * @return
         * @throws SystemException
         */
        @Bean(initMethod = "init", destroyMethod = "close")
        public UserTransactionManager userTransactionManager() throws SystemException {
            UserTransactionManager userTransactionManager = new UserTransactionManager();
            userTransactionManager.setTransactionTimeout(300);
            userTransactionManager.setForceShutdown(true);
            return userTransactionManager;
        }

        /**
         * 配置JTA事务管理器
         * @return
         * @throws SystemException
         */
        @Bean
        public JtaTransactionManager transactionManager() throws SystemException {
            JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
            jtaTransactionManager.setTransactionManager(userTransactionManager());
            jtaTransactionManager.setUserTransaction(userTransactionManager());
            return jtaTransactionManager;
        }

        @Bean
        public JPAApplication application() {
            return new JPAApplication();
        }

    }

测试用例如下:

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { Config.class, InventoryConfig.class, OrderConfig.class })
public class JPAApplicationTest {
    
    private static String productId = UUID.randomUUID()
        .toString();

    @Autowired
    JPAApplication application;

    @Autowired
    InventoryRepository inventoryRepository;

    @Autowired
    OrderRepository orderRepository;

    @Test
    public void testPlaceOrderSuccess() throws Exception {
        int amount = 1;
        long initialBalance = getBalance(inventoryRepository, productId);
        application.placeOrder(productId, amount);
        long finalBalance = getBalance(inventoryRepository, productId);
        assertEquals(initialBalance - amount, finalBalance);
    }

    @Test
    public void testPlaceOrderFailure() throws Exception {
        int amount = 10;
        long initialBalance = getBalance(inventoryRepository, productId);
        try {
            application.placeOrder(productId, amount);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        long finalBalance = getBalance(inventoryRepository, productId);
        assertEquals(initialBalance, finalBalance);
    }

    @BeforeAll
    public void setUp() throws SQLException {

        Inventory inventory = new Inventory();
        inventory.setProductId(productId);
        inventory.setBalance(Long.valueOf(10000));
        inventoryRepository.save(inventory);

    }

    private static long getBalance(InventoryRepository inventoryRepository, String productId) throws Exception {
        return inventoryRepository.findById(productId).orElseThrow(() -> new EntityNotFoundException(productId))
            .getBalance();

    }
}

最后

全部的代码,可以在这里查看 atomikos欢迎顶赞,感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值