Spring boot中hibernate默认设置mysql存储引擎为MyISAM导致Mysql事务不支持

原因

Mysql中表使用的存储引擎为MyISAM,导致对该表进行事务操作,没有进行正常回滚。

检查@Transactional

  • 是否用在public方法上面
  • 设置了 Exception.class类,即 @Transactional(rollbackFor = Exception.class)

检查msyql表

检查表存储引擎

zylMBP:~ zhangyalin$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1505
Server version: 5.7.19 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>use mydatabase;

mysql> SHOW TABLE STATUS WHERE Name = 'link';

结果如下:

检查mysql表存储引擎

当前link表,使用的存储引擎为MyISAM,这个是不支持事务的,要支持事务需要修改为InnoDB。

修改为InnDB

mysql> ALTER TABLE link ENGINE = InnoDB;
Query OK, 2 rows affected (0.04 sec)
Records: 2  Duplicates: 0  Warnings: 0

再次检查

ysql> SHOW TABLE STATUS WHERE Name = 'link';

结果如下:

再次检查mysql表存储引擎

这样就解决Spring设置事务无效的问题了。还需要设置Spring通过Hibernate创建mysql表默认使用InnoDB的存储引擎。

配置Hibernate

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

参考:

springBoot service 事务注解@Transactional不起作用的解决

Spring Boot + JPA MySQL InnoDB 配置

13.7.6.36 SHOW TABLE STATUS Syntax

转载于:https://my.oschina.net/fxtxz2/blog/2244826

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值