在applicationContext.xml中配置中配置以下内容

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--配置事务增强,事务如何切入-->
<tx:advice id="txAdvice">
<tx:attributes>
<!--所有方法都是事务方法-->
<tx:method name="*"/>
<!--以get开始的所有方法-->
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* org.ssm..controller..*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>

发现报错后事务没有回滚,将controller改成service后(如下),service层是可以回滚的。

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--配置事务增强,事务如何切入-->
<tx:advice id="txAdvice">
<tx:attributes>
<!--所有方法都是事务方法-->
<tx:method name="*"/>
<!--以get开始的所有方法-->
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* org.ssm..service..*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>

如果想要controller层回滚,则需要把切入点配置在spring-mvc.xml中

spring-mvc.xml

1
2
3
4
5
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* org.ssm..controller..*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
<!--配置事务增强,事务如何切入-->
<tx:advice id="txAdvice">
<tx:attributes>
<!--所有方法都是事务方法-->
<tx:method name="*"/>
<!--以get开始的所有方法-->
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
</tx:attributes>
</tx:advice>

这样controller层就可以回滚了,但如果在spring-mvc.xml中配置层service(如下),是不生效的,service并不会回滚

spring-mvc.xml

1
2
3
4
5
 <aop:config>
<!--切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* org.ssm..service..*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>