Transaction based on MySQL database

1. The concept of transaction

  1. What is a transaction

      A thing has n constituent units, either the n constituent units succeed at the same time or n units fail at the same time is to put n constituent units into a transaction

   2. MySQL transactions

    Default transaction: a sql statement is a transaction that starts and commits the transaction by default

    Manual transactions:

    1) Open a transaction displayed: start transaction

    2) Transaction commit: commit means that all sqls from the opening of the transaction to the transaction commit are considered valid and truly update the database

    3) The rollback of the transaction: rollback represents the rollback of the transaction. All sql operations from the opening of the transaction to the rollback of the transaction consider that the invalid database has not been updated

2. JDBC transaction operation

  The default is automatic transactions:

  Execute the sql statement: executeUpdate() ---- Each execution of the executeUpdate method represents the automatic commit of the transaction

  Manual transactions via jdbc's API:

  Open transaction: conn.setAutoComnmit(false);

  Commit transaction: conn.commit();

  Rollback transaction: conn.rollback();

  Note: The connection that controls the transaction must be the same

       The connection that executes sql and the connection that opens the transaction must be the same to control the transaction

3. DBUtils transaction operation

  1.QueryRunner

    Constructed with parameters : QueryRunner runner = new QueryRunner(DataSource dataSource);

    The parameterized construction passes the data source (connection pool) into the QueryRunner as a parameter,

    QueryRunner will obtain a database connection resource from the connection pool to operate the database

    So directly use the update method without the Connection parameter to operate the database


     No parameter construction : QueryRunner runner = new QueryRunner();

    The parameterless construction does not pass the data source (connection pool) as a parameter to the QueryRunner

    Then use the method with the Connection parameter when using the QueryRunner object to operate the database

5. Use ThreadLocal to bind connection resources

  Using a method call is to use a thread, which can bind the resource (Database Connection), so that the same resource is accessed in the same thread

6 Transaction Characteristics and Isolation Levels

  1. Transaction characteristics ACID

    1) Atomicity (Atomicity) Atomicity means that a transaction is an indivisible unit of work, and the operations in the transaction either all occur or none of them occur.

    2) Consistency In a transaction, the integrity of the data before and after the transaction must be consistent.

    3) Isolation (Isolation) Multiple transactions, the isolation of transactions refers to when multiple users access the database concurrently,

      A user's transaction cannot be interfered by other users' transactions, and data between multiple concurrent transactions must be isolated from each other.

    4) Durability Durability refers to the changes to the data in the database once a transaction is committed.

      It is permanent, and even if the database fails, it should not have any effect on it.

   2. Concurrency problems - caused by isolation (Isolation)

    1. If isolation is not considered, there are 3 concurrent access problems in transactions

      1) Dirty read: Transaction B has read data that has not yet been committed by transaction A 

         ----- Request B transaction to read the data submitted by A transaction

      2) Non-repeatable read: the content of the data read twice in a transaction is inconsistent 

         ----- The requirement is that the data is consistent when read multiple times in a transaction --- unpdate

      3) Phantom read/virtual read: The amount of data read twice in a transaction is inconsistent 

         ----- Requires that the number of data read multiple times in a transaction is consistent --insert delete

    2. The solution sets the isolation level of the transaction

      1) read uncommitted: read data that has not yet been submitted: no problem can be solved

      2) read committed: read committed data: dirty reading can be solved ---- oracle default

      3) Repeatable read: Repeated read: can solve dirty read and non-repeatable read---mysql default

      4) serializable: serialization: can solve dirty read, non-repeatable read and virtual read---equivalent to lock table

      View MySQL's database default isolation level SELECT @@tx_isolation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325318764&siteId=291194637