开发者

How to implement the ACID model for a database?

开发者 https://www.devze.com 2023-01-26 10:23 出处:网络
How do Igo about achieving the ACID prop开发者_Python百科erties for my database - A- Atomicity, C- Consistency, I- Isolation, D- Durability.

How do Igo about achieving the ACID prop开发者_Python百科erties for my database - A- Atomicity, C- Consistency, I- Isolation, D- Durability. Database system being used - MySql.


Well ACID is not a model that you can implement, but rather a set of rules that a database server must conform to in order to be able to handle transactions in a safe way.

MySQL is not an ACID compliant database server by design, but if you use the InnoDB storage engine for your tables (or better as your database server default storage engine by setting the default-storage-engine option to InnoDB (see default-storage-engine option)), you will be able to perform transaction-safe operations on your database.

I recommend you set InnoDB as your default storage engine because it is important that you don't mix operations on tables with different storage engines within a single transaction (InnoDB and MyISAM tables for example). Otherwise you might corrupt your data because you won't be able to rollback all your operations if you rollback your transaction.

That said, how do you ensure that your operations are ACID compliant? Well simply by grouping operations within transactions to go from one consistent state of your data to another, and commit at the end if everything went well, or rollback if something went wrong. To achieve this you need to tell the database server when your transaction start by issuing a START TRANSACTION statement, and when your transaction end with a COMMIT or ROLLBACK statement.

It is generally a good practice to disable the autocommit mode at the beginning of your scripts by using the SET AUTOCOMMIT=0 command, although it's not mandatory since the START TRANSACTION command actually disable the autocommit mode.

Also keep in mind that some statements issue an implicit COMMIT command (basically all DDL statements, and some others, see Statements That Cause an Implicit Commit).

You might also be interested in reading about the differences from standard SQL with regard to treatment of transactions in MySQL Transactions and Atomic Operations.


Use the innoDB-engine in MySQL, create a proper database schema including foreign keys, use transactions for related queries, use servergrade hardware and let the database do it's job.

http://en.wikipedia.org/wiki/ACID


If I understood you properly it is not your task. It is a task of MySql developers (Oracle). If database server support ACID you can use it by choose correct isolation level and use transactions (impicit or explicit).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号