Spring Boot

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

2020-07-09  本文已影响0人  EasyNetCN

项目中使用了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即可

上一篇 下一篇

猜你喜欢

热点阅读