How to implement the ACID model for a database?

前端 未结 3 1521
滥情空心
滥情空心 2021-02-04 21:29

How do Igo about achieving the ACID properties for my database - A- Atomicity, C- Consistency, I- Isolation, D- Durability. Database system being used - MySql.

相关标签:
3条回答
  • 2021-02-04 22:13

    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 讨论(0)
  • 2021-02-04 22:14

    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.

    0 讨论(0)
  • 2021-02-04 22:16

    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

    0 讨论(0)
提交回复
热议问题