spring data jpa使用@Transactional注解开启事务后失败不回滚

时间:2022-11-01 11:21:23

  如题,在数据库批量操作方法上使用@Transactional注解,其中一条数据抛出异常了,却死活不回滚。

  批量操作方法是公有的,spring也是默认支持事务的,排除代码层面问题,那么就看看数据库是否支持事务吧:

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
rows in set (0.01 sec)

  我们看到,只有InnoDB支持事务,那么我操作的表使用了哪个引擎呢:

mysql> show create table t_order;
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_order | CREATE TABLE `t_order` (
`id` bigint() NOT NULL AUTO_INCREMENT,
`access_channel` varchar() DEFAULT NULL,
`address` varchar() DEFAULT NULL,
`area` varchar() DEFAULT NULL,
`channel` varchar() DEFAULT NULL,
`linkphone` varchar() DEFAULT NULL,
`name` varchar() DEFAULT NULL,
`oprcode` varchar() DEFAULT NULL,
`oprtime` varchar() DEFAULT NULL,
`order_id` varchar() NOT NULL,
`servernum` varchar() DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT= DEFAULT CHARSET=utf8 |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.01 sec)

  明显不对,改成InnoDB吧:

mysql> alter table t_order engine=InnoDB;
Query OK, rows affected (0.16 sec)
Records: Duplicates: Warnings:

  再确认下:

mysql> show create table t_order;
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_order | CREATE TABLE `t_order` (
`id` bigint() NOT NULL AUTO_INCREMENT,
`access_channel` varchar() DEFAULT NULL,
`address` varchar() DEFAULT NULL,
`area` varchar() DEFAULT NULL,
`channel` varchar() DEFAULT NULL,
`linkphone` varchar() DEFAULT NULL,
`name` varchar() DEFAULT NULL,
`oprcode` varchar() DEFAULT NULL,
`oprtime` varchar() DEFAULT NULL,
`order_id` varchar() NOT NULL,
`servernum` varchar() DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8 |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.01 sec)

  再跑批量方法,事务终于回滚了。