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