Transaction Usage Basics

Four characteristics of business

Atomicity: Transaction operations either all succeed or all are rolled back.
Consistency: Before and after a transaction changes from one state to another, with no intermediate states.
Isolation: Multiple transactions do not interfere with each other, and are divided into four levels.
Durability: The impact on the database after a transaction is committed is durable.

Problems caused by multiple transactions concurrently

Dirty read: One transaction reads uncommitted data from another transaction.
Non-repeatable read: A transaction reads data multiple times, and there is inconsistent data.
Phantom read: A transaction reads data multiple times, and data is added or deleted.
The first type of update loss: when undoing a transaction, it overwrites the updated data submitted by other transactions.
The second type of lost update: one transaction overwrites the updated data committed by another transaction.

Four isolation levels of transactions

In order to solve the problem of concurrent multiple transactions, transactions are divided into four isolation levels.
Read uncommitted: No isolation.
Read committed: Prevent dirty reads.
Repeatable read: Prevent dirty reads and prevent non-repeatable reads.
Serializable: Prevent dirty reads, prevent non-repeatable reads, prevent phantom reads, and prevent lost updates.

Spring transaction propagation behavior

1.PROPAGATION_REQUIRED: If there is no current transaction, create a new transaction. If there is a current transaction, join the transaction. This setting is the most commonly used setting.
2.PROPAGATION_SUPPORTS: Support the current transaction, if there is a current transaction, join the transaction, if there is no current transaction, execute it with a non-transaction.
3.PROPAGATION_MANDATORY: support the current transaction, if there is a current transaction, join the transaction, if there is no current transaction, throw an exception.
4.PROPAGATION_REQUIRES_NEW: Create a new transaction, regardless of whether there is a current transaction, create a new transaction.
5.PROPAGATION_NOT_SUPPORTED: Execute the operation in a non-transactional manner. If there is a current transaction, suspend the current transaction.
6.PROPAGATION_NEVER: Executed in a non-transactional manner, if there is currently a transaction, an exception will be thrown.
7.PROPAGATION_NESTED: If a transaction currently exists, execute within a nested transaction. If there are no current transactions, do something similar to PROPAGATION_REQUIRED.

Handling of Update Lost Behavior

1. Pessimistic locking
Pessimistic locking means that resources are locked when a transaction is in progress. will affect performance.
2. Optimistic locks
Optimistic locks can be set using version or timestamp. The specific method is to add an int-type version attribute to the persistent object. When a transaction updates a piece of data, version+1. The version will be verified when the transaction is updated, and if the data of the version no longer exists in the database, the data will be rolled back. Hibernate supports optimistic locking. When a persistent object has a version attribute, Hibernate will automatically update and verify it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324770983&siteId=291194637