Hibernate Database Transaction and isolation level

Source: Internet
Author: User

< strong>

< p>

< p>

< p>

< strong>

< strong>

< p>

< p>

< p>

< p>

< strong>

< p>

< p>

< p>

< strong>

< p>

< p>

< p>

< p>

< p>

[Java]View plaincopyprint?

  1. < li>
  2. < li>
  3. < li>
  4. < li>
  5. < li>
  6. < li>
  7. < li>
  8. < li>
  9. < li>
  10. < li>
  11. < li>
  12. < li>
  13. < li>
  14. < li>
  15. < li>
  16. < li>
  17. < li>
  18. < li>
  19. < li>
  20. < li>


< p>

< p>

< p>

< p>

< p>

. Since

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

< p>

TransactionDefinitionThe interface defines five different transaction isolation levels:Isolation level refers to the degree of isolation between several concurrent transactions. The TransactionDefinition interface defines five constants at the isolation level:

  • TransactionDefinition. ISOLATION_DEFAULT: This is the default value, indicating that the default isolation level of the underlying database is used. For most databases, this value is usually TransactionDefinition. ISOLATION_READ_COMMITTED.
  • TransactionDefinition. ISOLATION_READ_UNCOMMITTED: This isolation level indicates that a transaction can read data modified by another transaction but has not yet been committed. This level does not prevent dirty reads and non-repeated reads. Therefore, this isolation level is rarely used.
  • TransactionDefinition. ISOLATION_READ_COMMITTED: This isolation level indicates that one transaction can only read the data committed by another transaction. This level can prevent dirty reads, which is also the recommended value in most cases.
  • TransactionDefinition. ISOLATION_REPEATABLE_READ: This isolation level indicates that a transaction can execute a query multiple times throughout the process, and the records returned each time are the same. Even if the newly added data between multiple queries meets the query requirements, these new records are ignored. This level prevents dirty reads and non-repeated reads.
  • TransactionDefinition. ISOLATION_SERIALIZABLE: all transactions are executed one by one, so that there is no interference between transactions. That is to say, this level can prevent dirty reads, non-repeated reads, and Phantom reads. However, this will seriously affect the program performance. This level is usually not used.

The TransactionDefinition interface defines seven transaction propagation behaviors..

  • TransactionDefinition. PROPAGATION_REQUIRED: this transaction is added if a transaction exists. If no transaction exists, a new transaction is created.
  • TransactionDefinition. PROPAGATION_REQUIRES_NEW: Creates a new transaction. If a transaction exists, the current transaction is suspended.
  • TransactionDefinition. PROPAGATION_SUPPORTS: if a transaction exists, it is added to the transaction. If no transaction exists, it continues to run as a non-transaction.
  • TransactionDefinition. PROPAGATION_NOT_SUPPORTED: runs in non-transaction mode. If a transaction exists, the current transaction is suspended.
  • TransactionDefinition. PROPAGATION_NEVER: runs in non-transaction mode. If a transaction exists, an exception is thrown.
  • TransactionDefinition. PROPAGATION_MANDATORY: this transaction is added if a transaction exists. If no transaction exists, an exception is thrown.
  • TransactionDefinition. PROPAGATION_NESTED: if a transaction exists, create a transaction to run as a nested transaction of the current transaction. If no transaction exists, the value is equivalent to TransactionDefinition. PROPAGATION_REQUIRED.

    • Bytes ----------------------------------------------------------------------------------------

      1. Missing or overwriting updates

      When two or more transactions select the same row and update the row based on the originally selected value, the update will be lost. Every transaction does not know the existence of other transactions. The last update will overwrite the updates made by other firms, which will lead to data loss.
      Example:

      Transaction A and transaction B Modify the value of A row at the same time,
      Transaction A changes the value to 1, commit transaction B, change the value to 2, and commit.
      When the value of data is 2, the update made by transaction A will be lost. Solution: Lock the row and allow only one update transaction to be concurrently. Bytes -----------------------------------------------------------------------------------------

      2. Dirty reading: unconfirmed correlation

      When the second transaction selects another row being updated, unconfirmed correlation issues will occur. The data being read by the second transaction has not been confirmed and may be changed by the transaction that updates this row.
      For example: Mary's original salary is 1000. The financial staff changed Mary's salary to 8000 (but did not submit a transaction) Mary Read her salary and found that her salary was changed to 8000, happy! The financial team found that the operation was wrong, rolled back the transaction, and Mary's salary changed to 1000.
      In this way, 8000 of Mary's salary is a dirty data.
      Solution: if any other transaction cannot read its modified value before the first transaction is committed, this problem can be avoided. Bytes -------------------------------------------------------------------------------------------

      Iii. Non-repeated read: inconsistent Analysis

      When the second transaction accesses the same row multiple times and reads different data each time, an inconsistent analysis problem occurs. The inconsistent analysis is similar to the unconfirmed correlation because other transactions are also changing the data being read by the second transaction. However, in an inconsistent analysis, the data read by the second transaction is committed by a transaction that has been changed. Furthermore, the inconsistent analysis involves reading the same row multiple times (twice or more) and the information is changed by other transactions each time. Therefore, the row is read non-repeatedly.
      The result of two reads before and after a transaction is inconsistent, resulting in non-repeated reads.
      For example: In transaction 1, Mary reads her own salary of 1000, and the operation is not completed in transaction 2. At this time, the financial personnel changed Mary's salary to 2000, and committed the transaction. in transaction 1, when Mary reads her salary again, the salary changes to 2000.
      Solution: this problem can be avoided if the data can be read only after the transaction is completely committed.

      Bytes --------------------------------------------------------------------------------------------

      Iv. Fantasy reading

      A phantom reading problem occurs when a row is inserted or deleted and the row belongs to the row being read by a transaction. The row range for the first read of the transaction shows that one row no longer exists in the second read or subsequent read because the row has been deleted by other transactions. Similarly, due to the insert operation of other transactions, the second or subsequent read of the transaction shows that a row does not exist in the original read.

      Example:

      Currently, there are 10 employees with a salary of 1000. Transaction A reads all employees whose salaries are 1000. At this time, transaction B inserts an employee record into the employee table, and the salary is also 1000
      Solution: if no new data can be added to any other transaction before the operation Transaction Completes data processing, this problem can be avoided.

    •  

      Official Spring Reference documents:

      Http://static.springsource.org/spring/docs/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.