spring boot transaction does not work

Posted by activeserver on Sun, 29 Mar 2020 17:11:06 +0200

Configured with springboot

@EnableTransactionManagement
@Transactional

But things don't work at all.

First, the first point: Spring's AOP transaction management is rollback for runtimeException by default, which is unchecked exceptions. unchecked exception exceptions are subclasses of runtimeException.
Do not roll back if you encounter checked exceptions.
How to change the default rule:
1. Roll back the checked exception: add @ transactional (rollback for = exception. Class) before the entire method
2. Let unchecked exception not roll back: @ Transactional(notRollbackFor=RunTimeException.class)

checked exceptions must be caught by the try{}catch block, or declared in the method signature through the throws clause. checked exceptions must be caught at compile time.

unchecked exceptions require the programmer to analyze the code to determine whether to capture and process, such as null pointer, divided by 0

 

The second is the second point

View the engine mode of tables in the database.

1. Check what storage engine 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;

Or 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 was created by the MyISAM engine and does not support transactions.

3. Modify the table to InnoDB engine alter table table [name engine = InnoDB;

Topics: MySQL SpringBoot Spring Database