spring boot 事务 Transactional 笔记

3 篇文章 0 订阅

最近发现事务不会回滚,查了原因总结一下原因:

1、@Transactional  没注解在public方法上

2、抛出的异常不是RuntimeException类异常,而@Transactional上没有注明rollbackFor哪一类异常。

解决方法可以是@Transactional( rollbackFor = Exception.class)

3、数据库存储引擎不支持回滚

以mysql为例,数据库引擎是MyISAM不回滚。把引擎改为InnoDB会回滚。

-- 查看库的存储引擎
show engines;
-- 查看表的存储引擎
-- show create table tablename;
-- 修改表的存储引擎
-- alter table tablename engine=InnoDB;

3.1、附批量修改表的存储引擎方法
-- 查看库里所有表
select * from information_schema.tables where table_schema='db_xiaoxiao_dev' and table_type='base table';
-- 批量行到修改表的存储引擎脚本
select CONCAT('alter table ',table_name,' engine=InnoDB;') from information_schema.tables where table_schema='db_xiaoxiao_dev' and table_type='base table' and `ENGINE`='MyISAM';

3.2、通常spring boot都会利用jpa直接创建表,如果要使创建的表引擎是InnoDB,则在配置文件中要加上

jpa:
  database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值