前言

不多bb了,直接上代码和注释,看不懂的找博主

数据库初始状态

   //数据库中 id = 1 name = 0000 balance = 2222//数据库中 id = 2 name = aaaa balance = null//other id = 1 name = AAAA//other id = 2 name = BBBB

总结:

1、事物开启的时间

如果service中即使一开始就表明了执行事物,但是事物开启的时间也要算在执行数据库语句的时候开开始

举例:一开始表1中name为0000,打上断点,先进入service,不操作数据库。这个时候手动修改数据库中的name为1111,再执行查询语句,我们会发现取到了那么为1111的值,而不是0000(安装可重复读取的思想解决)(不论是可重复读REPEATABLE_READ还是不可重复读READ_COMMITTED

如果是sql(注意一定是sql)更新操作,我们在当前事物中进行了更新操作,其他事物更新相同id的数据,则其他事物不能修改,因为更新操作会将这条数据锁住(排它锁),其他事物不能更新执这条id的数据(可以使用select普通查看,hibernate save方法更新不会马上执行sql语句,它会等到事物最后全部执行完成、或者这条数据的记录直到本事物中对它进行数据库操作的时候才会执行sql语句,所以hibernate相关save更新按照代码情况进行锁住锁住,影响到别的事物对它进行修改)

->(包括,新开启了一个独立事物对它进行修改也不能完成),但是对于其他的id是可以任意修改的的,除非等当前事物执行完成或者当前事物回滚(总之就是当前事物结束)


### 2、可重复读取`REPEATABLE_READ`

事物开始执行的时候,相当于我们将数据库中进行了一个拍照,不论其他事物修改了其他或者本表任何表的任何数据,我们通过find看到的数据都是拍照的数据,也就是说即使其他事物对我们拿到的数据进行了更新,或者添加了新的数据,我们还是看不到。我们只能拿到照片中的数据。

3、不可重复读READ_COMMITTED 一般情况下建议使用

1、只要不是我们从当前事物中取到的数据,其他事物修改了其他或者本表任何表的任何数据,我们查询的话,就是显示最新的数据

2、如果我们正在执行的事物取到了数据,即使别的事物修改成什么样子,我们再查询的话,还是一样的(不管是hibernate查询还是sql查询),相当于只给我们当前查询过的数据拍了照片(其实这个是我实际项目中得到的结果,但是关于网上给出的其他事物修改提交了,这里再次执行,应该是得到最新的数据)

4、update操作和重新赋值给另一个对象实体中内容变化不变化

1、update 原生 sql语句不会更新当前实体对象,
2、如果是同一个事物中,如果是一个根据id 进行更新,并且返回了结果 ,hibernate的操作会即使重新new一个对象出来也会对原理的对象 进行更新
3、如果是中间独立事物进行了修改,返回了其他对象,不会更新当前实体对象。

DemoEntity demoEntity =  demoEntityRepository.findOne(id);isolationService.updateName(id,"zhang123456" ); @Modifying@Query("UPDATE  DemoEntity  d set d.name = ?2 where d.id = ?1")int updateName(Long id ,String name);//即使重新赋值了新对象one,上面的demoEntity 也会跟着变化,一个事物中的数据,具有连贯性,update语句,则是返回了一个当前行。DemoEntity one = isolationService.transRequirs(1L,"transRequirsOne");

下面主要是可重复读取的数据package com.hlj.springboot.dome.common.moudle.service;import com.hlj.springboot.dome.common.entity.DemoEntity;
import com.hlj.springboot.dome.common.entity.OtherEntity;
import com.hlj.springboot.dome.common.entity.repository.DemoEntityRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;/*** 作者 :HealerJean* 日期 :2019/1/23  下午8:02.* 类描述:*/
@Service
public class IsolationStartService {@Resourceprivate DemoEntityRepository demoEntityRepository ;@Resourceprivate IsolationService isolationService ;@Transactional(isolation = Isolation.READ_COMMITTED ,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)public DemoEntity startTransactional(Long id) {//数据库中 id = 1 name = 0000 balance = 2222//数据库中 id = 2 name = aaaa balance = null//other id = 1 name = AAAA//other id = 2 name = BBBB//这个时候修改id为1的name为1111,以下查询还没有开启,表明我们的事物目前为止其实还没有开启,因为没有涉及到数据库的操作,有了数据库操作,才会真正开启事物DemoEntity demoEntity =  demoEntityRepository.findOne(id);demoEntity.setName("startTransactional"); //还没有保存到数据库中,因为事物还没有提交,方法运行结束才会提现目前开启的这个事物,除非中间出现独立事物System.out.println(demoEntity);     //DemoEntity(id=1, name=startTransactional, balance=2222)//开始独立事物直接保存数据,直接在数据库中有显示DemoEntity transRequirsNew =   isolationService.transRequirsNew(id,"transRequirsNew");System.out.println(transRequirsNew); //DemoEntity(id=1, name=transRequirsNew, balance=2222)//解释:即使独立事物修改过,我们这里还是不会变,还是本事物中的数据(因为是两个事物,具有隔离性)System.out.println(demoEntity);  //DemoEntity(id=1, name=startTransactional, balance=2222)//当前事物下保存数据,上面的额startTransactional 事物还没有提交,所以和一开始的状态是一致的demoEntity = isolationService.transRequirs(id,"transRequirs");System.out.println(demoEntity) ; //DemoEntity(id=1, name=transRequirs, balance=2222)// 目前,直接看数据库中,name应该能是transRequirsNew, 手动修改数据库中的 name为 3333//读取已经提交的数据,这里没有开启独立事物,所以即使配置了,读已提交也不会生效,因为再我们这个方法一开始的时候,就已经确定了隔离性质demoEntity = isolationService.isoLationReadCommitedFind(id);System.out.println(demoEntity); // DemoEntity(id=1, name=transRequirs, balance=2222)// 可重复读 这个时候我们修改 2中的数据为 bbbb,会发现这个时候查询的时候,下面还为aaa ,// READ_COMMITTED 可重复读取:修改了之后,我们查询到的数据库是bbbb,说明READ_COMMITTED 生效了DemoEntity two = isolationService.transRequirsFind(2L);System.out.println(two) ;//可重复读          DemoEntity(id=2, name=aaaa, balance=null)//READ_COMMITTED   DemoEntity(id=2, name=bbbb, balance=null)//下面一起修改了即使换一个变量名字,demoEntity的变量还是会跟着变的DemoEntity one = isolationService.transRequirs(1L,"transRequirsOne");System.out.println(one) ; //DemoEntity(id=1, name=transRequirsOne, balance=2222)System.out.println(demoEntity) ; //DemoEntity(id=1, name=transRequirsOne, balance=2222)//可重复读 这个时候直接修改 这张表中的数据为CCCC,我们会发现本事物中查询的还是之前的结果AAAA,说明本事物开启之后,就会对数据库中的数据进行锁定(相当于是拍了张照片)//READ_COMMITTED 这个时候直接修改数据库 这张表中的数据为CCCC, 这里查询得到的是CCCC,说明不可重复读生效了OtherEntity otherEntity = isolationService.findOther(1L);System.out.println(otherEntity);//可重复读 OtherEntity(id=1, name=AAAA)//READ_COMMITTED OtherEntity(id=1, name=CCC)//可重复读 这个时候直接修改 这张表中的数据为DDDD,我们会发现本事物中查询的还是之前的结果BBBB//READ_COMMITTED 这个时候直接修改数据库 这张表中的数据为CCCC, 这里查询得到的是CCCC,说明不可重复读生效了OtherEntity otherEntityTwo = isolationService.findOther(2L);System.out.println(otherEntityTwo);//可重复读 OtherEntity(id=2, name=BBBB)//READ_COMMITTED OtherEntity(id=2, name=DDDD)//指定id更新不会影响demoEntity 现在的数据isolationService.updateName(id,"zhang123456" );System.out.println(demoEntity); //DemoEntity(id=1, name=transRequirsOne, balance=2222)System.out.println(transRequirsNew);//DemoEntity(id=1, name=transRequirsNew, balance=2222)return demoEntity;//上面两种的最终数据库中就 都变成了// id =1 name= zhang123456  balance = 2222// id =2 name= bbb balance = nulll//other表 id= 1 name =CCCC//id = 2 name =DDDD}}

1、接口

package com.hlj.springboot.dome.common.moudle.service;import com.hlj.springboot.dome.common.entity.DemoEntity;
import com.hlj.springboot.dome.common.entity.OtherEntity;/*** 作者 :HealerJean* 日期 :2019/1/23  下午7:34.* 类描述:*/
public interface IsolationService {/*** 方法中的事物为入口方法的事物* @param id* @return*/DemoEntity  transRequirs(Long id,String name);DemoEntity  transRequirsFind(Long id);/*** 开启一个事物* @param id* @return*/DemoEntity  transRequirsNew (Long id,String name);/*** 事物隔离级别为 读已提交()* @param id* @return*/DemoEntity   isoLationReadCommitedFind(Long id) ;int updateName(Long id,String name);OtherEntity findOther(Long id);}

3、接口实现类

package com.hlj.springboot.dome.common.moudle.service.impl;import com.hlj.springboot.dome.common.entity.DemoEntity;
import com.hlj.springboot.dome.common.entity.OtherEntity;
import com.hlj.springboot.dome.common.entity.repository.DemoEntityRepository;
import com.hlj.springboot.dome.common.entity.repository.OtherEntityRepository;
import com.hlj.springboot.dome.common.moudle.service.IsolationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;/*** 作者 :HealerJean* 日期 :2019/1/23  下午7:35.* 类描述:*/
@Service
@Slf4j
public class IsolationServiceImpl  implements IsolationService {@Resourceprivate DemoEntityRepository demoEntityRepository ;@Resourceprivate OtherEntityRepository otherEntityRepository ;@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)@Overridepublic DemoEntity transRequirs(Long id,String name) {DemoEntity demoEntity =  demoEntityRepository.findOne(id);demoEntity.setName(name);return demoEntityRepository.save(demoEntity);}@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)@Overridepublic DemoEntity transRequirsFind(Long id) {return demoEntityRepository.findOne(id);}@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)@Overridepublic DemoEntity transRequirsNew(Long id,String name) {DemoEntity demoEntity =  demoEntityRepository.findOne(id);demoEntity.setName(name);return demoEntityRepository.save(demoEntity);}@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)@Overridepublic DemoEntity isoLationReadCommitedFind(Long id) {return demoEntityRepository.findOne(id);}@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)@Overridepublic int updateName(Long id, String name) {return demoEntityRepository.updateName(id,name);}@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)@Overridepublic OtherEntity findOther(Long id) {return otherEntityRepository.findOne(id);}
}

