1. The concept of database transactions
A database transaction is a unit of work consisting of one or more SQL statements that are dependent on each other, and the entire unit of work must be revoked if one of the SQL statements fails to execute.
In a concurrent environment, when multiple transactions access the same data resource at the same time, it can cause various concurrency problems, can be avoided by setting the transaction isolation level of the database, and pessimistic and optimistic locks can be used to resolve the concurrency problem of missing updates.
The database transaction must have acid characteristics:
A:atomic atomicity, the entire transaction is inseparable, either succeeds or is withdrawn.
C:consistency consistency, transactions do not break the integrity of relational data and the consistency of business logic, such as transfers, should ensure that the total deposit of the two accounts after the end of the transaction is unchanged.
I:isolation isolation, when multiple transactions manipulate the same data at the same time, each transaction has its own full data space d:durability persistence, as long as the transaction succeeds, the update to the database must be persisted, even if the system
System crashes and the database is restarted, the database can be restored to the state at the end of the transaction's success.
2. Transaction boundary declarations
As long as a transaction is declared, the database system automatically guarantees the acid characteristics of the transaction.
The Declaration transaction contains:
Start boundary of a transaction
Normal end boundary of transaction (COMMIT): Commit TRANSACTION, persist
Exception end boundary of a transaction (rollback): Undo transaction, database fallback to state before execution of transaction
The database supports two transaction modes:
autocommit mode: Each SQL statement is a separate transaction, and the transaction is automatically committed when the database finishes executing an SQL statement.
Manual commit mode: The start and end boundaries of a transaction must be explicitly specified by the client of the database
The method of transaction control for the JDBC connection class:
Setautocommit (Boolean autocommit) sets whether transactions are committed automatically, by default automatically
Commit () COMMIT Transaction
Rollback () revoking a transaction
Hibernate methods for controlling transactions:
1. Call Sessionfactory the Opensession method without parameters, get the connection from the connection pool, the session automatically sets the connection to commit the transaction mode manually.
Session session = Sessionfactory.opensession ();
If you call opensession with the connection parameter, you will need to set the manual submission yourself:
Connection.setautocommit (FALSE);
Session session = Sessionfactory.opensession (connection);
2. Declaring the start boundary of a transaction
Transaction tx = Session.begintransaction ();
3. Commit a transaction
Tx.commit ();
4. Revoking a transaction:
Tx.rollback ();
A session can correspond to multiple transactions, but priority should be given to a session that only corresponds to one transaction, and when a transaction ends or is revoked, the session is closed.
Whether the transaction succeeds or not, the session's close session should finally be called
At any one time, only one uncommitted transaction is allowed in a session, and two transactions cannot be started at the same time
3. Multi-transaction concurrency issues
There are 5 types of concurrency problems that can occur when you run multiple transactions concurrently to access the same data:
1. First category missing updates: When a transaction is revoked, the updates that have been committed by other transactions are overwritten
2. Dirty reads: One transaction reads uncommitted update data to another transaction
3. Virtual read: One transaction reads the newly inserted data that has been committed by another transaction
4. Non-repeatable reads: One transaction reads the updated data that has been committed by another transaction
5. Category Two missing updates: One transaction overwrites the updated data submitted by another transaction, non-repeatable read exceptions
1. First category missing updates:
T1 Start a transaction
T2 Start a transaction
T3 Enquiry Deposit balance of $1000
T4 Enquiry Deposit balance of $1000
T5 RMB 100, change the balance of deposit to 1100 yuan
T6 COMMIT Transaction
T7 withdraw $100 and change the deposit balance to $900
T8 Cancel the transaction, the balance of the account recovered to 1000 yuan
2. Dirty reads:
T1 Start a transaction
T2 Start a transaction
T3 Enquiry Deposit balance of $1000
T4
T5 withdraw $100 and change the deposit balance to $900
T6 Enquiry account balance of 900 yuan (dirty Read)
T7 Cancel the transaction, the balance of the account recovered to 1000 yuan
T8 RMB 100, deposit balance changed to $1000
T9 COMMIT Transaction
3. Virtual read:
T1 Start a transaction
T2 Start a transaction
T3 statistics website registered for 1000 people
T4 Registering a new user
T5 COMMIT Transaction
T6 Statistics Website Registration number is 1000 people (virtual reading)
T7 which statistic data is valid.
4. Non-repeatable reading:
T1 Start a transaction
T2 Start a transaction
T3 Enquiry account balance of 1000 yuan
T4 Enquiry account balance of 1000 yuan
T5 withdraw $100 and change the balance to $900
T6 COMMIT Transaction
T7 Enquiry account balance of 900 yuan
T8 whether the balance is 1100 yuan or 900+100 yuan.
5. The second category of missing updates:
T1 Start a transaction
T2 Start a transaction
T3 Enquiry account balance of 1000 yuan
T4 Enquiry account balance of 1000 yuan
T5 withdraw $100 and change the balance to $900
T6 COMMIT Transaction
T7 RMB 100, change the balance of deposit to 1100 yuan
T8 COMMIT Transaction
4. Database lock
The database must have the ability to isolate the individual transactions that are running concurrently, and the database uses locks to implement the transaction isolation
The release of sex.
Depending on the resources that the database can lock, it can be divided into the following locks:
Database-Level Locks
Table-Level Locks
Zone-Level locks
Page-level Locks
Key-value Lock: Locks a row of data in a database table with an index
Row-level Locks: Locks a single row of records in a database table
The greater the lock's blocking granularity, the higher the isolation, the lower the concurrency
Lock escalation refers to adjusting the granularity of locks, replacing multiple low-granularity locks with higher-granularity locks to reduce system load.
According to the degree of blockade, locks can be divided into:
Shared lock: For read data, non-exclusive, allows other transactions to read the locked resource at the same time, but does not allow its
His transaction is updated.
Locking: When a SELECT statement is executed, the database assigns a shared lock to the transaction, locking the queried