{"id":250,"date":"2016-09-14T01:25:48","date_gmt":"2016-09-13T17:25:48","guid":{"rendered":"https:\/\/vinta.ws\/code\/?p=250"},"modified":"2026-02-18T01:20:36","modified_gmt":"2026-02-17T17:20:36","slug":"transaction-isolation-levels-in-databases","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/transaction-isolation-levels-in-databases.html","title":{"rendered":"Transaction isolation levels in MySQL"},"content":{"rendered":"<h2>Dirty reads \u9ad2\u8b80<\/h2>\n<p>Transaction 1 \u8b80\u5230\u4e86 Transaction 2 \u7684\u672a\u63d0\u4ea4\u7684\u503c<br \/>\nREAD COMMITED \u53ef\u4ee5\u907f\u514d\u9019\u500b\u554f\u984c<br \/>\nREAD UNCOMMITTED \u6703\u6709\u9019\u500b\u554f\u984c<\/p>\n<p>A dirty read occurs when a transaction is allowed to read data from a row that has been modified by another running transaction and not yet committed.<\/p>\n<h2>Non-repeatable reads \u4e0d\u53ef\u91cd\u8907\u8b80<\/h2>\n<p>\u5728\u540c\u4e00\u500b Transaction \u88e1 \u4e0d\u540c\u7684\u6642\u9593\u9ede \u8b80\u53d6\u4e86\u540c\u4e00\u500b row \u537b\u5f97\u5230\u4e86\u4e0d\u540c\u7684\u503c\uff08\u4f46\u662f\u53ef\u80fd\u4f7f\u7528\u4e86\u4e0d\u540c\u7684 SELECT \u8a9e\u53e5\u53bb\u8b80\u5230\u540c\u4e00\u500b row\uff09<br \/>\nREPEATABLE READ \u53ef\u4ee5\u907f\u514d\u9019\u500b\u554f\u984c<br \/>\nREAD COMMITED \u6703\u6709\u9019\u500b\u554f\u984c<\/p>\n<p>A non-repeatable read occurs, when during the course of a transaction, a row is retrieved twice and the values within the row differ between reads.<\/p>\n<p>When Transaction 2 commits successfully, which means that its changes to the row with id 1 should become visible. However, Transaction 1 has already seen a different value for age in that row. At the SERIALIZABLE and REPEATABLE READ isolation levels, the DBMS must return the old value for the second SELECT. At READ COMMITTED and READ UNCOMMITTED, the DBMS may return the updated value; this is a non-repeatable read.<\/p>\n<h2>Phantom reads \u5e7b\u8b80<\/h2>\n<p>\u6709\u9ede\u985e\u4f3c Non-repeatable reads<br \/>\n\u5728\u540c\u4e00\u500b Transaction \u88e1 \u4e0d\u540c\u7684\u6642\u9593\u9ede \u5169\u500b\u76f8\u540c\u4f46\u662f\u5148\u5f8c\u57f7\u884c\u7684 SQL \u537b\u5f97\u5230\u4e86\u4e0d\u540c\u7684\u7d50\u679c<br \/>\n\u5dee\u5225\u5728\u65bc Phantom reads \u5c08\u9580\u6307\u6c92\u6709\u7528 range locks \u6642<br \/>\n\u5148\u5f8c\u5169\u500b\u540c\u6a23\u7684 SELECT \u537b\u8fd4\u56de\u4e86\u4e0d\u540c\u6578\u91cf\u7684\u7d50\u679c<br \/>\nSERIALIZABLE \u53ef\u4ee5\u907f\u514d\u4ee5\u4e0a\u7684\u4e09\u7a2e\u554f\u984c<\/p>\n<p>\u4e5f\u53ef\u4ee5\u8aaa non-repeatable reads \u7684\u539f\u56e0\u662f UPDATE<br \/>\n\u800c phantom reads \u7684\u539f\u56e0\u5247\u662f INSERT \u548c DELETE<\/p>\n<p>A phantom read occurs when, in the course of a transaction, two identical queries are executed, and the collection of rows returned by the second query is different from the first.<\/p>\n<h2>READ UNCOMMITTED<\/h2>\n<p>\u5141\u8a31 dirty reads<br \/>\n\u53ef\u80fd\u6703\u8b80\u5230\u5176\u4ed6 transaction \u5c1a\u672a\u63d0\u4ea4\u7684\u6578\u64da<\/p>\n<h2>READ COMMITED<\/h2>\n<p>\u4e0d\u6703\u6709 dirty reads<br \/>\n\u53ea\u6703\u8b80\u5230\u5176\u4ed6 transaction \u5df2\u7d93\u63d0\u4ea4\u7684\u6578\u64da<br \/>\n\u4f46\u662f\u5141\u8a31 non-repeatable reads<\/p>\n<p>\u5927\u90e8\u4efd\u8cc7\u6599\u5eab\u9ed8\u8a8d\u7684 isolation level \u90fd\u662f READ COMMITED<\/p>\n<h2>REPEATABLE READ<\/h2>\n<p>\u4e0d\u6703\u6709 dirty reads \u548c non-repeatable reads<br \/>\n\u4f46\u662f\u5141\u8a31 phantom reads<\/p>\n<p>MySQL \u9ed8\u8a8d\u7684 isolation level \u662f REPEATABLE READ<br \/>\n\u4f46\u662f InnoDB \u900f\u904e next-key lock \u4e5f\u9632\u6b62\u4e86 phantom reads<\/p>\n<h2>SERIALIZABLE<\/h2>\n<p>\u4e0d\u6703\u6709 dirty reads\u3001non-repeatable reads \u548c phantom reads<br \/>\n\u8b80\u5beb\u4e92\u65a5<br \/>\n\u6240\u4ee5\u8b80\u548c\u5beb\u90fd\u53ea\u80fd\u4e00\u500b\u4e00\u500b\u4f9d\u5e8f\u57f7\u884c<\/p>\n<p>ref:<br \/>\n<a href=\"https:\/\/www.percona.com\/blog\/2012\/08\/28\/differences-between-read-committed-and-repeatable-read-transaction-isolation-levels\/\">https:\/\/www.percona.com\/blog\/2012\/08\/28\/differences-between-read-committed-and-repeatable-read-transaction-isolation-levels\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A non-repeatable read occurs, when during the course of a transaction, a row is retrieved twice and the values within the row differ between reads.<\/p>\n","protected":false},"author":1,"featured_media":276,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[23,92],"class_list":["post-250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-database","tag-mysql","tag-transaction"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/276"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}