感兴趣的,欢迎添加博主微信,

哈,博主很乐意和各路好友交流,如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,备注您的微信或者其他联系方式。添加博主微信哦。

请下方留言吧。可与博主自由讨论哦

微信 微信公众号 支付宝

事物以及事物隔离性的代码详解相关推荐

  1. yii mysql 事务处理_Yii2中事务的使用实例代码详解

    前言 一般我们做业务逻辑,都不会仅仅关联一个数据表,所以,会面临事务问题. 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全 ...

  2. 委托与事件代码详解与(Object sender,EventArgs e)详解

    委托与事件代码详解 using System; using System.Collections.Generic; using System.Text; namespace @Delegate //自 ...

  3. sip消息类型和消息代码详解-转

    在学习asterisk的时候,经常遇到一些远程服务器传回的代码,这些代码都有很重要的信息,让我们了解到对方的sip是如何响应我们这边的sip消息的,于是网上找到了这些sip消息类型和消息代码,自己收藏 ...

  4. BilSTM 实体识别_NLP-入门实体命名识别(NER)+Bilstm-CRF模型原理Pytorch代码详解——最全攻略

    最近在系统地接触学习NER,但是发现这方面的小帖子还比较零散.所以我把学习的记录放出来给大家作参考,其中汇聚了很多其他博主的知识,在本文中也放出了他们的原链.希望能够以这篇文章为载体,帮助其他跟我一样 ...

  5. BilSTM 实体识别_NLP入门实体命名识别(NER)+BilstmCRF模型原理Pytorch代码详解——最全攻略...

    来自 | 知乎   作者 | seven链接 | https://zhuanlan.zhihu.com/p/79552594编辑 | 机器学习算法与自然语言处理公众号本文仅作学术分享,如有侵权,请联系 ...

  6. 标准oc算法的推导与99行代码详解

    文章目录 标准oc算法的推导与代码详解 问题描述 OC算法的数学描述 结果展示 OC算法的matlab代码及注释 参考文献 标准oc算法的推导与代码详解 对于变密度的参数化方法,设计变量x为材料相对密 ...

  7. 从PointNet到PointNet++理论及代码详解

    从PointNet到PointNet++理论及代码详解 1. 点云是什么 1.1 三维数据的表现形式 1.2 为什么使用点云 1.3 点云上以往的相关工作 2. PointNet 2.1 基于点云的置 ...

  8. Pytorch | yolov3原理及代码详解(一)

    YOLO相关原理 : https://blog.csdn.net/leviopku/article/details/82660381 https://www.jianshu.com/p/d13ae10 ...

  9. 免疫算法(IA)MatLab代码详解

    CSDN编辑器里面没有MATLAB选项,所以我为了注释更清楚,用的是//,大家不要被误解了呀,算法看代码就好了 1 免疫算法的精髓 生物免疫系统:动态性免疫.自适应免疫 免疫算法-保证信息的无害性.有 ...

