解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题
来源:互联网 发布:低通滤波算法原理 编辑:程序博客网 时间:2024/05/22 01:35
1、问题复现
spring 3.0 + hibernate 3.2
spring mvc使用注解方式;service使用@service注解 事务使用@Transactional
事务配置使用
- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
在插入或更新数据时,无报错,但数据库中无结果,而查询正常。疑为事务未提交。
2、问题检查
当修改dao层实现逻辑为:
- Assert.notNull(entity, "entity不能为空");
- Transaction ts = getSession().beginTransaction();
- getSession().saveOrUpdate(entity);
- getSession().flush();
- ts.commit();
- logger.debug("save entity: {}", entity);
- Assert.notNull(entity, "entity不能为空");
- Transaction ts = getSession().beginTransaction();
- getSession().saveOrUpdate(entity);
- getSession().flush();
- ts.commit();
- logger.debug("save entity: {}", entity);
可以正常提交插入、更新。确定为事务未提交。
3、问题分析
spring mvc使用注解方式时需要使用
方式用来扫描该包以及其子包下的@Controller注解的类,纳入spring管理,而同时spring 容器也需要使用这种方式扫描包含@Service、@Components、@Required、@Autowired等注解用来管理bean和完成DI。
- <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.
- 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题
- 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题
- 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题
- 关于Spring事务<tx:annotation-driven/>的理解。
- 关于Spring事务<tx:annotation-driven/>的理解
- 关于Spring事务<tx:annotation-driven/>的理解(Controller可以使用@Transactional)
- 关于Spring事务<tx:annotation-driven/>的理解(Controller可以使用@Transactional)
- 关于Spring事务<tx:annotation-driven/>的理解(Controller可以使用@Transactional)
- 关于Spring事务<tx:annotation-driven/>的理解(Controller可以使用@Transactional)
- 已解决:"通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明"等
- spring配置事务 元素 "tx:annotation-driven" 的前缀 "tx" 未绑定
- "通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明" 的解决方案
- "通配符的匹配很全面, 但无法找到元素 'tx:annotation-driven' 的声明" 的解决方案
- spring 事务 @Transactional注解参数 tx:annotation-driven
- spring 事务 @Transactional注解参数 tx:annotation-driven
- spring 事务 @Transactional注解参数 tx:annotation-driven
- spring mvc <mvc:annotation-driven>配置使用出现问题
- spring mvc <mvc:annotation-driven>配置使用出现问题
- 成为优秀高级程序员的10个要点
- 自学需静心
- extern ”C"的使用
- android应用程序如何调用支付宝接口
- Android学习(7)-UI布局
- 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题
- oracle 11g 安装
- LeetCode169——Majority Element
- 黑马程序员——Jave之反射
- BZOJ 3601 一个人的数论 莫比乌斯反演+高斯消元
- Matlab中的input命令
- linux和windows之间上传 下载文件 非ftp方式
- JSON 学习
- Virutualbox ubuntu 修改BIOS, MDI info 激活windows xp