catalogue
Programming transaction control related objects
Three objects of programming transaction control
Declarative transaction control based on XML
Implementation of declarative transaction control
Configuring declarative transaction control using annotations
Programming transaction control related objects
Three objects of programming transaction control
1,PlatformTransactionManager
The PlatformTransactionManager interface is the transaction manager of spring, which provides our common methods of operating transactions.
Note: PlatformTransactionManager is an interface type, and different Dao layer technologies have different implementation classes. For example, when Dao layer technology is jdbo or mybatis: org.springframework.jdbc.datasource.DataSourceTransactionManager
When Dao layer technology is Hibernate: org.springframework.orm.hibernate5.hibernatetransaction Manager
2,TransactionDefinition
TransactionDefinition is the transaction definition information object (parameter), which contains the following methods:
① Transaction isolation level
Setting the isolation level can solve the problems caused by transaction concurrency, such as dirty read, non repeatable read and virtual read.
ISOLATION_DEFAULT
ISOLATION_READ_UNCOMMITTED
ISOLATION_READ_COMMITTED
ISOLATION_REPEATABLE_READ
ISOLATION_SERIALIZABLE
② Communication behavior of things [A-B]
REQUIRED: if there is no transaction at present, create a new transaction. If there is already a transaction, join it. General selection (default)
SUPPORTS: SUPPORTS the current transaction. If there is no transaction, it will be executed in a non transactional manner (no transaction)
MANDATORY: use the current transaction. If there is no transaction, an exception will be thrown
Requests_new: create a new transaction. If it is currently in a transaction, suspend the current transaction.
NOT_SUPPORTED: execute the operation in a non transactional manner. If there is a current transaction, suspend the current transaction. NEVER: run in a non transactional manner. If there is a current transaction, throw an exception
NESTED: if there is a transaction currently, execute within the NESTED transaction. If there is no transaction currently, execute REQUIRED
Similar operation
Timeout: the default value is - 1. There is no timeout limit. If there is, it is set in seconds
Read only: it is recommended to set it as read-only when querying
3,TransactionStatus
The TransactionStatus interface provides the specific running status of transactions. The methods are described below.
Declarative transaction control based on XML
What is declarative transaction control?
As the name suggests, Spring's declarative transaction is to process transactions in a declarative way. The declaration here refers to declaring in the configuration file and replacing the code type transaction with the declarative transaction in the Spring configuration file.
The role of declarative transactions
① Transaction management does not invade the developed components. Specifically, business logic objects will not realize that they are in transaction management. In fact, it should also be so, because transaction management is a system level service, not a part of business logic. If you want to change the transaction management plan, you only need to reconfigure it in the definition file
② When transaction management is not required, the transaction management service can be removed by modifying the setting file without changing the code and recompiling, which is extremely convenient for maintenance
Note: the underlying layer of Spring declarative transaction control is AOP.
Implementation of declarative transaction control
① Introducing tx namespace
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
② Configure transaction enhancements
<!--Configure platform transaction manager--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--Enhancement of notification transactions--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--Sets the attribute information of the transaction--> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice>
③ Configure transaction AOP weaving
<!--Configure the of the transaction aop Weave in--> <aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.longdi.service.impl.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config>
④ Test transaction control transfer business code
@Override public void transfer(String outMan, String inMan, double money) { accountDao.out(outMan,money); int i = 1/0; accountDao.in(inMan,money); }
Transaction parameter configuration of pointcut method
<!--Enhancement of notification transactions--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--Sets the attribute information of the transaction--> <tx:attributes> <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice>
Where, < TX: method > represents the configuration of the transaction parameters of the pointcut method, for example:
<tx:method name= "transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="-1"read-only="false" / >
Name: pointcut method name
Isolation: isolation level of transaction
Propagation: propagation behavior of transactions
Timeout: timeout
Read only: read only
Configuring declarative transaction control using annotations
1. Write AccoutDao
@Repository("accountDao") public class AccountDaoImpl implements AccountDao { @Autowired private JdbcTemplate jdbcTemplate; public void out(String outMan, double money) { jdbcTemplate.update("update account set money=money-? where name=?",money,outMan); } public void in(String inMan, double money) { jdbcTemplate.update("update account set money=money+? where name=?",money,inMan); } }
2. Write AccountService
@Service("accountService") @Transactional(isolation = Isolation.REPEATABLE_READ) public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED) public void transfer(String outMan, String inMan, double money) { accountDao.out(outMan,money); int i = 1/0; accountDao.in(inMan,money); } //@Transactional(isolation = Isolation.DEFAULT) public void xxx(){} }
3. Write the applicationContext.xml configuration file
<!I. before omission datsSource,jdbcTemplate,Configuration of platform transaction manager--> <!--Component scan--> <context:component-scan base-package="com.longdi"/> <!--Annotation driven--> <tx:annotation-driven transaction-manager="transactionManager"/>
Annotation configuration declarative transaction control resolution
① Use @ Transactional to modify classes or methods that need transaction control. The attributes available for annotation are the same as xml configuration methods, such as isolation level, propagation behavior, etc.
② If annotations are used on a class, all methods under the class are configured with the same set of annotation parameters.
③ In terms of methods, different methods can adopt different transaction parameter configurations.
④ Annotation driven to enable transaction in XML configuration file < TX: annotation driven / >