Spring 事务控制配置

事务配置实现方式有两种方法:xml配置和注解配置。
注意:默认Spring事务只在发生未被捕获的 runtimeexception 时才回滚。

Xml配置方式

配置命名空间

spring配置文件中加入事务 和 aop 命名空间

事务

1
2
3
4
xmlns:tx="http://www.springframework.org/schema/tx" 

http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd

Aop

1
2
3
4
xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd

配置如下

spring.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">

</beans>

开启Aop自动代理

spring.xml
1
<aop:aspectj-autoproxy />

配置数据源

这里使用c3p0

srping.xml
1
2
3
4
5
6
7
8
9
10
<!-- 配置数据库基本信息 -->
<context:property-placeholder location="classpath:db.properties" />

<!-- 配置 c3p0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}"/>
</bean>

配置事务管理器

这里使用的是jdbc

spring.xml
1
2
3
4
5
<!-- 配置数据源的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 使用上面定义的数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>

配置事务相关通知

spring.xml
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 设置事务通知 -->
<!-- add inert update delete 开头的方法都启用事务管理 -->
<!-- 指定事务管理器是上面定义的事务管理器txManager -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="query*" read-only="true"/>
</tx:attributes>
</tx:advice>

配置Aop

配置事务切面,这里配置 org.example.service包及其下面子包的所有类的所有方法

spring.xml
1
2
3
4
5
6
7
<!-- aop 切面配置 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="servicePointcut" expression="execution(* org.example.service..*.*(..))"/>
<!-- 配置通知 txAdvice 是上面定义的事务通知 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" />
</aop:config>

注解配置方式

配置事务管理器

这里使用的是jdbc

spring.xml
1
2
3
4
5
<!-- 配置数据源的事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 使用上面定义的数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>

配置注解支持

spring.xml
1
2
<!-- 配置事务注解支持 -->
<tx:annotation-driven transaction-manager="txManager"/>

方法上加入事务注解

业务层需要添加事务的方法上加上事务注解。

UserService
1
2
3
4
5
6
7
8
/**
* 测试事务注解
*/
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation= Propagation.REQUIRED)
public void test(){}