Transaction
Learning Goals |
Case-Complete transfer |
I. Overview of transactions 1. What is a transaction
One thing has n constituent units or this N constituent unit succeeds at the same time either n units fail at the same time
is to put n elements into one transaction
2. MySQL's business
Default transaction: An SQL statement is a transaction that opens the transaction and commits the transaction by default
Manual transaction:
1) Open a transaction as shown: Start transaction
2) Transaction commit: Commit represents all SQL from open transaction to transactional commit is considered valid for real update database
3) Rollback of the transaction: rollback represents the rollback of the transaction from the open transaction to the middle of the transaction rollback, all SQL operations considered that the invalid database was not updated
Second, JDBC transaction operations
The default is automatic transactions:
Execute SQL statement: executeupdate ()----Each time the Executeupdate method is executed on behalf of the transaction autocommit
Manual transactions through the JDBC API:
Open transaction: Conn.setautocomnmit (FALSE);
Commit TRANSACTION: Conn.commit ();
ROLLBACK TRANSACTION: Conn.rollback ();
Note: The connnection of a control transaction must be the same
The connection of the execution SQL and the connnection of the open transaction must be the same to control the transaction
Third, dbutils transaction operation 1. Queryrunner
Structure: Queryrunner runner = new Queryrunner (DataSource DataSource);
A reference construct passing a data source (connection pool) as a parameter to Queryrunner,queryrunner will get a database connection resource operations database from the connection pool, so you can manipulate the database directly using the Update method without the connection parameter
Non-parametric construction: Queryrunner runner = new Queryrunner ();
The parameterless construct does not pass the data source (connection pool) as a parameter to the Queryrunner, so we use a method with connection parameters when manipulating the database with the Queryrunner object
Iv. using threadlocal binding to connect resources
V. Characteristics and isolation level of the transaction (conceptual question---interview) 1. Characteristics of the transaction acid
? 1) atomicity (atomicity) means that a transaction is an inseparable unit of work, and the operations in the transaction either occur or do not occur.
? 2) Consistency (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 is that when multiple users access the database concurrently, the transaction of one user cannot be interfered by other users ' transactions, and the data between multiple concurrent transactions is isolated from each other.
? 4) persistence (durability) persistence refers to the fact that once a transaction is committed, it changes the data in the database to be permanent, and then it should not have any effect even if the database fails.
2. Concurrent access issues----caused by isolation
If isolation is not considered, there are 3 concurrent access issues in the transaction.
1) Dirty read: b transaction read to a transaction has not yet committed data------require B transaction to read the data submitted by a transaction
2) Non-repeatable reads: The content of data that is read two times in a transaction is inconsistent-----requires that data be consistent over multiple reads in a transaction---unpdate
3) Phantom read/virtual read: The number of data read two times in a transaction is inconsistent-----requires that the number of data read more than one transaction is consistent--insert Delete
3. Isolation level of a transaction
1) READ UNCOMMITTED: Read data that has not yet been committed: no problem is solved
2) Read Committed: Reads the data that has been submitted: can resolve dirty read----Oracle Default
3) Repeatable READ: Reread read: Can resolve dirty read and non-repeatable read---mysql default
4) Serializable: Serialization: Can resolve dirty read non-repeatable read and virtual read---equivalent to the lock table
Note: The default isolation level for MySQL databases
View the default isolation level for MySQL database: SELECT @ @tx_isolation
Set the isolation level for MySQL: Set session transaction ISOLATION level setting transaction isolation Levels
Summarize:
MySQL's transaction control:
Open transaction: Start transaction;
Commit: Commit;
Rollback: rollback;
JDBC Transaction control:
Open transaction: Conn.setautocommit (FALSE);
Submitted by: Conn.commit ();
Rollback: Conn.rollback ();
Dbutils's transaction control is also through JDBC
ThreadLocal: Implements a thread-bound way to pass parameters
Concept:
Characteristics of the transaction acid
Concurrency problems: Dirty reading, non-rereading, virtual reading \ Phantom Reading
Resolving Concurrency: Setting isolation Levels
READ UNCOMMITTED
Read Committed
Repeatable READ (MySQL default)
Serialazable
Performance at the isolation level:
Read Uncommitted>read committed>repeatable read>serialazable
Security:
Read Uncommitted<read committed<repeatable read<serialazable
Figures:
Transfer operation
"Javaweb Study notes" 19_ Business