SpringBoot 项目运行时突现神秘错误:SqlSession 未注册,项目代码无改动,该如何解决?

springboot 项目运行时突现神秘错误

近期,一位开发人员在springboot项目运行过程中遭遇了一个困扰其许久的错误:

creating a new sqlsession
sqlsession [org.apache.ibatis.session.defaults.defaultsqlsession@2dbe837b] was not registered for synchronization because synchronization is not active

令人诧异的是,项目代码并未做任何改动,以往运行良好。困惑不解的开发人员在网上苦苦搜寻答案,却发现这个错误并不是常见的代码错误。

进一步排查,开发人员检查了项目的yml配置文件,发现相关配置如下:

server:
  port: 8080
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.driver
      url: jdbc:mysql://110.35.20.7:3306/cos?useunicode=true&characterencoding=utf8&autoreconnect=true&allowmultiqueries=true&zerodatetimebehavior=converttonull&usessl=false&servertimezone=asia/shanghai
      username: mes_it
      password: 123456

mybatis-plus:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

检查配置后,开发人员发现并没有明显的错误。正当他一筹莫展之际,一位经验丰富的程序员为他指点了一条明路:

解决办法:

前往 csdn 博客网址 https://blog.csdn.net/web1829...,查看一篇题为《sqlsession [org.apache.ibatis.session.defaults.defaultsqlsession@2dbe837b] was not registered for synchronization because synchronization is not active》的文章。

这篇文章详细分析了导致该错误的原因,并给出了相应的解决方案。按照文章中的指导,开发人员在项目中添加了以下代码:

@Configuration
public class MyBatisConfig {

  @Bean
  @Scope("prototype")
  @Primary
  pub

lic SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) { return new SqlSessionFactoryBean().getObject(); } }

添加代码后,开发人员重新启动项目,错误消失,项目恢复正常运行。