声明式事务就是来解决注解式事务的弊端的
那注解式事务的弊端是什么?
- 加载类上的
@Transactional注解对类中所有的方法都起作用,如果类找有查询和增删改,就不好使,这样的话你每个方法都要加注解
Spring通过声明式事务的控制,来完成所有事务的添加
- 创建一个新的配置文件
applicationContext_trans.xml(Spring Config)
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--此配置文件与applicationContext_service.xml的功能一样,只是事务配置不同--><!--导入applicationContext_mapper.xml--><!--添加包扫面--><!--添加事务管理器--><!--使用声明式事务的好处: 可以配置事务切面--><!--绑定切面和切入点--></beans>
- 完成增加操作包
add,save,insert,set - 更新操作包含
update,change,modify - 删除操作包含
delete,drop,remove,clear - 查询操作包含
select,find,search,get
配置事务切面时可以使用通配符的*来匹配所有方法
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--此配置文件与applicationContext_service.xml的功能一样,只是事务配置不同-->
<!--导入applicationContext_mapper.xml-->
<import resource="applicationContext_mapper.xml" />
<!--添加包扫面-->
<context:component-scan base-package="com.chentianyu.service.impl" />
<!--添加事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--使用声明式事务的好处: 可以配置事务切面-->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--你项目中所有的方法的命名包含这些关键词,我就可以统一的给你上事务的切面-->
<tx:method name="*select*" read-only="true"/>
<tx:method name="*find*" read-only="true"/>
<tx:method name="*search*" read-only="true"/>
<tx:method name="*get*" read-only="true"/><!--read-only:只读,默认为false-->
<tx:method name="*insert*" propagation="REQUIRED" /><!--propagation:需要事务的使用这个标签-->
<tx:method name="*add*" propagation="REQUIRED" />
<tx:method name="*save*" propagation="REQUIRED" />
<tx:method name="*set*" propagation="REQUIRED" />
<tx:method name="*update*" propagation="REQUIRED" />
<tx:method name="*modify*" propagation="REQUIRED" />
<tx:method name="*change*" propagation="REQUIRED" />
<tx:method name="*delete*" propagation="REQUIRED" />
<tx:method name="*drop*" propagation="REQUIRED" />
<tx:method name="*remove*" propagation="REQUIRED" />
<tx:method name="*clear*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" /><!--除了以上命名的所有方法支持事务-->
</tx:attributes>
</tx:advice>
<!--绑定切面和切入点-->
<aop:config>
<!--这就指定了所有的切入点是在业务逻辑层中的-->
<aop:pointcut id="mycut" expression="execution(* com.chentianyu.service.impl.*.*(..))"/>
<!--绑定-->
<aop:advisor advice-ref="myAdvice" pointcut-ref="mycut" />
</aop:config>
</beans>
就完成了整个事务的处理
测试
UsersServiceImpl.java
@Service
public class UsersServiceImpl implements UsersService {...}
AccountServiceImpl.java
@Service
public class AccountServiceImpl implements AccountService {...}
测试
@Test
public void test03(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_trans.xml");
UsersService usersService = (UsersService) ac.getBean("usersServiceImpl");
usersService.insertUsers(new Users(2,"李四","2345"));
}
结果时报错的,因为AccountServiceImpl里面有System.out.println(1/0);异常,又因为AccountServiceImpl是包含在UsersServiceImpl里面的,所以都不会添加进去,详情请看
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@71104a4] will not be managed by Spring
==> Preparing: insert into users values(?,?,?)
==> Parameters: 1(Integer), 张三(String), 123(String)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@476b0ae6]
1
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ac85b0c] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@71104a4] will not be managed by Spring
==> Preparing: insert into accounts values(?,?,?)
==> Parameters: 300(Integer), 王五(String), 账户好的呢(String)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ac85b0c]
添加用户成功,num=1
java.lang.ArithmeticException: / by zero
表面上成功了,但是数据库中没有数据
| userid | uname | upass |
|---|---|---|
| aid | aname | acountent |
|---|---|---|
我们在使用注解事务的时候可以使异常不回滚的,那我们也可以在声明式的事务中也可以使用,写法是:添加no-rollback-for
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--此配置文件与applicationContext_service.xml的功能一样,只是事务配置不同-->
<!--导入applicationContext_mapper.xml-->
<import resource="applicationContext_mapper.xml" />
<!--添加包扫面-->
<context:component-scan base-package="com.chentianyu.service.impl" />
<!--添加事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--使用声明式事务的好处: 可以配置事务切面-->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--你项目中所有的方法的命名包含这些关键词,我就可以统一的给你上事务的切面-->
<tx:method name="*select*" read-only="true"/>
<tx:method name="*find*" read-only="true"/>
<tx:method name="*search*" read-only="true"/>
<tx:method name="*get*" read-only="true"/><!--read-only:只读,默认为false-->
<tx:method name="*insert*" propagation="REQUIRED" no-rollback-for="ArithmeticException" /><!--propagation:需要事务的使用这个标签-->
<tx:method name="*add*" propagation="REQUIRED" />
<tx:method name="*save*" propagation="REQUIRED" no-rollback-for="ArithmeticException" />
<tx:method name="*set*" propagation="REQUIRED" />
<tx:method name="*update*" propagation="REQUIRED" />
<tx:method name="*modify*" propagation="REQUIRED" />
<tx:method name="*change*" propagation="REQUIRED" />
<tx:method name="*delete*" propagation="REQUIRED" />
<tx:method name="*drop*" propagation="REQUIRED" />
<tx:method name="*remove*" propagation="REQUIRED" />
<tx:method name="*clear*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" /><!--除了以上命名的所有方法支持事务-->
</tx:attributes>
</tx:advice>
<!--绑定切面和切入点-->
<aop:config>
<!--这就指定了所有的切入点是在业务逻辑层中的-->
<aop:pointcut id="mycut" expression="execution(* com.chentianyu.service.impl.*.*(..))"/>
<!--绑定-->
<aop:advisor advice-ref="myAdvice" pointcut-ref="mycut" />
</aop:config>
</beans>
