mybati事物配置

事物---申明(service),注解(可以到control),遇到无效情况----mysql 配置改 innerdb  切面中改 (service层方法)throw  runtimeexception(或者配置事物的方法中加rollback-for="Exception")
配置rollback-for="Exception"只要service抛出exception即可,不配要跑出runingtimeexception,至于control层继续抛出进行全局异常处理还是,catch写到一个页面随便




申明式实物,注解事物-----------------、????????????????????????????????
   http://www.tuicool.com/articles/6Jb6nm
   http://blog.csdn.net/z69183787/article/details/37819831
   Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_servlet.xml)产生的是子容器。子容器Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,
   而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service exclude掉,此时得到的将是原样的无事务处理能力的Service。
     <mvc:annotation-driven  >
<context:component-scan base-package="com.wondersgroup.employeeBenefits" >
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
<!-- 自动扫描(自动注入) -->
<context:component-scan base-package="com.wondersgroup.employeeBenefits" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

service 层要加@service主机
   http://my.oschina.net/u/205170/blog/152438
   http://www.mamicode.com/info-detail-668183.html
    @Transactional注解标签

因为在运行中,所以使用RuntimeException进行异常抛出,类中不能使用try捕捉异常,直接throws Exception抛出异常

如运行时使用Exception抛出异常,则回滚无效
  配置事物的异常不能catch调 要一直抛出处理
  ===============================================================================
  父容器排除control(listeren中加载的配置文件),子容器排除service
  父
  <context:component-scan base-package="com.wondersgroup.employeeBenefits" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
<context:annotation-config/>父启动会注解
<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" /> //申明式事物和注解事物可以同时存在

  子
  <context:component-scan base-package="com.wondersgroup.employeeBenefits" >
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
<mvc:annotation-driven/>spring mvc这个子容器启动注解

control可以catch 也可throws,service不能catch只能throw
事物类要@service标记
具体中的方法
@Transactional (readOnly =  false , rollbackFor = Exception.class )  //默认只用runningtimeexception

数据库要支持mysql  innerdb

<tx:method name="tx*" propagation="REQUIRED" rollback-for="Exception"/>//合并于当前大的事物中
 
 

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2249809