Database "transaction" to learn the basics (a)

When the database before the school did not pay much attention to the concept of affairs, when the usual practice is to basically useless, but this is a very useful concept, it is worth to learn. This article is primarily concerned with very basic knowledge about the concept of a transaction and to manage transactions with JDBC, some advanced concepts are not involved in the transaction.

First, the database "transaction" Overview

== database transaction (transaction) == refers to access and operate a possible operating sequence database of various data items, these operations are either all executed or not executed all, it is an indivisible unit of work. All database operations performed by the transaction between the transaction and the end of the beginning transactions.
In simple terms, the transaction can be understood as a set of sets of operations: For some operations between, if not "synchronize", it will bring serious error and, therefore, these operations form a transaction, then the transaction management to unify the management of this operating. For example, when shopping, there are two basic operations: paid and delivery. If delivery is not paid or not paid on delivery, which will cause an error. Pay and is ready to be delivered to the two operations consisting of a transaction process (paying attention to the provisions front, after the delivery, after paying a certain delivery), so that the operation can avoid the generation of sync mistake.

Second, the database "transaction" feature

  • Atomic
    overall operation of the database transaction is indivisible, either completed or is not performed all

  • Consistency
    transaction several parallel execution, the execution result must be consistent with certain order of serial execution

  • Isolation of
    concurrent execution of transactions do not affect each other, and their impact on the database when they perform the same serial

  • Persistence
    Once the transaction is committed, their updates to the database is persistent, any transaction or system failure will not cause data loss

Third, the common database concurrency exception

  • Lost modify
    a transaction covers other transactions to modify data has been submitted, cause these changes appears to be missing, like

  • Dirty read
    One transaction reads data other matters not yet submitted

  • Non-repeatable read
    to read the results of a transaction on the same data inconsistencies

  • Magic Reading
    when a transaction reads data in a range, because the operation of other transactions leading to inconsistent results before and after two readings. Magic Reading and the difference is that non-repeatable read, non-repeatable read for a line is defined in terms of data, and phantom reads are multiple rows of data for uncertainty.

On the one hand there is a database transaction itself is to reduce errors, on the other hand can be easily restored to the state before the error occurred by the transaction when an error occurs.
To avoid the above exceptions, by transactionIsolation LevelsTo deal with in relation to the isolation levels, refer to the blog chiefs of four isolation level of the transaction , do discuss here temporarily.

Basic operations IV database "affairs"

  • Open
  • submit
  • Roll back
    when a database error occurs, it is necessary to restore the database to the state before the error occurred.

In JDBC inside, useConnectionObjects to manage transactions:

  • setAutoCommit (false)
    will indicate on the transaction

  • commit ()
    submitted by the end of the transaction

  • rollback ()
    to roll back the transaction ends

Code format

try{
     con.setAutoCommit(false);//开启事务
     ......
     con.commit();//try的最后提交事务      
} catch() {
    con.rollback();//回滚事务
}

Here Insert Picture Description
2019.12.22

Published 52 original articles · won praise 59 · views 6825

Guess you like

Origin blog.csdn.net/ataraxy_/article/details/103653194