Declarative transaction (XML(tx namespace management transaction))
tx configuration
tx:advice
-
Name: tx:advice
-
Type: Label
-
Attribution: beans tag
-
Role: dedicated to declaring transaction notifications
-
Format:
<beans> <tx: advice id ="txAdvice" transaction-manager="txManager"> </tx:advice> </beans>
-
Basic properties:
- id: used to specify the id of the notifier when configuring aop
- Transaction manager: Specifies the transaction manager bean
tx:attributes
-
Name: tx:attributes
-
Type: Label
-
Attribution: tx:advice tag
-
Role: define notification properties
-
Format:
<tx:advice id= "txAdvice" transaction-manager="txManager"> <tx: attributes> </tx:attributes> </tx:advice>
-
Basic properties:
- nothing
tx:method
-
Name: tx:method
-
Type: Label
-
Attribution: tx:attribute tag
-
Function: set specific transaction attributes
-
Format:
<tx:attributes> <!--All operations add read-write transactions--> <tx:method name="*" read-only=" false" /> <!--get All operations at the beginning are added with read-only transactions--> <tx:method name="get*" read only="true" /> </tx:attributes>
-
explain:
- Usually, multiple transaction attributes are configured, including one read-write full transaction attribute and one read-only query transaction attribute
code implementation
Extract the transaction processing function of the business layer, make it into an AOP notification class, and modify it into the form of a configuration file, that is, replace the TxAdvice class and the content of configuring AOP in applicationContext.xml
-
Declare the namespace of tx in applicationContext.xml
<beans ... xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="... http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd ...">
-
Replace in applicationContext.xml
-
Configuring transaction specific notification classes using the tx namespace
<!--Define notification classes for transaction management--> <tx:advice id="txAdvice" transaction-manager="txManager"> <!--Define controlled transactions--> <tx:attributes> <!-- All operations add read-write transactions--> <tx:method name="*" read-only="false"/> <!-- get All operations at the beginning are added with read-only transactions--> <tx:method name="get*" read-only="true"/> <!--find All operations at the beginning are added with read-only transactions--> <tx:method name="find*" read-only="true"/> </tx:attributes> </tx:advice>
-
Use aop:advisor to reference transaction specific notification classes in AOP configuration
<aop:config> <!-- com.yy.service All in service The interface at the end, all methods in the, That is, monitor all methods in the interface and accurately lock the business layer, so that there will be no problems--> <aop:pointcut id="pt" expression="execution(* com.itheima.service.*Service.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config>
-
The difference between aop:advice and aop:advisor
- AOP: the notification class configured by advice can be ordinary java objects, which do not implement interfaces or use inheritance relationships
- AOP: the notification class configured by the advisor must be implemented on the notification interface
- MethodBeforeAdvice
- AfterReturningAdvice
- ThrowsAdvice
- ...
tx:method attribute
<tx:method <!--Which method--> name="transfer" read-only="false" <!--Timeout duration,-1 No restrictions--> timeout="-1" <!--The isolation level follows the database by default--> isolation="DEFAULT" <!--If there are exceptions configured later in the transaction, do not roll back --> no-rollback-for="" <!--If there is any exception configured in the transaction, it must be rolled back, The above and this are a special case --> rollback-for="" <!-- --> propagation="REQUIRED" />
Transaction propagation behavior
-
Transaction administrator
-
Affairs Coordinator
-
Transaction propagation behavior: describes the transaction coordinator's attitude towards the transaction carried by the transaction administrator
It is the relationship between the affairs of the affairs coordinator Kim and the affairs of the affairs administrator, that is, the attitude of the affairs coordinator towards the affairs carried by the affairs administrator
-
REQUIRED: if the transaction administrator starts a transaction, the transaction coordinator will join the transaction, so they both use the same transaction, both T1; If the transaction administrator does not start a transaction, the transaction coordinator will directly create a new transaction
-
Requirements new: the transaction coordinator will create a new transaction no matter whether the transaction administrator starts or does not start a transaction
-
SUPPORTS: if the transaction administrator originally has a transaction, the transaction coordinator will join the transaction; If the transaction administrator does not have a transaction, the transaction coordinator will not join and will not be involved in the transaction
-
NOT SUPPORTED: the transaction coordinator will not have any transactions when the transaction administrator comes
-
MANDATORY: there must be a transaction. When the transaction administrator comes with a transaction, the transaction coordinator will join the transaction. If there is no transaction when the transaction administrator comes, an error will be reported
-
NEVER: there is no need to bring a transaction. If the transaction administrator brings a transaction, an error will occur; If there is no transaction, the transaction coordinator will not have a transaction
-
NESTED: relative to the rollback point set before rollback
tx configuration of applicationContext.xml
<!-- TX format --> <!-- TX format --> <!-- TX format --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--Define notification classes for transaction management--> <tx:advice id="txAdvice" transaction-manager="txManager"> <!--Define controlled transactions--> <tx:attributes> <!-- All operations add read-write transactions--> <tx:method name="*" read-only="false"/> <!-- get All operations at the beginning are added with read-only transactions--> <tx:method name="get*" read-only="true"/> <!--find All operations at the beginning are added with read-only transactions--> <tx:method name="find*" read-only="true"/> <!-- Declare other transaction operations--> <tx:method name="a" read-only="false" propagation="REQUIRED"/> <tx:method name="b" read-only="false" propagation="NEVER"/> <tx:method name="transfer" read-only="false" timeout="-1" isolation="DEFAULT" no-rollback-for="" rollback-for="" propagation="REQUIRED" /> <!--<tx:method name="transfer" read-only="false"/>--> </tx:attributes> </tx:advice> <aop:config> <!-- com.yy.service All in service The interface at the end, all methods in the, That is, monitor all methods in the interface and accurately lock the business layer, so that there will be no problems--> <aop:pointcut id="pt" expression="execution(* com.itheima.service.*Service.*(..))"/> <aop:pointcut id="pt2" expression="execution(* com.itheima.dao.*.b(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt2"/> </aop:config>