Spring boot 连接Mysql数据库注解事务不回滚


搭建了一个新框架,使用了spring boot 替换以简化原来繁杂的spring配置,使用Spring注解管理事务,持久层使用mybatis。

在启动类Main 类上加上对事物的支持的注解 @EnableTransactionManagement

在相关的service类中update()加上开启事物注解 @Transactional

在update()方法中设置除数为0 , 为使运行时抛异常而回滚事务。

可是奇葩的问题来了,抛出了除数为0的异常,但是事物也提交成功了,查了下原因,后来发现是mysql数据库引擎的问题。

检查你的数据库支持哪些引擎:

mysql> show engines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
| CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
| InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO    



查询当前数据库默认的引擎    :

mysql> show variables like '%storage_engine%';
+----------------+--------+
| Variable_name  | Value  |
+----------------+--------+
| storage_engine | MyISAM |
+----------------+--------+
1 row in set (0.00 sec)


看某个表用了什么引擎:

show  create table  表名


修该表的存储引擎的方法:

1. alter table 表名 ENGINE = InnoDB;

2. 修改配置文件my.cnf,在[mysqld]最后添加为上default-storage-engine=InnoDB,重启数据库服务,数据库默认的引擎修改为InnoDB

3. 建表的时候指定       eg:  create table t_user(  id int primary key,name varchar(50) ) engine=InnoDB;

4.   批量生成某个库的的修改引擎语句,执行生成的这些语句即可

SELECT CONCAT('ALTER TABLE ',table_name,' ENGINE=InnoDB;') FROM information_schema.tables WHERE table_schema='数据库的名称' AND ENGINE='myisam';


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于Spring Boot数据源的实现,可以通过配置多个数据源并在需要使用的时候进行切换。在Spring Boot中,可以通过使用注解来声明多个数据源,并使用特定的注解来标识当前使用的数据源。 下面是一个简单的例子: 首先在application.properties文件中配置多个数据源: ``` # 数据源1 spring.datasource.url=jdbc:mysql://localhost:3306/datasource1 spring.datasource.username=root spring.datasource.password=123456 # 数据源2 spring.datasource.datasource2.url=jdbc:mysql://localhost:3306/datasource2 spring.datasource.datasource2.username=root spring.datasource.datasource2.password=123456 ``` 然后,在需要使用数据源的地方,使用@Primary注解来标识默认的数据源,使用@Qualifier注解来指定使用哪个数据源: ``` @Service public class UserServiceImpl implements UserService { @Autowired @Qualifier("datasource1") // 指定使用datasource1数据源 private DataSource dataSource; @Autowired @Qualifier("datasource2") // 指定使用datasource2数据源 private DataSource dataSource2; @Autowired private JdbcTemplate jdbcTemplate; @Autowired @Qualifier("transactionManager") // 指定使用事务管理器 private PlatformTransactionManager transactionManager; @Override @Transactional public void addUser(User user) { // 使用默认数据源 jdbcTemplate.update("insert into user(name, age) values(?, ?)", user.getName(), user.getAge()); // 切换数据源 TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) { JdbcTemplate jdbcTemplate2 = new JdbcTemplate(dataSource2); jdbcTemplate2.update("insert into user(name, age) values(?, ?)", user.getName(), user.getAge()); } }); } } ``` 在上面的例子中,我们使用了JdbcTemplate来操作数据库,并且在需要使用不同数据源的时候,使用@Qualifier注解来指定具体的数据源。同时,在需要进行事务管理的地方,使用了Spring声明式事务,并且通过注入PlatformTransactionManager来指定使用哪个事务管理器。 希望这个例子能够帮助你理解Spring Boot数据源的实现方式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值