MySQL的事务陷阱和艺术_MySQL
作者在之前的文章 “MySQL事务及为何不能在PHP模仿事务” 里面, 详细说明了事务的优点,并介绍了一些简单的SQL命令,使得应用程序更加健壮。但在web程序员的生命旅程中并没有多少事情是看起来那样简单的。。。。。
不能回滚的语句(Statements you can’t ROLLBACK)
很遗憾滴通知你, 并不是所有的数据库操作都支持回滚( ROLLBACK ) 。如果你更改数据库/表结构(schema), 所有当前事务都会被提交, 而升级(alteration )将会在其独有的事务中运行(不属于任何客户端事务)。这些语句包括:
CREATE DATABASE ALTER DATABASE DROP DATABASE CREATE TABLE ALTER TABLE DROP TABLE RENAME TABLE TRUNCATE TABLE CREATE INDEX DROP INDEX CREATE EVENT DROP EVENT CREATE FUNCTION DROP FUNCTION CREATE PROCEDURE DROP PROCEDURE我们不能撤消数据库根本上的变化, 例如:
START TRANSACTION; DROP TABLE MyImportantData; -- 所有事务会被强制提交, existing (empty) transaction is COMMIT-ed -- 然后该表就被永久地删除了(table is dropped permanently) ROLLBACK; -- 没机会了,数据已经不要你了. no chance, mate - your data's gone
提示: 临时表(TEMPORARY)
创建、升级和删除(CREATE, ALTER, and DROP)临时表并不会引起隐式提交(implicit COMMIT. )。当然,这些操作也是不能回滚的。
保存点(Savepoint)
我们对异常那是爱之深责之切,那么让我们来看看另一个设计优美的部分。保存点(Savepoint)是事务中有效的命名位置。你可以回滚到某个保存点而不影响改点之前的SQL更新。。。有点像Photoshop中的历史面板。
最简单的方法,我们一起来看个示例:
START TRANSACTION; -- 增加 tableA 的记录 INSERT INTO tableA VALUES (1,2,3); -- 创建保存点 tableAupdated SAVEPOINT tableAupdated; -- 增加 tableB 的记录 INSERT INTO tableB VALUES (4,5,6); -- 反正发生了些什么不愉快的事,要取消对 tableB 所做的更新... ROLLBACK TO tableAupdated; -- 这时候提交,就只有 tableA 被更新了 COMMIT;
当然, 也可以设置多个保存点标识符(SAVEPOINT identifiers), 并且在事务中回滚到任意一处。
也可以删除一个保存点,语法如下:
RELEASE SAVEPOINT savepointName;
只要事务提交或者(整个)回滚,那么所有的保存点都会被删除。
事务和保存点的使用非常简单,能有效保护 InnoDB 中重要的数据。你还有什么理由坚持使用 MyISAM 呢,现在MyISAM的效率也不如InnoDB了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Are you tired of seeing the traditional black borders on your Word documents all the time? Are you looking for ways how to add some colorful and artistic borders to your documents to make them more attractive and fun? How about adding different artistic borders to different pages of your Word document? Or apply a single artistic border to all pages in the document at once? I know you’re as excited as we are about this whole artistic borders thing! Go straight to this article to learn how to successfully apply artistic borders to Word documents. Part 1: How to Apply the Same Artistic Page Border to All Pages in a Word Document Step 1: Open the Word document and click the "Design" tab in the top ribbon. Choose in DESIGN

Lockwaittimeoutexceeded;tryrestartingtransaction - How to solve the MySQL error: transaction wait timeout. When using the MySQL database, you may sometimes encounter a common error: Lockwaittimeoutexceeded;tryrestartingtransaction. This error indicates that the transaction wait timeout. This error usually occurs when

Produced by Big Data Digest Author: Caleb In order to celebrate the 70th anniversary of Queen Elizabeth II’s accession to the throne, the UK has also been filled with celebrations early. It is understood that the UK will have a public holiday for four consecutive days from June 2 to 5, and will hold a number of celebrations during the period. The British Royal Mint is also carefully crafting the largest coin in history, with a diameter of 220 mm, a weight of 15 kilograms, a face value of 15,000 pounds, and took nearly 400 hours to create. It is the largest coin produced by the factory in 1,100 years. This gold coin is engraved with the symbol EIIR representing Queen Elizabeth II on one side, surrounded by roses, daffodils, thistles and shamrocks representing the United Kingdom. The other side shows the Queen on horseback. In such a lively day, of course AI must come and join in the fun

MySQL transaction processing: the difference between automatic submission and manual submission. In the MySQL database, a transaction is a set of SQL statements. Either all executions are successful or all executions fail, ensuring the consistency and integrity of the data. In MySQL, transactions can be divided into automatic submission and manual submission. The difference lies in the timing of transaction submission and the scope of control over the transaction. The following will introduce the difference between automatic submission and manual submission in detail, and give specific code examples to illustrate. 1. Automatically submit in MySQL, if it is not displayed

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq

The principle and application scenarios of MySQL transactions In the database system, a transaction is a set of SQL operations. These operations are either all executed successfully or all fail and are rolled back. As a commonly used relational database management system, MySQL supports transaction characteristics and can ensure that the data in the database is consistent, isolated, durable and atomic. This article will start with the basic principles of MySQL transactions, introduce its application scenarios, and provide specific code examples for readers' reference. The principle of MySQL transactions: My

Analysis of solutions to transaction management problems encountered in MongoDB technology development As modern applications become more and more complex and large, the transaction processing requirements for data are also getting higher and higher. As a popular NoSQL database, MongoDB has excellent performance and scalability in data management. However, MongoDB is relatively weak in data consistency and transaction management, posing challenges to developers. In this article, we will explore the transaction management issues encountered in MongoDB development and propose some solutions.

Transactions ensure database data integrity, including atomicity, consistency, isolation, and durability. JDBC uses the Connection interface to provide transaction control (setAutoCommit, commit, rollback). Concurrency control mechanisms coordinate concurrent operations, using locks or optimistic/pessimistic concurrency control to achieve transaction isolation to prevent data inconsistencies.
