Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Understanding Database Transactions and MySQL Isolation Levels

Notes 1

Database transactions are fundamental operations that ensure data integrity and consistency. A transaction is a sequence of one or more SQL statements executed as a single unit of work. For a database to support transactions, it must adhere to four key properties, often abbreviated as ACID.

Atomicity ensures that all operations within a transaction are completed successfully or none are applied. If any part fails, the entire transaction is rolled back, leaving the database unchanged.

Consistency guarantees that a transaction transitions the database from one valid state to another, maintaining all defined rules and constraints throughout the process.

Isolation prevents interference between concurrent transactions. When multiple users access the database simultaneously, each transaction operates independently, as if it were the only one running.

Durability ensures that once a transaction is committed, its changes persist permanently, even in the event of system failures.

When multiple threads execute transactions concurrently, isolation becomes critical to avoid data anomalies. Without proper isolation, several issues can arise:

Dirty Read occurs when a transaction reads uncommitted data from another transaction. For example, suppose Transaction A modifies a record but hasn't committed yet. If Transaction B reads that record, it sees the uncommitted change. If Transaction A then rolls back, Transaction B has read invalid data.

-- Example: Transaction A updates a balance but hasn't committed
UPDATE accounts SET balance = balance + 500 WHERE id = 1;
-- Transaction B reads the uncommitted balance
SELECT balance FROM accounts WHERE id = 1;
-- If Transaction A rolls back, Transaction B's read is incorrect

Non-Repeatable Read happans when a transaction reads the same data twice but gets different results because another transaction modified and committed the data in between. For instance, Transaction A reads a value, Transaction B updates it and commits, and then Transaction A reads it again, observing a change.

Phantom Read involves a transaction reading a set of rows that satisfies a condition, but another transaction inserts or deletes rows that match that condition and commits. When the first transaction repeats the read, it sees a different number of rows, asif new rows appeared or disappeared.

MySQL provides four isolation levels to control these issues, each offering a balance between data consistency and performance:

  • Serializable: Prevents dirty reads, non-repeatable reads, and phantom reads by locking tables, but it can reduce concurrency and efficiency.
  • Repeatable Read: Avoids dirty reads and non-repeatable reads, but phantom reads may still occur. This is the default isolation level in MySQL.
  • Read Committed: Prevents dirty reads, but non-repeatable reads and phantom reads are possible.
  • Read Uncommitted: The lowest level, where all anomalies can happen, as it allows reading uncommitted data.

To check the current isolation level in MySQL:

SELECT @@transaction_isolation;

To set the isolation level (ensure this is done before starting a transaction):

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- Or using the variable
SET @@transaction_isolation = 'REPEATABLE-READ';

Note that isolation level settings are connection-specific. In MySQL command-line interfaces, each window represents a connection, and changes apply only to that session. Similarly, in JDBC, each Connection object has its own isolation level.

In JDBC, isolation levels are defined as constants:

  • TRANSACTION_NONE: No transaction support.
  • TRANSACTION_READ_UNCOMMITTED: Corresponds to Read Uncommitted.
  • TRANSACTION_READ_COMMITTED: Corresponds to Read Committed.
  • TRANSACTION_REPEATABLE_READ: Corresponds to Repeatable Read.
  • TRANSACTION_SERIALIZABLE: Corresponds to Serializable.

Spring Framework maps these to its own isolation levels:

  • ISOLATION_DEFAULT: Uses the database's default.
  • ISOLATION_READ_UNCOMMITTED: Allows dirty reads.
  • ISOLATION_READ_COMMITTED: Prevents dirty reads.
  • ISOLATION_REPEATABLE_READ: Prevents dirty and non-repeatable reads.
  • ISOLATION_SERIALIZABLE: Prevents all anomalies.

Higher isolation levels enhance data consistency but may impact performance due to increased locking. Choose an appropriate level based on your application's requirements for accuracy and concurrency.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.