spring boot transaction doesn't work

Configured with springboot

@EnableTransactionManagement
@Transactional

But transactions don't work at all.

First, the first point: Spring's AOP transaction management defaults to rollback for RunTimeException, which is unchecked exceptions. Unchecked exceptions are all subclasses of RuntimeException.
It will not roll back if it encounters checked exceptions.
How to change the default rules:
1 Make checked exceptions also rollback: add @Transactional(rollbackFor=Exception.class) before the entire method
2 Make unchecked exceptions not rollback: @Transactional(notRollbackFor=RunTimeException.class)

Checked exceptions must be caught by try{}catch blocks, or declared in the method signature via the throws clause. Checked exceptions must be caught and handled at compile time.

Unchecked exceptions require programmers to analyze the code to decide whether to capture and handle them, such as null pointers, division by 0...

 

followed by the second point

View the engine mode of the table in the database.

1 Check what storage engines mysql now provides: show engines; only InnoDB supports transactions

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 View the engine mode of your own table show create table table name;

         或者 show table status from db_name where name='table_name';

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 |                |         |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+

         You can see that the user table is created by the MyISAM engine and does not support transactions.

3 Modify the table to InnoDB engine alter table table_name engine=innodb;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325304522&siteId=291194637