使用springboot 配置了mysql
@EnableTransactionManagement @Transactional
可是事務根本不起做用。程序員
首先第一點:Spring的AOP事務管理默認是針對RunTimeException回滾,這個例外是unchecked exceptions。unchecked exception異常都是RuntimeException的子類。
若是遇到checked exceptions意外就不回滾。
如何改變默認規則:
1 讓checked例外也回滾:在整個方法前加上 @Transactional(rollbackFor=Exception.class)
2 讓unchecked例外不回滾: @Transactional(notRollbackFor=RunTimeException.class)spring
checked exceptions 其必須被 try{}catch語句塊所捕獲,或者在方法簽名裏經過throws子句聲明.受檢查的異常必須在編譯時被捕捉處理。sql
unchecked exceptions 須要程序員本身分析代碼決定是否捕獲和處理,好比 空指針,被0除...數據庫
其次是第二點springboot
查看數據庫中,表的引擎方式。app
1 查看mysql如今已提供什麼存儲引擎: show engines; 只有InnoDB是支持事務的ide
mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
2 查看本身的表的引擎方式 show create table 表名;指針
或者 show table status from db_name where name='table_name';code
mysql> show create table user; +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | user | CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `grade_id` bigint(20) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 | +-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
mysql> show table status from test where name='user'; +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+ | user | MyISAM | 10 | Dynamic | 1 | 20 | 20 | 281474976710655 | 2048 | 0 | 2 | 2018-04-27 21:50:12 | 2018-04-28 18:43:10 | NULL | utf8_general_ci | NULL | | | +------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
能夠看到 user表是MyISAM引擎建立的,不支持事務。
3 修改表爲InnoDB引擎 alter table table_name engine=innodb;