question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

springboot 2.6.3 and shardingsphere 5.1.1 , transaction do not roll back

See original GitHub issue

When I use shardingsphere version 5.1.1, transaction rollback fails. When using version 5.0.0 or not, the transaction failure rollback is successful

<properties>    
<spring-boot.version>2.6.3</spring-boot.version>    
     <spring-cloud.version>2021.0.2</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.0.1.0</spring-cloud-alibaba.version>

<!--<sharding-sphere.version>5.0.0</sharding-sphere.version>-->
<sharding-sphere.version>5.0.0</sharding-sphere.version>
</>
 <dependencyManagement>
        <dependencies>
            <!-- spring boot 依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>


            <!-- spring cloud 依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring cloud alibaba 依赖 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.shardingsphere</groupId>
                <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
                <version>${sharding-sphere.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

server:
  tomcat:
    uri-encoding: UTF-8
    threads:
      max: 1000
      min-spare: 30
    connection-timeout: 5000ms
  port: 8090
  servlet:
    context-path: /sanyi/manager
spring:
  transaction:
    rollback-on-commit-failure: true
  jackson:
    default-property-inclusion: non_null
  # 不使用 shardingSphere
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://192.168.2.254:3307/sanyi_manager?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai

# 5.0.0 版本配置
  #shardingsphere:
  #  datasource:
  #    names: master
  #    master:
  #      type: com.zaxxer.hikari.HikariDataSource
  #      driver-class-name: com.mysql.cj.jdbc.Driver
  #      username: root
  #      password: root
  #      jdbc-url: jdbc:mysql://192.168.2.254:3307/sanyi_manager?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
  #  props:
  #    sql-show: true
  #  enabled: true
  #  rules:
  #    # 读写分离
  #    readwrite_splitting:
  #      data-sources:
  #        sanyi_manager:
  #          write-data-source-name: master
  #          read-data-source-names: master
  #          load-balancers-name: round-robin
  #      load-balancers:
  #        round-robin:
  #          type: ROUND_ROBIN
  #          props:
  #            workId: 1

  # 5.1.1 版本配置 数据源
  #shardingsphere:
  #  datasource:
  #    names: master
  #    master:
  #      type: com.zaxxer.hikari.HikariDataSource
  #      driver-class-name: com.mysql.cj.jdbc.Driver
  #      username: root
  #      password: root
  #      jdbc-url: jdbc:mysql://192.168.2.254:3307/sanyi_manager?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
  #  props:
  #    sql-show: true
  #  enabled: true
  #  rules:
  #    # 读写分离
  #    readwrite-splitting:
  #      data-sources:
  #        onn_auth:
  #          type: DYNAMIC
  #          props:
  #            auto-aware-data-source-name: master
  #            write-data-source-name: master
  #            read-data-source-names: master
  #          load-balancers-name: round-robin
  #      load-balancers:
  #        round-robin:
  #          type: ROUND_ROBIN
  #          props:
  #            workId: 1


# Spring 相关
# mybaits-plus配置
mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto
      table-underline: true
      logic-delete-value: 1
      logic-not-delete-value: 0
      db-column-underline: true
      refresh-mapper: true
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    call-setters-on-nulls: true
    jdbc-type-for-null: 'null'
    default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  type-enums-package: com.demo.**.enums


@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "status_record")
@Builder
public class StatusRecord implements Serializable {
    private static final long serialVersionUID = 1L;
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField(value = "project_id")
    private Integer projectId;
}

/**
 * by zhaowenyuan create 2022/5/27 17:45
 */
public interface StatusRecordMapper extends BaseMapper<StatusRecord> {
}



/**
 * by zhaowenyuan create 2022/5/27 17:45
 */
public interface StatusRecordService extends IService<StatusRecord> {

    void saveTest();

}


@Slf4j
@Service
public class StatusRecordServiceImpl extends ServiceImpl<StatusRecordMapper, StatusRecord> implements StatusRecordService {

    @Override
    @Transactional
    public void saveTest() {

        save(StatusRecord.builder()
                .projectId(11)
                .build());

        int s = 1 / 0;
    }
}


@Slf4j
@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringRunner.class)
public class DemoTest {

    @Autowired
    StatusRecordService statusRecordService;

    /**
     * success
     * shardingsphere version 5.0.0 and do not use shardingsphere
     * error
     * shardingsphere version 5.1.1
     *
     */
    @Test
    public void transactionalTest(){
        statusRecordService.saveTest();
    }

}

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
github-actions[bot]commented, Oct 8, 2022

Hello , this issue has not received a reply for several days. This issue is supposed to be closed.

0reactions
dragonorantcommented, Jun 2, 2022

我也有同样的问题。我使用的版本如下:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.6.7</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
  <groupId>org.apache.shardingsphere</groupId>
  <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
  <version>5.1.1</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.28</version>
  <scope>runtime</scope>
</dependency>

通过调试mysql驱动源码,发现这与一个useLocalSessionState参数有关。我注意到mysql官方文档中这个参数的默认值为false。但是在shardingsphere 5.1.1版本中,有这么一段代码来设置这个参数值。

图片

两个类中都存在类似的代码。 图片

shardingsphere 5.0.0 中没有这些修改数据库驱动参数值的行为。这有什么原因吗?

我尝试如下修改参数值,然后Spring的事务回滚行为正常生效。

图片

那么设置这个参数值的代码能不能改成false呢?

Thank you for your advice

Read more comments on GitHub >

github_iconTop Results From Across the Web

FAQ - Apache ShardingSphere
The use norm of Spring Namespace does not require to deploy xsd files to the official ... in spring boot project when integrating...
Read more >
Spring Boot Transaction not rolling back - jpa - Stack Overflow
The problem is that the transacction not rollback on exceptions, with or without the rollBackFor attribute.
Read more >
Search Results - CVE
Since Apache Hama is EOL, we do not expect these issues to be fixed. ... To revert to the original behaviour, the administrator...
Read more >
How does Apache ShardingSphere implement distributed ...
If not, a background process will clear the data. The app runs the rollback command, and the connection registered in the transaction that...
Read more >
PoC in GitHub - Domini's blog
CVE-2020-0976. A spoofing vulnerability exists when Microsoft SharePoint Server does not properly sanitize a specially crafted web request to an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found