最新文章

  1. 你和人工智能的对话,正在被人工收听
  2. fail2ban封IP之Http
  3. JVM优化系列-详解常用的虚拟机调优参数
  4. 大数据可视化大屏图表设计经验,教给你
  5. 《 .NET软件设计新思维》一书作者MSDN课程日程
  6. Flutter实战之AS快键键
  7. 贝叶斯网络(数据预测)Python代码资源推荐
  8. 如何删除kafka消费组
  9. 这几款超实用办公神器,让你的工作省心省时又省力!
  10. 微信小程序头像上传(一)
  11. 超硬核万字!web前端学霸笔记,学完就去找工作吧
  12. Python Flask开发简单http api接口 示例
  13. Unity载入骨骼动画详解
  14. 【P1889 士兵站队】(洛谷)
  15. OSCP-Pelican(Exhibitor、gcore提权)
  16. 2022年中国航天发射列表(全年64发)
  17. 手机上所部署的DigiCert证书过期怎么办
  18. (附源码)springboot+mysql+基于Spring boot开发电子宿舍管理系统 毕业设计132056
  19. 数学与计算机科学国际研讨会怎么样,数学大咖齐聚杨祠探讨数学与计算机科学前沿问题...
  20. 自制Unity小游戏TankHero-2D(1)制作主角坦克

热门文章

  1. PTA n个分数相加
  2. 应用在汽车倒车影像中的环境光传感芯片
  3. 微信小程序开发(五)——数据绑定、运算、样式导入、页面跳转
  4. 韦东山 IMX6ULL和正点原子_正点原子Linux第五十七章Linux MISC驱动实验
  5. 什么是敏捷开发,敏捷开发落地指南之迭代排期
  6. matlab决策树模型过程,利用MATLAB统计工具箱进行决策树分类的一个例子
  7. mysql常用增删改查命令总结
  8. 程序化交易的基本特点分析
  9. 如何标题编号自动生成_标题编号的自动生成+自动修改的方法步骤
  10. VM虚拟机局域网组网配置