mysql的engine不同,导致事物回滚失败的问题

近期在项目上遇到遇到一个头疼的问题,前方销售团队反馈了一个客户那边在创建用户(save object to DB)报错了以后,前台展示了错误,但是数据库却保存了这条记录。

接到这个BUG以后,第一时间查看了事物是否正确回滚,排查了代码后发现事物回滚成功,并且在我的环境下回滚成功,错误没有复现,

这个时候就比较棘手了,客户的事物回滚失败,但是我们的回滚成功,我们的项目使用的是grails框架做的,查询了grails声明性事物回滚的规则,我们的代码没有问题,那我就把问题转移到了数据库身上,

要了客户的数据库,以SQL的形式把数据库到出来,和我本地的数据库表进行比对,在比对的过程中发现了不同之处

客户的SQL(mysql)

ENGINE=MyISAM AUTO_INCREMENT=522 DEFAULT CHARSET=utf8
ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8

我们的SQL

发现问题所在,我们用的数据库引擎是InnoDB,而客户使用的是MYISAM,查询这两种引擎不同之处

https://dev.mysql.com/doc/refman/5.6/en/storage-engines.html

MySQL 5.6 Supported Storage Engines

InnoDB: The default storage engine in MySQL 5.6. InnoDB is a transaction-safe (ACID compliant) storage engine for MySQL that has commit, rollback, and crash-recovery capabilities to protect user data. InnoDB row-level locking (without escalation to coarser granularity locks) and Oracle-style consistent nonlocking reads increase multi-user concurrency and performance. InnoDB stores user data in clustered indexes to reduce I/O for common queries based on primary keys. To maintain data integrity, InnoDB also supports FOREIGN KEY referential-integrity constraints. For more information about InnoDB, see Chapter 14, The InnoDB Storage Engine.

MyISAM: These tables have a small footprint. Table-level locking limits the performance in read/write workloads, so it is often used in read-only or read-mostly workloads in Web and data warehousing configurations.

Memory: Stores all data in RAM, for fast access in environments that require quick lookups of non-critical data. This engine was formerly known as the HEAP engine. Its use cases are decreasing; InnoDB with its buffer pool memory area provides a general-purpose and durable way to keep most or all data in memory, and NDBCLUSTER provides fast key-value lookups for huge distributed data sets.

CSV: Its tables are really text files with comma-separated values. CSV tables let you import or dump data in CSV format, to exchange data with scripts and applications that read and write that same format. Because CSV tables are not indexed, you typically keep the data in InnoDB tables during normal operation, and only use CSV tables during the import or export stage.

Archive: These compact, unindexed tables are intended for storing and retrieving large amounts of seldom-referenced historical, archived, or security audit information.

Blackhole: The Blackhole storage engine accepts but does not store data, similar to the Unix /dev/null device. Queries always return an empty set. These tables can be used in replication configurations where DML statements are sent to slave servers, but the master server does not keep its own copy of the data.

NDB (also known as NDBCLUSTER)—This clustered database engine is particularly suited for applications that require the highest possible degree of uptime and availability.

InnoDB支持事物,而MyISAM不支持事物,

在次复现错误,错误复现出来。

解决方式:

修改这张表的engine

alter table user engine=InnoDB

修改后再次测试,事物回滚成功。

bug解决。

为了避免再次创建表的engine是MyISAM引擎,修改mysql默认引擎,在修改之前查看数据库支持的所有引擎

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

存在InnoDB

在配置文件my.cnf中的 [mysqld] 下面加入default-storage-engine=INNODB 一句,保存。

重启mysql服务器:mysqladmin -u root -p shutdown或者service mysqld restart 登录mysql数据库,在mysql>提示符下搞入show engines;命令。如果出现 InnoDB |DEFAULT,则表示我们 设置InnoDB为默认引擎成功。

时间: 2024-08-07 19:11:49

mysql的engine不同,导致事物回滚失败的问题的相关文章

springboot 项目中使用@transactional注解不生效导致事务回滚失败问题总结

