解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题
来源:互联网 发布:linux 配置代理服务器 编辑:程序博客网 时间:2024/05/22 07:08
看了半天,也不知道这篇文章在说什么,后来想应该是这位仁兄把<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
放在application.xml
中,而<context:component-scan base-package="com.fengzhiyin" />
放spring-servlet.xml
,要放就要放在一起
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);
可以正常提交插入、更新。确定为事务未提交。
3、问题分析
spring mvc使用注解方式时需要使用
<context:component-scan base-package="com.fengzhiyin" />
方式用来扫描该包以及其子包下的@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中扫描controller的context:component
分成两部分,将扫描其他目录的context:component
放入application.xml
文件中(该文件为)org.springframework.web.context.ContextLoaderListener
的加载文件。
<!-- spring mvc 配置文件 --> <context:component-scan base-package="com.fengzhiyin.controller" />
和
<!-- spring context配置文件 --> <context:component-scan base-package="com.fengzhiyin."> <context:exclude-filter type="regex" expression=".*Controller$" /> </context:component-scan>
- 解决 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>配置使用出现问题
- Android系统源代码情景分析笔记00
- oc作用域-点语法
- 线程的生命周期
- 设计原则理解
- 左倾与右倾的中美差别
- 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题
- 01_电话拨号器工程
- Java基础总结(二)——方法调用
- 表单提交数据get请求和post请求的区别?
- PHP require在Linux下的路径
- 博客搬家
- 对Memcached使用的总结和使用场景
- eclipse配置jmeter源码开发环境
- 嵌入式系统原理与接口技术