|
| 1 | +/* |
| 2 | + * Copyright © 2018 organization baomidou |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.baomidou.dynamic.datasource.fixture.v1; |
| 17 | + |
| 18 | +import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; |
| 19 | +import com.baomidou.dynamic.datasource.creator.DataSourceProperty; |
| 20 | +import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator; |
| 21 | +import com.baomidou.dynamic.datasource.fixture.v1.service.tx.*; |
| 22 | +import com.baomidou.dynamic.datasource.tx.TransactionContext; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.boot.SpringApplication; |
| 26 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 27 | +import org.springframework.boot.test.context.SpringBootTest; |
| 28 | +import org.springframework.transaction.support.TransactionSynchronization; |
| 29 | + |
| 30 | +import javax.sql.DataSource; |
| 31 | +import java.util.Arrays; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 35 | +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
| 36 | + |
| 37 | + |
| 38 | +@SpringBootTest(classes = DsTransactionalApplication.class, webEnvironment = RANDOM_PORT) |
| 39 | +public class DsTransactionalTest { |
| 40 | + @Autowired |
| 41 | + private OrderService orderService; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private AccountService accountService; |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private ProductService productService; |
| 48 | + |
| 49 | + @Autowired |
| 50 | + DataSource dataSource; |
| 51 | + |
| 52 | + @Autowired |
| 53 | + DefaultDataSourceCreator dataSourceCreator; |
| 54 | + private DynamicRoutingDataSource ds; |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testDsTransactional() { |
| 58 | + DataSourceProperty orderDataSourceProperty = createDataSourceProperty("order"); |
| 59 | + DataSourceProperty productDataSourceProperty = createDataSourceProperty("product"); |
| 60 | + DataSourceProperty accountDataSourceProperty = createDataSourceProperty("account"); |
| 61 | + ds = (DynamicRoutingDataSource) dataSource; |
| 62 | + ds.addDataSource(orderDataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(orderDataSourceProperty)); |
| 63 | + ds.addDataSource(productDataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(productDataSourceProperty)); |
| 64 | + ds.addDataSource(accountDataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(accountDataSourceProperty)); |
| 65 | + PlaceOrderRequest placeOrderRequest = new PlaceOrderRequest(1, 1, 22, OrderStatus.INIT); |
| 66 | + |
| 67 | + //商品不足 |
| 68 | + TransactionContext.registerSynchronization(new TransactionSynchronization() { |
| 69 | + @Override |
| 70 | + public void afterCompletion(int status) { |
| 71 | + if (status == STATUS_ROLLED_BACK) { |
| 72 | + placeOrderRequest.setOrderStatus(OrderStatus.FAIL); |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + assertThrows(RuntimeException.class, () -> orderService.placeOrder(placeOrderRequest)); |
| 77 | + assertThat(placeOrderRequest.getOrderStatus()).isEqualTo(OrderStatus.FAIL); |
| 78 | + assertThat(orderService.selectOrders()).isEmpty(); |
| 79 | + assertThat(accountService.selectAccount()).isEqualTo(new Account(1, 50.0)); |
| 80 | + assertThat(productService.selectProduct()).isEqualTo(new Product(1, 10.0, 20)); |
| 81 | + |
| 82 | + //账户不足 |
| 83 | + TransactionContext.registerSynchronization(new TransactionSynchronization() { |
| 84 | + @Override |
| 85 | + public void afterCompletion(int status) { |
| 86 | + if (status == STATUS_ROLLED_BACK) { |
| 87 | + placeOrderRequest.setOrderStatus(OrderStatus.FAIL); |
| 88 | + } |
| 89 | + } |
| 90 | + }); |
| 91 | + placeOrderRequest.setAmount(6); |
| 92 | + placeOrderRequest.setOrderStatus(OrderStatus.INIT); |
| 93 | + assertThrows(RuntimeException.class, () -> orderService.placeOrder(placeOrderRequest)); |
| 94 | + assertThat(placeOrderRequest.getOrderStatus()).isEqualTo(OrderStatus.FAIL); |
| 95 | + assertThat(orderService.selectOrders()).isEmpty(); |
| 96 | + assertThat(accountService.selectAccount()).isEqualTo(new Account(1, 50.0)); |
| 97 | + assertThat(productService.selectProduct()).isEqualTo(new Product(1, 10.0, 20)); |
| 98 | + |
| 99 | + //正常下单 |
| 100 | + TransactionContext.registerSynchronization(new TransactionSynchronization() { |
| 101 | + @Override |
| 102 | + public void afterCommit() { |
| 103 | + placeOrderRequest.setOrderStatus(OrderStatus.SUCCESS); |
| 104 | + } |
| 105 | + }); |
| 106 | + placeOrderRequest.setAmount(5); |
| 107 | + placeOrderRequest.setOrderStatus(OrderStatus.INIT); |
| 108 | + assertThat(orderService.placeOrder(placeOrderRequest)).isEqualTo(OrderStatus.INIT); |
| 109 | + assertThat(placeOrderRequest.getOrderStatus()).isEqualTo(OrderStatus.SUCCESS); |
| 110 | + assertThat(orderService.selectOrders()).isEqualTo(Arrays.asList(new Order(3, 1, 1, 5, 50.0))); |
| 111 | + assertThat(accountService.selectAccount()).isEqualTo(new Account(1, 0.0)); |
| 112 | + assertThat(productService.selectProduct()).isEqualTo(new Product(1, 10.0, 15)); |
| 113 | + } |
| 114 | + |
| 115 | + private DataSourceProperty createDataSourceProperty(String poolName) { |
| 116 | + DataSourceProperty result = new DataSourceProperty(); |
| 117 | + result.setPoolName(poolName); |
| 118 | + result.setDriverClassName("org.h2.Driver"); |
| 119 | + result.setUrl("jdbc:h2:mem:" + poolName + ";INIT=RUNSCRIPT FROM 'classpath:db/ds-with-transactional.sql'"); |
| 120 | + result.setUsername("sa"); |
| 121 | + result.setPassword(""); |
| 122 | + return result; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +@SpringBootApplication |
| 127 | +class DsTransactionalApplication { |
| 128 | + public static void main(String[] args) { |
| 129 | + SpringApplication.run(DsTransactionalApplication.class, args); |
| 130 | + } |
| 131 | +} |
0 commit comments