Spring transaction management
- What is transaction management
- The characteristics of database transactions
- What is a transaction
- What's the use of affairs
- ACID characteristics of transactions
- Transaction isolation
- Spring transaction manager
- Spring transaction management interface
- PlatformTransactionManager interface
- TransactionDefinition interface
- TransactionStatus interface
- Transaction manager included in Spring
- Transactional behavior
- Spring declarative transaction management
- Jar package dependency
What is transaction management
There are two ways of transaction management in the Spring framework: one is traditional programmatic transaction management, that is, transaction management achieved by writing code; the other is declarative transaction management based on AOP technology. Since programmatic transaction management is rarely used in the Spring framework, we only explain Spring's declarative transaction management in detail.
Spring's declarative transaction management uses AOP technology at the bottom. Its biggest advantage is that there is no need to manage transactions through programming. You only need to declare relevant rules in the configuration file to apply transaction rules to business logic.
When we are interviewing, the interviewer sometimes asks whether AOP has been used when using the spring framework. This is actually a pit. After we use declarative transaction management, we use its underlying AOP technology.
Spring implements declarative transaction management mainly in two ways:
Declarative transaction management based on XML files.
Transaction management through Annotation.
The characteristics of database transactions
What is a transaction
A transaction is a series of operations performed as a logical unit. A transaction work unit must have four characteristics: atomicity, consistency, isolation, and durability. Only then can it become a transaction.
What's the use of affairs
The role of things for the database is a series of operations on the data, either all succeed or all fail, to prevent the emergence of intermediate states to ensure that the data in the database is always in the correct state.
ACID characteristics of transactions
数据库的事务的 ACID 特性由数据库的事务管理系统来保证的。 原子性(Atomicity):事务中的所有操作作为一个整体像原子一样不可分割，要么全部成功, 要么全部失败。 一致性(Consistency):事务的执行不能破坏数据库数据的完整性和一致性，一个事务在执 行之前和执行之后，数据库都必须处于一致性状态。如果数据库系统在运行过程中发生故障，有些事务尚未完成就被迫中断，这些未完成的事务对数据库所作的修改有一部分已写入物理 数据库，这时数据库就处于一种不正确的状态，也就是不一致的状态 隔离性(Isolation):并发执行的事务不会相互影响,其对数据库的影响和它们串行执行时 一样。比如多个用户同时往一个账户转账,最后账户的结果应该和他们按先后次序转账的结 果一样。 持久性(Durability):事务一旦提交,其对数据库的更新就是持久的。任何事务或系统故障 都不会导致数据丢失。
Consistency is the ultimate goal of transactions. Atomicity, isolation, and durability are all for achieving consistency.
Ignoring the isolation of transactions will lead to three problems:
dirty reads, non-repeatable reads, and phantom reads (virtual reads). You
should know that there is a difference between non-repeatable reads and phantom reads:
non-repeatable reads occur when data changes during transaction update, phantom reads Generally occurs when new and changed data
solutions (four isolation levels)
Read Uncommited (read uncommitted content) will generate dirty reads
Read Commited (read submitted content) to solve the dirty read problem read submit two queries will produce different Query results will cause non-repeatable read problems.
Repeatable Read (repeatable read) to solve the non-repeatable read problem may also have phantom reading problems
Serializable (serializable) to solve the phantom reading problem
大多数数据库默认的事务隔离级别是 Read committed，比如 Sql Server , Oracle。 Mysql 的默认隔离级别是 Repeatable read。 隔离级别的设置只对当前链接有效。对于使用 MySQL 命令窗口而言，一个窗口就 相当于一个链接，当前窗口设置的隔离级别只对当前窗口中的事务有效；对于 JDBC 操作数据库来说，一个 Connection 对象相当于一个链接，而对于 Connection 对象 设置的隔离级别只对该 Connection 对象有效，与其他链接 Connection 对象无关。
Spring transaction manager
A variety of transaction managers are provided in the Spring framework for transaction management. Spring's transaction manager is implemented based on AOP. Spring's transaction manager includes configuration transaction propagation behavior, isolation level, read-only and timeout properties. These properties provide methods and description strategies for transaction application.
In the development of Java EE projects, a layered model is often used. Spring's transaction processing is located in the business logic layer, and it provides solutions for transactions.
Spring transaction management interface
Three core interfaces for transaction management are included in Spring's transaction module (spring-tx-5.2.7.RELEASE.jar).
The PlatformTransactionManager interface is a transaction manager interface provided by Spring for managing transactions. Spring encapsulates the configuration details of the transaction into the TransactionDefinition object, and then obtains the transaction status (TransactionStatus) through the getTransaction() method of the transaction manager, and performs the next step of the transaction.
Three transaction operation methods are provided in this interface, as follows:
TransactionStatus getTransaction（TransactionDefinition definition）：用于获取事务 状态信息。 void commit（TransactionStatus status）：用于提交事务。 void rollback（TransactionStatus status）：用于回滚事务。
The TransactionDefinition interface is the object of transaction definition (description), which provides methods for obtaining transaction-related information, including five operations, as follows:
String getName()：获取事务对象名称。 int getIsolationLevel()：获取事务的隔离级别。 int getPropagationBehavior()：获取事务的传播行为。 int getTimeout()：获取事务的超时时间。 boolean isReadOnly()：获取事务是否只读。
The TransactionStatus interface is the status of the transaction, which describes the status information of the transaction at a certain point in time. It includes six operations, as follows:
void flush() 刷新事务 boolean hasSavepoint() 获取是否存在保存点 boolean isCompleted() 获取事务是否完成 boolean isNewTransaction() 获取是否是新事务 boolean isRollbackOnly() 获取是否回滚 void setRollbackOnly() 设置事务回滚
Transaction manager included in Spring
Transaction propagation behavior refers to how transactions are propagated among these methods when multiple methods containing transactions call each other.
Spring declarative transaction management
Spring declarative transaction management implementation:
基于 XML 文件方式实现声明式事务管理。 通过 Annotation 注解方式实现声明式事务管理。
Jar package dependency
Spring 核心容器模块 spring-beans-5.2.7.RELEASE.jar spring-context-5.2.7.RELEASE.jar spring-core-5.2.7.RELEASE.jar spring-expression-5.2.7.RELEASE.jar Spring JDBC 模块 spring-jdbc-5.2.7.RELEASE.jar Spring 事务模块 spring-tx-5.2.7.RELEASE.jar Spring AOP 模块 spring-aop-5.2.7.RELEASE.jar AspectJ 框架 aspectjweaver-1.9.5.jar Spring 整合 AspectJ 框架模块 spring-aspects-5.2.7.RELEASE.jar Commons-Loggin 日志 commons-logging-1.2.jar 数据库驱动 mysql-connector-java-5.1.48.jar