Affairs
The transaction is a logical execution unit consisting of an operating sequence one step or multiple steps. This sequence is either executed or all give up the execution. Four characteristics of transactions: atomicity, consistency, isoization, and durability atomicity: the smallest execution unit of transaction application, cannot be divided. It is the minimum logical execution body that is no longer divided in affairs.
Consistency: The result of the execution of the transaction must make the database from one consistency to another state.
Isoities: The execution of each transaction is not interfered, and the internal operations of any transaction are isolated for other concurrent transactions. That is: the intermediate status of the other party cannot be seen between the transaction of concurrent execution, and the affairs of the concurrent execution cannot be affected by each other.
Durability: continuity is also known as Persistence. It refers to the change of data once the transaction is submitted and any changes to the data. It is usually stored in a physical database.
The sentences involved in the usual database are: a set of DML (data munipuration language, data operation language) statement, this set of DML statements will maintain good consistency after modification; the operating table statements, such as inserting, modifying, deleting delete Wait; a DDL (data definition language, data definition language) statement, the language of the data object has Create, Alter, Drop. A DATA Control Language, data control language) statement, mainly Grant and Revoke statements. DDL and DCL statements can only be available at most, because they will cause immediately submitting transactions. When all the database operations contained in the office are successfully implemented, transactions should be submitted to make these modifications permanently effective. There are two ways of transaction submission: display submission and automatic submission. Display Submission: Use Commit to submit automatic submission: execute DLL or DCL, or the program is withdrawn from any database operation execution of any database contained in the transaction, it should be rolled back to make all the modifications in the transaction fail. There are two ways to roll the transaction: display rollback and automatic rollback. Show rollback: Use rollback to roll automatically: System error or forcibly exit.
Possible issues of transaction concurrent processing
1. Dirty Read: One transaction reads the data that has not been submitted by another transaction
2. Non-Repertable Read: The operation of one transaction causes different data to read twice before and after another
3. Phantom Read: The operating of one transaction leads to the results of the results of the results of two queries before and after another
Example:
During the execution of transaction A and B:
Java jdbc transaction mechanism
Import java.sql.Connection; Import Java.SQL.DriverManager; Import Java.SQL.PreparedStatement; Import java.sql.sqlexception; Public Class CTION {Public Static Final String URL = "COM.MYSQL.JDBC.Driver"; Public Static Final String user = "root"; Public Static Final String Passwd = "123456"; Public Static void JDBCTRANSACTION (int ID) {Connection CONN = Null; EMENT PSTMTUPDATE = NULL; PreparedStatement PSTMTQURY = NULL; String Updatesql = "Update SQL"; String querySql = "Query SQL"; Try {class.Forname ("com.mysql.jdbc.driver"); conn = driverManager.getConnection (url, user, passwd); ommit (false); // Automatically submit settings Perform the update operation of PSTMTUPDATE = const.preparestatement (updateSQL); psttingupdate.executeupdate (); // execute the search operation pstmtQuery = conn.preparestatement (querySql); stmtquery.executequry (); conn.Commit (); conn. setAutocommit (True); pstmtupdate.close (); pstmtQuery.close (); const.close ();} Catch (Exception E) {try {conn.rollback ();} Catch (sqlexcep Tion E1) {} E.printstacktrace ( );} Finally {try {if (pstmtupdate! = Null) {pstmtupdate.close ();} if (pstmtqury! = Null) {pstmtqury.close ();} if (con! = null) {conn.cl ose (ose) ;}} Catch (sqlexception e2) {}}}}}
JDBC's affairs support
JDBC's Connection also supports things. Connection opens the automatic submission by default, that is, close things. In other words, the execution of each SQL statement will be submitted to the database immediately, which will be permanently effective and cannot operate it. Close the automatic submission of Connection and start things. The setAutocommit method of Connection is: Connection.setAutocommit (FALSE); through connection.getAutocommit () to obtain things. When we turn on things, the database operation completed in the current Connection will not be submitted to the database immediately, and we need to call the Commit method of Connection. If there is a statement failure, you can call Rollback back and forth. Note: If the Connection encounters an unprocessed SQLEXception abnormality, the system will exit abnormally, and the system will automatically roll back the transaction. If the program captures this abnormality, it is necessary to display the rollback transaction in the abnormal treatment. Connection provides methods for setting the saving point in the middle of the transaction: Setsavepoint. There are two methods to set the middle point: Savepoint Setsavepoint (): Create an unnamed middle point in the current transaction, and return the Savepoint object of the middle point. Savepoint Setsavepoint (String name): Create a middle point with a specified name in the current transaction and return the Savepoint object of the middle point. Generally, setsavepoint (string name) sets the name of the middle point. Roll back, but roll back according to the middle point object. The setting name is just a better distinction between the middle point object. Use Connection's Rollback (Savepoint Savepoint) method to complete the rollback to the specified middle point.
JDBC's support for transactions is reflected in three aspects:
1. Auto-Compom Mode
Connection provides an Auto-Commit property to specify when the transaction is over
2. When Auto-Commit is True, when the execution of each independent SQL operation is completed, the transaction is immediately submitted, which means that each SQL operation is a transaction
When is an independent SQL operation calculated, the JDBC specification is defined like this:
For data operation language (DML) and data definition language (DDL), as soon as the sentence is executed
3. When Auto-Commit is False, each transaction must display the call method to submit, or the callback method is displayed to roll back. Auto-Commit default
Transaction Levels
JDBC defines the level of five transactions::
Preservation point
JDBC defines the Savepoint interface and provides a more fine -grained transaction control mechanism. When a saving point is set, you can rollback to the status at the saving point, not the entire transaction of Rollback. Let's take a look at what major problems will bring us the existing JDBC operations, such as a business After modifying a message, you can query this information. It seems to be a simple business, which is also very easy to achieve, but when this business is placed on a multi -threaded high -concurrency platform, the problem naturally appears. After the modification, a thread also executed the modification statement before performing the inquiry. At this time, we execute the inquiries, and the information we see may be different from what we modify. To solve this problem, we must introduce the JDBC transaction mechanism. In fact, the current code is very simple. Give the example code for your reference: