当前位置:网站首页>SqlSession [[email protected]]
SqlSession [[email protected]]
2022-08-03 02:24:00 【市井榴芒】
1、报错场景
在搞SpringBoot项目的时候,把Mybatis打印日志的配置打开后,发现每条Sql打印的时候都会在前面打印如下的信息,强迫症受不了,得查查为什么;
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@521978e4] was not registered for synchronization because synchronization is not active
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@5bb2b956] will not be managed by Spring
==> Preparing: select * from xxx where xxx = ? and xxx = ?
==> Parameters: opHQA5gq2qfn1DRl069YOLv1Wv0Q(String), 0(Integer)
<== Columns: xxx, xxx, xxx, xxx
<== Row: xxx, xxx, xxx, xxx
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@521978e4]
前面打印的内容意思大概是:这次的SqlSession未注册进来,同步关着呢哎,所以你这玩意Spring不会管理的;
2、问题原因及解决办法
原因就是你现在这个事务吧,不是Spring管理着呢,是你的数据库插件Mybatis或者MybatisPlus给你管理呢,所以Spring给你温馨提醒了一下而已,又没有报错!但是,有一种情况,比如你的方法里面逻辑比较多,增删改查的SQL比较多的话,而你自己又不清楚事务怎么管理的话,推荐你把@Transactional给加到方法上面,让Spring接管起来当前的事务,将来报错更好回滚。
当你把@Transactional加到方法上面时;
@Override
@Transactional
public Boolean xxxx(String xxx, String xxx) {
Txxx txxx = txxxMapper.xxx(xxx, Integer.parseInt(xxx));
//...
return true;
}
再次打印日志的话就变成了这样:
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3ba27661]
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@57d27e73] will be managed by Spring
==> Preparing: select * from xxx where xxx = ? and xxx = ?
==> Parameters: opHQA5gq2qfn1DRl069YOLv1Wv1Q(String), 0(Integer)
<== Columns: xxx, xxx, xxx, xxx
<== Row: xxx, xxx, xxx, xxx
<== Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3ba27661]
Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3ba27661]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3ba27661]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3ba27661]
能看见Spring已经will be managed by Spring帮你管理了;
边栏推荐
猜你喜欢
随机推荐
leetcode:139. 单词拆分
工作两年成跳槽高峰期,程序员会在一家公司待多久?
【Objective-C语言中的@property增强】
05-分布式计算框架
VS中使用BugTrap定位程序崩溃点
Spark SQL简介
SPI机制是什么?
How does Excel compare if two columns of strings are the same?
46LVS+Keepalived群集
FLIR E95 在8层楼看马路上行驶的CAR的热成像形态?
堆的应用:堆排序和TOP-K问题
【Flink】使用arthas在线诊断flink的那些事
Mysql-如何进行慢SQL查询
二叉树的前序遍历、中序遍历、后序遍历和层序遍历
C语言入门--指针
vs studio install opencv environment
leetcode:140. 单词拆分 II
5.软件测试-----自动化测试
vsftp容器搭建+go开发web用户管理界面(更新于2022.02.23)
Go新项目-编译项目的细节(4)









