如题,在数据库批量操作方法上使用@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 |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.01 sec)
我们看到,只有InnoDB支持事务,那么我操作的表使用了哪个引擎呢:
mysql>show create table t_order;+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_order |CREATE TABLE `t_order` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`access_channel` varchar(32) DEFAULT NULL,
`address` varchar(512) DEFAULT NULL,
`area` varchar(32) DEFAULT NULL,
`channel` varchar(50) DEFAULT NULL,
`linkphone` varchar(32) DEFAULT NULL,
`name` varchar(32) DEFAULT NULL,
`oprcode` varchar(32) DEFAULT NULL,
`oprtime` varchar(32) DEFAULT NULL,
`order_id` varchar(32) NOT NULL,
`servernum` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=38 DEFAULT CHARSET=utf8 |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
明显不对,改成InnoDB吧:
mysql> alter table t_order engine=InnoDB;
Query OK,0 rows affected (0.16sec)
Records:0 Duplicates: 0 Warnings: 0
再确认下:
mysql>show create table t_order;+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_order |CREATE TABLE `t_order` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`access_channel` varchar(32) DEFAULT NULL,
`address` varchar(512) DEFAULT NULL,
`area` varchar(32) DEFAULT NULL,
`channel` varchar(50) DEFAULT NULL,
`linkphone` varchar(32) DEFAULT NULL,
`name` varchar(32) DEFAULT NULL,
`oprcode` varchar(32) DEFAULT NULL,
`oprtime` varchar(32) DEFAULT NULL,
`order_id` varchar(32) NOT NULL,
`servernum` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8 |
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
再跑批量方法,事务终于回滚了。