apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.87k stars 6.73k forks source link

use spring boot 2.6.4 , transaction do not roll back #16117

Closed shangwq closed 2 years ago

shangwq commented 2 years ago

use the lastest version spring boot 2.6.4 and shardingsphere 5.1.0 transaction do not roll back

ps. use spring boot 2.5.x and 2.6.x has this same problem,spring boot 2.4.x is ok

zhaojinchao95 commented 2 years ago

@shangwq Hello, Can you provide a demo project?

shangwq commented 2 years ago

it's a simple project

the main dependencies is

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.4</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
    <version>5.1.0</version>
</dependency>

application.properties

mybatis.mapper-locations = classpath*:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case = true

spring.shardingsphere.props.sql-show = true
spring.shardingsphere.datasource.names = master,slave

spring.shardingsphere.datasource.master.type = com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url = jdbc:mysql://xxx:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8&allowMultiQueries=true
spring.shardingsphere.datasource.master.username = xxx
spring.shardingsphere.datasource.master.password = xxx

spring.shardingsphere.datasource.slave.type = com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.slave.jdbc-url = jdbc:mysql://xxx:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8&allowMultiQueries=true
spring.shardingsphere.datasource.slave.username = xxx
spring.shardingsphere.datasource.slave.password = xxx

Unit Test

@SpringBootApplication
@MapperScan(basePackages = {"com.example.demo"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

@Transactional(rollbackFor = Exception.class)
@SpringBootTest(classes = {DemoApplication.class})
class DemoApplicationTests {

    @Resource
    private AddressMapper addressMapper;

    @Test
    void test() {
        AddressModel address = new AddressModel();
        address.setAddressId(111L);
        address.setContactMobile("18888888888");
        address.setContactName("Tom");
        addressMapper.insertSelective(address);
    }

}

normally when the unit test completed, the transaction will be roll back. but actually the data Tom was still in database, do not roll back.

when i change spring boot version to 2.4.13, the transaction can roll back successfully

jingshanglu commented 2 years ago

Maybe it is problem of spring boot, when exception reported, spring not send a rollback.

user20161119 commented 2 years ago

shardingsphere 5.1.0 and spring boot 2.6.3 has the same issue according transaction log,spring transaction management roll back transaction,but the data saved into db. 1650537934(1)

shangwq commented 2 years ago

@user20161119 downgrade spring boot to 2.6.2 can fix it temporarily.

user20161119 commented 2 years ago

@user20161119 downgrade spring boot to 2.6.2 can fix it temporarily.

It's work,thanks

user20161119 commented 2 years ago

@user20161119 downgrade spring boot to 2.6.2 can fix it temporarily.

Could you explain the issue and what's the plan for the issue to fix?

thanks

user20161119 commented 2 years ago

@shangwq Hello, Can you provide a demo project?

@zjcnb There is a plan to fix the issue?

zhaojinchao95 commented 2 years ago

@user20161119 @shangwq @jingshanglu Hi, I think this problem not belong ShardingSphere. Because, when we use @Transaction in unit test. the isDefaultRollback(TestContext testContext) method has problem.

image
user20161119 commented 2 years ago

@zjcnb I tested with spring boot 2.6.3 with jdbc,transaction worked as excepted,the data did not save to db

image

image

zhaojinchao95 commented 2 years ago

shardingsphere-jdbc-pools-test.zip

Refer to this example.

user20161119 commented 2 years ago

@user20161119 @shangwq @jingshanglu Hi, I think this problem not belong ShardingSphere. Because, when we use @Transaction in unit test. the isDefaultRollback(TestContext testContext) method has problem.

image

You said the issues is not belong Sharding-jdbc,i tested with jdbc,the transaction work as excepted, the issue is belong to sharding-jdbc?

zhaojinchao95 commented 2 years ago

@user20161119 @shangwq @jingshanglu Hi, I think this problem not belong ShardingSphere. Because, when we use @Transaction in unit test. the isDefaultRollback(TestContext testContext) method has problem.

image

You said the issues is not belong Sharding-jdbc,i tested with jdbc,the transaction work as excepted, the issue is belong to sharding-jdbc?

Support your project demo please.

user20161119 commented 2 years ago

springtrx.zip

run junit test com.example.demo.DBServiceTest.testAdd()

zhaojinchao95 commented 2 years ago

\

springtrx.zip

run junit test com.example.demo.DBServiceTest.testAdd()

Did you use ShardingSphere for this project???

user20161119 commented 2 years ago

@zjcnb I tested with spring boot 2.6.3 with jdbc,transaction worked as excepted,the data did not save to db

image

image

This project do not depends on shardingSphere,I just want to verify my above question it's spring boot transaction issue or ShardingSpere issue.

shangwq commented 2 years ago

shardingsphere-jdbc-pools-test.zip

Refer to this example.

upgrade spring boot version to 2.6.4 or above, will be reproduction.

yanghao888 commented 2 years ago

18008 Is it the same as this issue?