Detailed explanation of Java transactions and simple application examples

Simple use of Java transactions

Java transactions will be asked in some interviews.

During the interview, our first answer is: transactions can ensure the integrity and consistency of data.

If you have more profound skills: let's talk about some principles (set not to submit the task before the task starts, and then submit the task after all tasks are completed. If the task is disconnected in the middle, rollback will be executed, and the previously executed task will be revoked). Let's take a simple example (for example, the problem of saving and withdrawing money. For example, if the bank transfers between two accounts and transfers 1000 yuan from account a to account B, the system first reduces 1000 yuan from account a, and then increases 1000 yuan for account B. if all the implementation is successful, the database is consistent; if only the modification of account a amount is completed without increasing the amount of account B, the database is inconsistent In this case, you need to cancel the previous operation.)

This article briefly discusses Java transactions. When we ask about Java transactions, we should know that they are related to databases.

1、 Let's start with a simple piece of code

Transaction processing using JDBC

The above code is a relatively simple java transaction execution. In the above three delete operations, as long as one execution fails, the task rollback will be executed, which is equivalent to either success together or nothing.

If there is no transaction management, the previous execution will be updated in the database immediately. If the execution fails, you will exit and no longer perform the subsequent tasks. The consistency of data cannot be guaranteed.

2、 Basic concepts of Java transactions

Atomicity: a transaction is a complete operation. Each step of a transaction is inseparable (atomic); either all operations are executed or none are executed. Consistency: when the transaction is completed, the data must be in a consistent state isolation (isolation): all concurrent transactions that modify data are isolated from each other, which indicates that transactions must be independent,

It should not rely on or affect other transaction permanence in any way: after the transaction is completed, its modifications to the database are permanently maintained, and the transaction log can maintain the permanence of the transaction

Java transaction description: if the database is operated multiple times, each execution or step is a transaction If the database operation is not executed at a certain step or an exception occurs, resulting in transaction failure, some transactions are executed and some are not executed, so there is transaction rollback and the previous operation is cancelled

In database operation, a transaction is an inseparable unit of work composed of one or more SQL statements that update the database. Only when all operations in the transaction are completed normally can the whole transaction be committed to the database. If an operation is not completed, the whole transaction must be revoked.

For example, in the bank transfer transaction, suppose Zhang San transfers 1000 yuan from his account to Li Si's account. The relevant SQL statements are as follows:

update account set monery=monery-1000 where name='zhangsan'

update account set monery=monery+1000 where name='lisi'

These two statements must be processed as a completed transaction. This transaction can only be committed when both are successfully executed. If one sentence fails, the whole transaction must be undone.

Three methods to control transactions are provided in the connection class: (1) setautocommit (Boolean autocommit): set whether to automatically commit transactions; (2) commit(); Commit transactions; (3) rollback(); Undo the transaction;

In the JDBC API, the default is to automatically commit transactions, that is, each SQL statement updating the database represents a transaction. After the operation is successful, the system will automatically call commit (), otherwise it will call rollback () to undo the transaction.

In the JDBC API, you can disable automatic transaction submission by calling setautocommit (false). Then you can make SQL statements of multiple update databases as a transaction. After all operations are completed, call commit () to make the overall submission. If one of the SQL operations fails, the commit () method will not be executed, but the corresponding sqlException will be generated. At this time, the rollback () method can be captured in the abnormal code block to cancel the transaction.

Generally speaking, developers who specialize in database development must have a deep understanding of transactions, but ordinary programmers don't need to spend too much time on this aspect. Just have an understanding of the general function.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>