美文网首页Spring Boot
数据库配置引发的事务失效

数据库配置引发的事务失效

作者: EasyNetCN | 来源:发表于2020-07-09 22:39 被阅读0次

    项目中使用了Spring Data JPA,在具体代码中会JPA操作和基于JdbcTemplate操作混合使用,有次发现了事务会失效

    bootstrap.yml中的数据库配置

    spring:
      application:
        name: goods-service
      jpa:
        show-sql: false
      datasource:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:mysql://${db.host:localhost}:${db.port:3306}/${db.name:test}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&autoReconnect=true
        username: ${db.username:}
        password: ${db.password:}
        hikari:
          data-source-properties:
            cachePrepStmts: true
            prepStmtCacheSize: 500
            prepStmtCacheSqlLimit: 2048
            useServerPrepStmts: true
            useLocalSessionState: true
            useLocalTransactionState: true
            rewriteBatchedStatements: true
            cacheResultSetMetadata: true
            cacheServerConfiguration: true
            elideSetAutoCommits: true
            maintainTimeStats: false
    

    JPA配置

    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    @Configuration
    @EnableJpaAuditing
    @EnableTransactionManagement
    @EntityScan("")
    @EnableJpaRepositories("")
    public class GoodsJpaConfig {
    
    }
    

    最后定位问题,移除了useLocalSessionState,useLocalTransactionState即可

    相关文章

      网友评论

        本文标题:数据库配置引发的事务失效

        本文链接:https://www.haomeiwen.com/subject/sfvecktx.html