在常规使用@transactional注解时,如果碰到不生效问题,要首先想到如下几个问题: 1. 如果是有关数据库操作,首先要查看牵涉到的表使用的引擎是什么引擎,要知道使用"MYISAM"数据库引擎是不支持事务回滚操作的,需要使用"InnoDB数据引擎". 2. 查看方法是否是public方法,如果方法是private方法,也是不支持事务的. 3. 如果事务回滚失败还要查看出现的异常是checked异常还是unchecked异常.checked异常会回滚,unche

MySQL【Update误操作】回滚(转)

前言:      继上一篇MySQL[Delete误操作]回滚之后,现在介绍下Update回滚,操作数据库时候难免会因为“大意”而误操作,需要快速恢复的话通过备份来恢复是不太可能的,因为需要还原和binlog差来恢复,等不了,很费时.这里说明因为Update 操作的恢复方法:主要还是通过binlog来进行恢复,前提是binlog_format必须是Row格式,否则只能通过备份来恢复数据了.和上一篇的条件一样.方法:     条件:开启Binlog,Format为Row.     步骤:1.通过M

msql,触发器无事物回滚,插入之前满足条件再插入

很少写mysql的触发器和存储过程,由于需要需要做一个很小的判断,要用到触发器,要达到的效果就是,插入之前判断是否满足条件如果不满足就不插入 如果用sqlserver 或者orcale 就很简单,按sqlserver 和orcale的思路去找事物回滚,或者抛异常,都没用,最后只有不断查资料不断尝试,只有用mysql的触发器机制,触发器中无法修改,删除原表的值,也无法有返回值 最后代码如下 CREATETRIGGER `duanxin_before_insert` BEFORE INSERT ON

复习课程jdbc:使用配置文件properties进行连接数据库,数据库存取图片,批处理,时间戳,事物回滚等等

使用配置文件properties进行连接数据库 首先创建一个file自定义文件名,但是后缀名必须改为.properties(不分大小写):如config.properties: 然后双击config.properties进行编辑:此文件数据是根据键值对来存储的:我们可以把连接数据库的一些连接字符串存储在此文件里:然后用的时候直接读配置文件,到时候更换的时候方便移植和修改. name                                                          

java 手动事物回滚Transaction

1.spring mvc配置异常事物回滚机制 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">        ......(省略)    </bean> <!-- 事务管理器 -->    <bean id=&qu

php中对MYSQL操作之事务控制,回滚

<?php //事务控制,回滚 //创建一个mysqli对象 $mysqli = new MySQLi("主机名","mysql用户名","密码","数据库名"); //判断是否链接成功 if($mysqli->connect_error){ die($mysqli->connect_error); } //由于在事务提交中系统默认提交,故这里设置为FALSE先不提交 $mysqli->autocomm

mysql 实现事务的提交与回滚

最近要对数据库的数据进行一个定时迁移,为了防止在执行过程sql语句因为某些原因报错而导致数据转移混乱,因此要对我们的脚本加以事务进行控制. 首先我们建一张tran_test表 CREATE TABLE tran_test( f1 VARCHAR(10) NOT NULL, f2 INT(1) DEFAULT NULL, PRIMARY KEY (f1) )ENGINE=INNODB CHARSET=utf8 我想对tran_test插入两条数据,但是为了防止插入中报错,因此我要把插入语句控制在一

spring事物回滚机制 (事务异常回滚,捕获异常不抛出就不会回滚)

当异常被捕获catch的时候,spring的事物则不会回滚 为什么不会滚呢??  spring aop  异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获runtimeexception的异常: 解决方案: 1.例如service层处理事务,那么service中的方法中不做异常捕获,或者在catch语句中最后增加throw new RuntimeException()语句,以便让aop捕获异常再去回滚,并且在se

thinkphp 事物回滚

1 $m=D('YourModel');//或者是M(); 2 $m2=D('YouModel2'); 3 $m->startTrans();//在第一个模型里启用就可以了,或者第二个也行 4 $result=$m->where('删除条件')->delete(); 5 $result2=$m2->where('删除条件')->delete(); 6 if($result && $result2){ 7 $m->commit();//成功则提交 8 }e