zoukankan      html  css  js  c++  java
  • 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题(转载)

    1、问题复现

         spring 3.0 + hibernate 3.2

         spring mvc使用注解方式;service使用@service注解 事务使用@Transactional

         事务配置使用

      
    Java代码 复制代码 收藏代码
    1. <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />  
    1. <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />  

         在插入或更新数据时,无报错,但数据库中无结果,而查询正常。疑为事务未提交。

    2、问题检查

         当修改dao层实现逻辑为:

    1. Assert.notNull(entity, "entity不能为空");   
    2. Transaction ts = getSession().beginTransaction();   
    3. getSession().saveOrUpdate(entity);   
    4. getSession().flush();   
    5. ts.commit();   
    6. logger.debug("save entity: {}", entity);  
    Java代码 复制代码 收藏代码
    1. Assert.notNull(entity, "entity不能为空");   
    2. Transaction ts = getSession().beginTransaction();   
    3. getSession().saveOrUpdate(entity);   
    4. getSession().flush();   
    5. ts.commit();   
    6. logger.debug("save entity: {}", entity);  
    1. Assert.notNull(entity, "entity不能为空");  
    2. Transaction ts = getSession().beginTransaction();  
    3. getSession().saveOrUpdate(entity);  
    4. getSession().flush();  
    5. ts.commit();  
    6. logger.debug("save entity: {}", entity);  

       可以正常提交插入、更新。确定为事务未提交。

    3、问题分析

        spring mvc使用注解方式时需要使用

    Xml代码 复制代码 收藏代码
    1. <context:component-scan base-package="com.fengzhiyin" />  
    1. <context:component-scan base-package="com.fengzhiyin" />  

        方式用来扫描该包以及其子包下的@Controller注解的类,纳入spring管理,而同时spring 容器也需要使用这种方式扫描包含@Service、@Components、@Required、@Autowired等注解用来管理bean和完成DI。

       

    Java代码 复制代码 收藏代码
    1. <context:component-scan base-package="com.fengzhiyin" />  
    1. <context:component-scan base-package="com.fengzhiyin" />  

     出现在spring mvc的配置文件中时,web 容器在扫描包含@Service或@Components的类并包含@Transaction是,此时@Transaction并为完成,导致事务未被注册。

    4、问题解决

    分两部分扫描:

    spring-mvc.xml中扫描controller

    application.xml中扫描其他的

    mvc 的只扫描controller组件 注意使用 use-default-filters="false" 
    <context:component-scan base-package="com.fengzhiyin" use-default-filters="false" > 
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 
    </context:component-scan> 

    主体的扫描除controller外的所有组件 
    <context:component-scan base-package="com.fengzhiyin" > 
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 
    </context:component-scan>


    经调试代码发现: 

    1、如果不设置use-default-filters="false",则Spring会扫描并优先注册默认的bean(当然包括标记为@Service的bean),这样,标记为@Transactional的service由于transaction manager尚未注册而未能生效,导致事务管理失效。 
    原理是:标记为@Transactional的service会wrap为经过transactional proxied(不管是CGLIB based或是JDK based)的bean,而不再是纯的service; 

    2、app的context-scan其实无所谓,但exclude掉controller显然会improve efficiency.

  • 相关阅读:
    Codeforces 912 D. Fishes (贪心、bfs)
    Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)
    HDU
    HDU
    POJ-2299 Ultra-QuickSort (树状数组)
    deque!
    HDU
    乘法逆元
    Codeforces 911D. Inversion Counting (数学、思维)
    Codeforces 909E. Coprocessor (拓扑、模拟)
  • 原文地址:https://www.cnblogs.com/wjwen/p/4021650.html
Copyright © 2011-2022 走看看