springboot + mybatisplus出现was not registered for synchronization because synchronization is not active
原因一:缺少事务注解,底层mybatisplus的接口方法有事务
原因二:该服务器被限制访问要连接的数据库
原因三:乐观锁失效
乐观锁由@version注解标注,有以下使用要求
支持的数据类型只有:int、Integer、long、LonDate、Timestamp、LocalDateTime
整数类型下 newVersion = oldVersion + 1
newVersion 会回写到 entity 中
仅支持 updateById(id) 与 update(entity, wrapper) 方法
在 update(entity, wrapper) 方法下, wrapper 不能复用
拓展研究:
问题一、为什么每次都Creating a new SqlSession,会不会影响性能?
Creating a new SqlSession mybatis官方就是这么和spring整合的,SqlSession是对Connection的封装,Connection都在池里呢,没什么影响。
那个是信息,就是在一个数据库连接上,启动事务,创建一个对象来管理事务,就是在数据库连接上发出transaction.start,对象来记录相关信息。对性能影响很少
问题二、明明配置了事务,确提示事务没有交给spring管理,求解?
只有连接不是spring管理的(应该是应用服务器上建立的),实际上事务还是spring管理,报错还是会回滚。
乐观锁实现方式:
版本号
- 取出记录时,获取当前version
- 更新时,带上这个version
- 执行更新时, set version = newVersion where version = oldVersion
- 如果version不对,就更新失败
时间版本
- 实现MetaObjectHandler拦截器,对要判断的时间字段进行拦截,
- 时间字段加上对应注解 @TableField(value = "UPDATE_TIME",fill = FieldFill.UPDATE)
- 取出记录时,获取当前time
- 更新时,带上这个time
- 执行更新时, set time = newTime where version = oldTime
- 如果 time 不对,就更新失败
springboot + mybatisplus出现was not registered for synchronization because synchronization is not active的更多相关文章
- spring-boot+mybatisPlus+shiro的集成demo 我用了5天
spring-boot + mybatis-plus + shiro 的集成demo我用了五天 关于shiro框架,我还是从飞机哪里听来的,就连小贱都知道,可我母鸡啊.简单百度了下,结论很好上手,比s ...
- IDEA上创建 Maven SpringBoot+mybatisplus+thymeleaf 项目
概述 在WEB领域,Java也是在不断的探索和改进,从开始的JSP--->Struts1--->Struts2+Spring--->Spring MVC--->SpringBo ...
- was not registered for synchronization because synchronization is not active
报SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7862f70e] was not registered for s ...
- springboot+mybatisplus 测试代码生成
测试springboot + mybatisplus 实现代码生成 使用默认的模板引擎 pom.xml文件 <?xml version="1.0" encoding=&q ...
- SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fa5519] was not registered for synchronization because synchronization is not active
Creating a new SqlSessionSqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fa5519] w ...
- 2、SpringBoot+MybatisPlus整合-------BaseCRUD
开发工具:STS 代码下载链接:GitHub管理代码 版本: Springboot:1.5.14.RELEASE 使用2.0以上的Springboot,会报出一些异常.欢迎知道异常原因的大牛解惑. M ...
- SpringBoot+MyBatisPlus整合时提示:Invalid bound statement(not found):**.dao.UserDao.queryById
场景 在使用SpringBoot+MyBatisPlus搭建后台启动项目时,使用EasyCode自动生成代码. 在访问后台接口时提示: Invilid bound statement (not fou ...
- springboot+mybatisplus+sharding-jdbc分库分表实例
项目实践 现在Java项目使用mybatis多一些,所以我也做了一个springboot+mybatisplus+sharding-jdbc分库分表项目例子分享给大家. 要是用的springboot+ ...
- springboot + mybatisPlus 入门实例 入门demo
springboot + mybatisPlus 入门实例 入门demo 使用mybatisPlus的优势 集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用ma ...
- 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享四:部署到阿里云
框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 阿里云开放必要端口,mysql与t ...
随机推荐
- encodeURI和encodeURIComponent
encodeURI和encodeURIComponent的作用对象都是URL,唯一的区别就是编码的字符范围: encodeURI不会对ascii字母.数字.~!@#$&*()=:/,;?+' ...
- keepalived检测UDP端口
keepalived支持的健康检测方式有:HTTP_GET|SSL_GET.TCP_CHECK.SMTP_CHECK.MISC_CHECK. 由于keepalived自身并不支持udp检测,有TCP_ ...
- 1238. 循环码排列 (Medium)
问题描述 1238. 循环码排列 (Medium) 给你两个整数 n 和 start.你的任务是返回任意 (0,1,2,,...,2^n-1) 的排列 p,并且满足: p[0] = start p[i ...
- 1996. 游戏中弱角色的数量 (Medium)
问题描述 1996. 游戏中弱角色的数量 (Medium) 你正在参加一个多角色游戏,每个角色都有两个主要属性: 攻击 和 防御 .给你一个二维整数数组 properties ,其中 properti ...
- LaTex【五】latex导入中文包,支持中文显示
\documentclass{article} \usepackage{CJKutf8} \begin{document} \begin{CJK}{UTF8}{gbsn} 中文样例,UTF-8编码,字 ...
- ubuntu22.04安装mysql5.7
22版本的ubuntu默认安装mysql8.0版本,要想安装5.x的版本就得下载安装,这个比较详细的教程,可以参考: https://www.cnblogs.com/juanxincai/p/1648 ...
- hadoop模板虚拟机配置
在安装好虚拟机软件后,进行IP配置 配置windows系统的ip 配置Vmware的ip 配置虚拟机的ip 首先 输入su root切换至root身份. 然后配置ip和网关 vim /etc/sysc ...
- nchu第二次面向对象编程博客作业
前言: 本次博客包含的内容有pta题目集4(四边形).5(五边形)以及期中考试三次题目集.其中第四次和第五次题目集难度较大,比较复杂,涉及的知识点也比较多.而期中考试由于是在课堂上完成,难度较小, ...
- php相关知识总结
class Father{ public static function getStatic(){ return new static(); } public static function getS ...
- scala流程控制
1.分支控制if-else 分支控制有三种:单分支.双分支.多分支: 1.1 单分支 (1).语法入下: if(条件表达式){ 执行代码块 //当条件表达式为true时,才会执行代码块内容 ...