Home Java javaTutorial A detailed introduction to Java basic transactions

A detailed introduction to Java basic transactions

Mar 16, 2017 am 10:22 AM

This article mainly introduces the relevant information about the detailed introduction of Java basic transactions. Friends who need it can refer to

java transactions detailed explanation

1 , What is a transaction

A transaction is a sequence of operations to access the database. The database application system completes access to the database through transaction sets. The correct execution of a transaction converts the database from one state to another.

Transactions must comply with the ACID principles established by ISO/IEC. ACID is the abbreviation for atomicity, consistency, isolation and durability. Transactions must comply with the ACID principles established by ISO/IEC. ACID is the abbreviation for atomicity, consistency, isolation and durability.

Atomicity. That is, indivisibility, either all transactions are executed or none are executed. If all sub-transactions of the transaction are submitted successfully, all database operations are submitted, and the database state is converted; if any sub-transaction fails, the database operations of other sub-transactions are rolled back, that is, the database returns to The state before transaction execution, no state transition will occur.

Consistency or stringability. The execution of a transaction converts the database from one correct state to another correct state.
Isolation. Before the transaction is correctly committed, any changes to the data by the transaction are not allowed to be provided to any other transaction, that is, until the transaction is correctly committed, its possible results should not be displayed to any other transaction.

Persistence. After a transaction is submitted correctly, its results will be permanently saved in the database. Even if there are other failures after the transaction is submitted, the processing results of the transaction will be saved.

When you run an embedded SQL application or script, the transaction is automatically started the first time the executable SQL statement is executed (after the connection to the database is established or after the existing transaction is terminated) . After starting a transaction, it must be explicitly terminated by the user or application that started the transaction, unless a process called automatic commit is used (in which case each individual SQL statement issued is viewed Doing a single transaction is implicitly committed as soon as it is executed).

In most cases, a transaction is terminated by executing a COMMIT or ROLLBACK statement. When the COMMIT statement is executed, all changes made to the database since the transaction was started become permanent - that is, they are written to disk. When the ROLLBACK statement is executed, all changes made to the database since the transaction was started are undone, and the database is returned to the state it was in before the transaction began. In either case, the database is guaranteed to return to a consistent state when the transaction completes.

It is important to note that although transactions provide general database consistency by ensuring that changes to data become permanent only after the transaction is successfully committed, the user or application is still required to ensure The sequence of SQL operations performed within each transaction always results in a consistent database.

2. The database system supports two transaction modes:

Auto-commit mode: Each SQL statement is an independent transaction. After the database system executes a SQL statement, it will automatically submit the transaction.
Manual commit mode: The specified transaction start boundary and end boundary must be displayed by the database client program.

Note: Database tables in MySQL are divided into 3 types: INNODB, BDB and MyISAM, among which MyISAM does not support database transactions. The create table statement in MySQL defaults to the MyISAM type.

3. For multiple transactions running at the same time, when these transactions access the same data in the database, if the necessary isolation mechanism is not adopted, various concurrency problems will occur. These concurrency problems can be summarized as follows Several categories:

  • The first type of lossUpdate: When a transaction is revoked, the updated data submitted by other transactions is overwritten.

  • Dirty read: One transaction reads updated data submitted by another transaction.

  • Virtual read: One transaction reads newly inserted data that has been submitted by another transaction.

  • Non-repeatable read: One transaction reads updated data that has been submitted by another transaction.

  • The second type of lost update: This is a special case of non-repeatable read, where one transaction overwrites the updated data submitted by another transaction.

4. Isolation level

When the database system adopts the read Commited isolation level, it will lead to the second type of non-repeatable read The concurrency problem of lost updates can be avoided by using pessimistic locking or optimistic locking in the application. From an application perspective, locks can be divided into the following categories:

  1. Serializable: A transaction cannot see the updates made to the database by other transactions during execution.

  2. Repeatable Read (repeatable read): During the execution of a transaction, you can see the newly inserted records that have been submitted by other transactions, but you cannot see the changes to existing records by other transactions. renew.

  3. Read Committed (read committed data): During the execution of a transaction, you can see the newly inserted records that have been committed by other transactions, and you can also see the pairs that have been committed by other transactions. Update of existing records

  4. Read Uncomitted (read uncommitted data): During execution, a transaction can copy newly inserted records that have not been committed by other transactions, and can see other transactions There are no committed updates to existing records.

The higher the isolation level, the more complete and consistent the data can be guaranteed, but the greater the impact on concurrency performance. For most applications, you can give priority to setting the isolation level of the database system to Read Commited, which can avoid dirty reads and has better concurrency performance. Although it will lead to concurrency problems such as non-repeatable reads, virtual reads, and second-type lost updates, in individual situations where such problems may occur, they can be controlled by the application using pessimistic locks or optimistic locks.

When the database system adopts the read Commited isolation level, it will lead to non-repeatable reads and the second type of lost update concurrency problems. You can use pessimistic locks or optimistic locks in the application to avoid such problems. From the perspective of the application, locks can be divided into the following categories:

 A. Pessimistic lock: refers to the locking of data resources displayed in the application. Although it prevents concurrency problems such as lost updates and non-repeatable reads, it affects concurrency performance and should be used with caution.

 B. Optimistic locking: Optimistic locking assumes that when the current transaction operates the data resource, no other transactions will access the data resource at the same time, so it completely relies on the isolation of the database levels to automatically manage locks. Applications use version control to avoid possible concurrency issues.

5. There are two ways to implement pessimistic locking.

A. Explicitly specify in the application program to use the exclusive location of the database system to lock data resources. SQL statement: select... for update, use get in hibernate, load like session.get(Account .class,new Long(1),LockMode,UPGRADE)

 B. Add a LOCK field indicating the record status in the database table. When its value is "Y", Indicates that the record has been locked by a transaction. If it is "N", it indicates that the record is idle and the transaction can access it. This can be achieved by adding a lock tag field.

Using Hibernate’s version control to implement optimistic locking

Optimistic locking is a mechanism provided by the program. This mechanism can not only ensure that multiple transactions access data concurrently, but also prevent the second Class missing update problem.

In the application, you can use the version control function provided by Hibernate to view optimistic locking. The element and <timestamp> in the OR mapping file are both version controlled. function, it is generally recommended to use

The above is the detailed content of A detailed introduction to Java basic transactions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

How to Run Your First Spring Boot Application in Spring Tool Suite? How to Run Your First Spring Boot Application in Spring Tool Suite? Feb 07, 2025 pm 12:11 PM

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo

See all articles