The basic elements of the transaction:
Atomicity (atomicity): all operations after the start of the transaction, either all executed successfully, if the intermediate error occurs, the transaction is rolled back to the state before the transaction began.
Consistency ( Consistency ): After the start of the transaction, integrity constraints of the database is not corrupted, for example:. A transfer to B, A deduction of the amount, B did not receive missing
Isolation ( Isolation ): the same time, only one transaction request for the same data, there is no mutual interference between different transactions.
Persistent (Durability): After the completion of the transaction, the transaction all updates to the database are kept in the database and can not be rolled back.
Transaction concurrency problem
Dirty read: Transaction A transaction B reads the updated data, the transaction is rolled back transaction B A read data is dirty
Unrepeatable reads: A transaction reads the same data multiple times, transaction B A several times during the transaction, the data and submit multiple updates, results in the same data repeatedly read transaction A, inconsistent results
Magic Reading: The system administrator student achievement for all database changes from a specific fraction of ABCD grades, the original management system to add specific scores a B student management system changed after the original A data not found that there is a change overnight, though. hallucinations same.
summary:
Weight to the modified non-repeatable read, row lock solution taken to meet the conditions. Phantom read to insert and delete weight, solution lock table
MySQL transaction isolation level
Transaction isolation level | Dirty read | Non-repeatable read | Magic Reading |
Read Uncommitted (read-uncommit) | Yes | Yes | Yes |
Non-repeatable read (read-committed) | no | Yes | Yes |
Repeatable read (repeatable-read) | no | no | Yes |
Serialization (Serializable) | no | no | no |
mysql default transaction isolation level: Repeatable Read (reeatable-read)
In spring Transaction Management
Transaction abstraction
Provides a consistent transaction model (JDBC / Hibernate / mybatis / dataSource / JTA)
Transaction Interface
PlatformTransactionManager
-
-
- DataSourceTransactionManager
- HibernateTransactionManager
- JtaTransactionManager
-
Transaction-related methods
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;
TransactionStatus getTransaction(@Nullable TransactionDefinition definition) throws TransactionException;
Transaction-related definitions
TransactionDefinition
Attributes:
Propagation (propagation mode)
Isolation (isolation)
Timeout
Read-only status (true or false)
Custom Services:
// 其中 dataSource 框架会自动为我们注入
@Bean(name = "MyTransaction")
public PlatformTransactionManager txManager(@Qualifier("MyDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
传播特性
传播性 | 值 | 描述 |
PROPAGATION_required | 0 | 当前有事务就用, 没有事务就新建一个事务(默认) |
PROPAGATION_supports | 1 | 事务可有可无,不是必须的 |
PROPAGATION_mandatory | 2 | 当前一定要有事务,不然就抛出异常 |
PROPAGATION_REQUIRED_NEW | 3 | 无论是否有事务,都创建一个新事务 |
PROPAGEATION_NOT_SUPPORTS | 4 |
不支持事务,按照非事务方式进行 |
PROPAGEATION_NAVER | 5 |
不支持事务,如果有事务则抛出异常 |
PROPAGEATION_NESTED | 6 | 当前有事务就在当前事务内在起一个事务,内部事务不影响外部事务 |
事务隔离特性
隔离性 | 值 | 脏读 | 不可重复读取 | 幻读 |
ISOLATION_READ_UNCOMMITTED (未提交) | 1 | √ | √ | √ |
ISOLATION_READ_COMMITTED(不可重复读取) | 2 | × | √ | √ |
ISOLATION_REPEATABLE_READ(可重复读取) | 3 | × | × | √ |
ISOLATION_SERIAILZABLE(串行化) | 4 | × | × | × |