Jdbc--transactions

Transaction:
A transaction is an event, which may consist of multiple units that either all succeed or none of them succeed.
Transaction operation:
open transaction:
set transaction submission mode to manual: con.setAutoCommit(false);
set rollback point: con.setSavepoint();
transaction rollback: con.rollback();
transaction commit: con.commit() ;

Transaction characteristics:
ACID
Atomicity (Atomicity) Atomicity
means that a transaction is an indivisible unit of work, and the operations in the transaction either occur or none of them occur.
Consistency
The integrity of data before and after a transaction must be consistent.
Isolation
The isolation of a transaction means that when multiple users access the database concurrently, the transaction of one user cannot be interfered by the transactions of other users, and the data between multiple concurrent transactions must be isolated from each other.
Durability Durability
means that once a transaction is committed, its changes to the data in the database are permanent, and even if the database fails, it should not have any effect on it.

Dirty read: One transaction reads uncommitted data of another transaction.
Non-repeatable read: refers to inconsistent data read multiple times (reads the committed data of a transaction) Emphasize update [referring to changes in data content]
virtual read or Phantom read: Refers to the inconsistency of data read multiple times. Emphasis on insert [referring to the change in the amount of data]

Transaction isolation level:
1 Serializable: It can avoid the occurrence of dirty reads, non-repeatable reads, and virtual reads. (serialize)
2 Repeatable read: It can avoid the occurrence of dirty read and non-repeatable read. (Repeatable read) Virtual read cannot be avoided
3 Read committed: Dirty read can be avoided (read has been committed)
4 Read uncommitted: The lowest level, none of the above conditions can be guaranteed. (Read uncommitted)
Mysql database default is Repeatable read
Oracle database default is read committed
Set transaction isolation level:
MySQL: Set session transaction isolation level read uncommitted;
jdbc:
void setTransactionIsolation(int level) throws SQLException
The above method is the Connection interface The method parameter in which sets the transaction isolation level can be
level - one of the following Connection constants:
Connection.TRANSACTION_READ_UNCOMMITTED,
Connection.TRANSACTION_READ_COMMITTED,
Connection.TRANSACTION_REPEATABLE_READ, or
Connection.TRANSACTION_SERIALIZABLE.
(Note that Connection.TRANSACTION_NONE cannot be used because it specifies an unsupported transaction.)

Missing updates:
Two transactions operate on the same record, and the later-committed transaction overwrites the modification operations of the first-committed transaction.
Option 1: Pessimistic lock (assuming that lost updates will definitely occur) ----- Use the internal lock mechanism of the database to manage transactions
Option 2: Optimistic locking (assuming that lost updates will not happen) ------- Add in the program The version field solves the problem of lost updates

Pessimistic locks are divided into two types: 
1. Shared locks can add multiple shared locks to a record lock in share mode
2. Exclusive locks Exclusive locks cannot coexist with other locks, as long as the row is added After the statement it locks is executed, other statements that want to execute this statement can be executed.
The update operation adds an exclusive lock by default 
for update [select * from account for update;] to add an exclusive lock
optimistic lock:
it is controlled by the version field.
            create table product (
   id int,
   name varchar(20),
   updatetime timestamp
);
insert into product values(1,'refrigerator',null);
update product set name='washing machine' where id = 1
; Execute a query, get the time of time, and compare this time with the previous time. If it is equal, it means
that no other transaction has operated on this data before. If it is not equal, it means that other transactions have operated on this data.

Guess you like

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