JDBC detailed explanation of Java transaction management learning
What is a Java transaction
The general idea is that transactions are only related to databases.
Transactions must comply with the acid principles established by ISO / IEC. Acid is atomicity, consistency, isolation, and persistence (durability). Atomicity of a transaction means that any failure during the execution of a transaction will invalidate any modification made by the transaction. Consistency means that when the execution of a transaction fails, all data affected by the transaction should be restored to the state before the execution of the transaction. Isolation means that the modification of data during the execution of the transaction will affect other things before the transaction is committed No visible. Persistence means that the state of the submitted data should be correct when the transaction fails.
Generally speaking, a transaction is a group of atomic operation units. From the database point of view, it is a group of SQL instructions. Either all instructions are executed successfully. If there is an error in the execution of one of the instructions for some reason, all previously executed instructions will be revoked. To put it more simply: either all execution is successful, or no execution is revoked.
Since the concept of transaction comes from database, what is java transaction? What is the connection between them?
In fact, if a Java application system wants to operate the database, it is implemented through JDBC. Addition, modification and deletion are realized indirectly through corresponding methods, and the control of transactions is transferred to Java program code accordingly. Therefore, the transaction of database operation is conventionally called Java transaction.
Transaction characteristics:
1) Atomicity: a transaction is the logical work unit of a database and must be an atomic work unit. For data modification, either all or no execution is required.
2) Consistency: when a transaction is completed, all data must be consistent. In the relevant database, all rules must be applied to the modification of the transaction to maintain the integrity of all data.
3) Isolation: the execution of a transaction cannot be affected by other transactions.
4) Persistence: once a transaction is committed, the operation of the transaction will be permanently saved in the DB. Even if the rollback operation is performed at this time, the changes can not be undone.
Transaction: it is a unit of concurrency control and a user-defined sequence of operations. These operations are either done or not done. They are an inseparable work unit. Through transactions, SQL server can bind a set of logically related operations together so that the server can maintain data integrity. Transactions usually start with begin transaction and end with commit or rollback. Command means commit, that is, commit all operations of the transaction. Specifically, all data updates in the transaction are written back to the physical database on the disk, and the transaction ends normally. Rollback means rollback, that is, if a fault occurs during the operation of the transaction and the transaction cannot continue, the system will undo all completed operations on the database in the transaction and roll back to the start state of the transaction.
Auto commit transaction: each individual statement is a transaction. A commit is implied after each statement. (default)
Explicit transaction: start with begin transaction display and end with commit or rollback.
Implicit transaction: when the connection operates in implicit transaction mode, the SQL Server database engine instance will automatically start a new transaction after committing or rolling back the current transaction. Instead of describing the beginning of things, just commit or roll back each transaction. However, each transaction still ends explicitly with a commit or rollback. After the connection sets the implicit transaction mode to open, when the database engine instance executes any of the following statements for the first time, it will automatically start an implicit transaction: alter table, insert, create, open, delete, revoke, drop, select, fetch, truncate table, grant, update. The transaction will remain valid until the commit or rollback statement is issued. After the first transaction is committed or rolled back, the next time the connection executes any of the above statements, the database engine instance will automatically start a new transaction. This instance will continuously generate the implicit transaction chain until the implicit transaction mode is turned off.
JDBC transaction management
How to manage transactions when using JDBC. Look directly at the code
Sample code
For the above codes, the description is as follows:
The above code has several explanatory parts:
1. Conn.setautocommit (false) does not commit transactions after execution.
It has no impact on select, but for insert and update, the data will not be modified without submitting
2. conn.close(); The code to close the connection is dropped by mark to show the effect of conn.setautocommit (false).
The reason is that a commit will be executed when the connection is closed
If the connection pool is used in the application server, the connection will not be closed and commit will not be executed
3. Setautocommit (false) is mostly submitted after multiple statements are executed.
Therefore, for the third point above, the code closer to the actual situation is shown in example code 2
Example code 2
Note here: conn.rollback();
As long as there is an exception in the execution, it is necessary to rollback
If no exception occurs during execution, rollback is performed. If an exception occurs after the first statement is executed, and the con is neither committed nor rolled back, the table will be locked (if the Oracle database is a row lock), but this lock has no chance to be released.
May be executing con The lock will be released when closing (), but if the application server uses the database connection pool, the connection will not be disconnected.
summary
The above is the whole content of this article. I hope the content of this article can help you learn or use Java. If you have any questions, you can leave a message. Thank you for your support for programming tips.