MySQL- Chapter 14. Transaction Management

1. What is a transaction

    A transaction is a logical step in the execution unit by the sequence of steps or operations database consisting of a series of operations that either all executed or give up all executed.

2, includes four transaction properties of:

  1 "atomicity (Atomicity): transaction application is the smallest unit of execution, a transaction can not be divided applications smallest logical execution bodies.

  2 "Consistency (Consistency): the results of transaction execution, the database must be consistent from one state to another consistent state variable. When the database contains only the outcome of the transaction successfully committed, the database is in a consistent state. If the system is running an interrupt occurs, a transaction has not been forced to interrupt the outstanding transactions have been written to the database modifications made to the database is complete, and at this time the database will be in a bad state. Consistency is ensured by atomicity.

  3 "Isolation (Isolation): performing interfering each transaction, a transaction of any other internal operations of concurrent transactions is isolated.

  4 "persistent (Durability): persistence, also known as persistence, means that once a transaction is committed, any changes made to the data to be recorded in the permanent memory, is usually saved to the physical database.

3, transaction database by the following statements:

  1, "a group of DML statements, after data this group DML statements modifications will maintain a good consistency.

  2 "is a DDL statement

  3 "A DCL statement

  DDL and DCL statements can only be one, because DDL and DCL statements will cause the transaction to immediately submit.

4. When all database operations are included in the successful implementation of the firm, should be submitted to (commit) transaction, the modification permanent. Committed transactions in two ways:

  1 "explicitly committed: Using commit

  2 "automatic submission: execute DDL or DCL statement, or exit the program properly.

5, when any one of the database operation comprising executing the transaction fails, should be rolled back (ROLLBACK) transaction, so that changes made in the whole transaction fails. Transaction rollback of two ways:

  1 "shows rollback: Use the rollback.

  2 "automatic rollback: system error or forced to quit.

6, MySQL off by default Affairs (opens automatically submitted). To open the MySQL transaction support, you can explicitly call the following command:

{0 = the AUTOCOMMIT the SET |}. 1 // 0 off submitted automatically, i.e. open transaction

  Enter the above statement once in the MySQL command line window is closed automatically submit the command line window where all DML statements will not take effect immediately. After completion of the transaction on a first DML statement begins a new transaction, and subsequent execution of all SQL statements in the transaction, unless explicitly use commit to commit the transaction, or normal exit, or run DDL, DCL statement causes implicit transaction commits. Of course, you can use the rollback to roll back to the end of the transaction, use the rollback end of the transaction will lead to the changes made in this transaction DML statements all fail.

 (Note: MySQL command-line window represents a single connection session, set the set the window autocommit = 0, equivalent to close the connection session is automatically submitted, have any effect on other connections will not, that is not other command-line window by setting affects this.)

7. If you do not want to shut down the entire command line window automatically submit, but to temporarily start the transaction.

    MySQL can be used to provide a start transaction or begin two commands in the start transaction or begin DML statements do not take effect immediately, unless explicitly commit to commit the transaction, or the use of DDL, DCL statements implicitly commit the transaction.

8, submit, whether explicitly or implicitly commit to submit, will end the current transaction; rollback, whether explicit or implicit rollback rollback will end the current transaction.

9, MySQL also provides a savepoint to set the mid-point of the transaction, allowing the transaction to roll back to the middle point designated by setting the mid-point of the transaction, rather than roll back the entire transaction.

savepoint a;

   Once the intermediate point, can be used to specify that the intermediate rollback rollback point.

rollback a;

10, JDBC transaction support

   1 " JDBC connection also provides transaction support, transaction support JDBC connection provided by Connection, Connection by default to automatically submit that the transaction is closed. Connection can call a setAutoCommit () method to turn off auto-commit, open transactions.

// turn off auto-commit, open transaction 
conn.setAutoCommit ( false );

   Once the transaction after the start, the program can create a Statement object as usual, we created the Statement object, you can perform any number of DML statements.

   You can also invoke provided getAutoCommit Connection () method to return the connection automatically commit mode .

   Connection of the commit () method and rollback () methods:

conn.commit();
conn.rollback();

    Note: When Connection encountered an unhandled exception SQLException, the system will exit abnormally , the transaction will automatically be rolled back . However, if the capture program exception , the exception processing block need explicit transaction rollback (rollback manually) .

   2 "Connection also provides a method of setting an intermediate point:

       1> Savepoint setSavepoint (): create an unnamed intermediate point in the current transaction and returns on behalf of the mid-point of Savepoint object.

       2> Savepoint setSavepoint (String name) : created a middle point with the specified name in the current transaction and returns on behalf of Savepoint object of the intermediate point.

       The rollback intermediate point, the intermediate point is not concerned with the name, but the intermediate rollback point object, the Connection rollback (Savepoint savepoint) method to roll back to a specified middle point.

Guess you like

Origin www.cnblogs.com/ZeroMZ/p/11373007.html