Spring事务管理注意小事项

在service类前加上@Transactional,声明这个service所有方法需要事务管理。每一个业务方法开始时都会打开一个事务。

Spring默认情况下会对运行期例外(RunTimeException)进行事务回滚。这个例外是unchecked

如果遇到checked意外就不回滚。

如何改变默认规则:

1 让checked例外也回滚:在整个方法前加上 @Transactional(rollbackFor=Exception.class)

2 让unchecked例外不回滚: @Transactional(notRollbackFor=RunTimeException.class)

3 不需要事务管理的(只查询的)方法:@Transactional(propagation=Propagation.NOT_SUPPORTED)

4 如果不添加rollbackFor等属性,Spring碰到Unchecked Exceptions都会回滚,不仅是RuntimeException,也包括Error。

注意点: 1. @Transactional注解起作用的方法必须是public类型  

          2.该方法必须抛出异常,如果异常被try{}catch{}了,事务就不回滚了,

      如果想让事务回滚必须再往外抛try{}catch{throw Exception}。

        3.mysql数据库类型的选择(InnoDB) 

    4.Controller层只支持 @Transactional 注解式事务!

    5.另外,事务没有指定在最外层方法,会无效,如:在InventoryController中调用了InventoryService类的createAndDownloadData()方法,

    而createAndDownload()方法中又调用了createInfo()方法,实际上需要做事务控制的内容就在createInfo()方法中,但是,

    如果将@Transactional注解加在createInfo()方法上,事务却不生效;而把@Transactional加在createAndDownload()方法上,事务就正常生效。

spirng-mybatis配置事务

@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class) 

/**
* 配置事务 同时添加成功才可以,否则回滚
*/

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">


<!-- 开启事务注解驱动 -->
<tx:annotation-driven />

<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

经过自己验证,通过上述方法在controller层也能成功配置事务,但网上许多人表示有问题,并不能成功,

下面是他们的找出的问题根源和解决办法:

1、在主容器中(applicationContext.xml),将Controller的注解排除掉 
<context:component-scan base-package="com"> 
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
</context:component-scan> 

而在springMVC配置文件中将Service注解给去掉 
<context:component-scan base-package="com"> 
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
  </context:component-scan> 

因为spring的context是父子容器,所以会产生冲突,由ServletContextListener产生的是父容 器,springMVC产生的是子容器,子容器Controller进行扫描装配时装配了@Service注解的实例,而该实例理应由父容器进行初始化以 保证事务的增强处理,所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。 

还有一种方式是将service层改用xml配置,其实这样做也是变相的让springmvc无法扫描service,而只能依赖父窗口也就是ServletContextListener来进行初始化,这样同样被赋予了事务性。

2、自己单独做了个小网站 但是发现action事务不起作用了 但是如果用service层就没问题 找了很多办法没解决 最后自己解决了

其实就是一个加载顺序的问题
首先使用了spring MVC的项目是不需要配置action bean  而是通过spring mvc的配置文件进行扫描注解加载的
spring事务配置文件还有上下文都是通过org.springframework.web.context.ContextLoaderListener加载的,
而spring MVC的action是通过org.springframework.web.servlet.DispatcherServlet加载的 
这样就有个优先级的问题了  web是先启动ContextLoaderListener后启动DispatcherServlet
在ContextLoaderListener加载的时候action并没在容器中,所以现在使用AOP添加事务或者扫描注解都是无用的。
那么解决办法就是在DispatcherServlet 加载spring-MVC配置文件后再进行一次AOP事务扫描和注解事务扫描就OK了

<tx:annotation-driven transaction-manager="transactionManager"/>
<aop:config>  
        <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.yang.web.*.action.*.*(..))"/>  
</aop:config>

原文地址:https://www.cnblogs.com/jobs-lgy/p/6392338.html