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为默认引擎成功。