论坛首页 Java企业应用论坛

解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题

浏览 12054 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (8) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-07-16   最后修改:2010-07-16

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>

 

 

 

 

 

 

 

 

   发表时间:2010-07-17  
这样好像能插入,但是事务不回滚?
0 请登录后投票
   发表时间:2010-07-19  
我也遇到这样的问题,郁闷了好几天了

将扫描其他目录的context:component放入application.xml文件中(该文件为)org.springframework.web.context.ContextLoaderListener的加载文件。

这个是如何处理? 可否写清楚一点?
0 请登录后投票
   发表时间:2010-08-02  
请LZ说的清除一些可以吗?
这个问题,我现在也遇到了,很是郁闷~~
0 请登录后投票
   发表时间:2010-11-28   最后修改:2010-11-28
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 请登录后投票
   发表时间:2010-12-01  
jacktea 写道
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.
1 请登录后投票
   发表时间:2011-03-21  
这个问题困扰死我了
我事务用的是xml配置的声明事务
MVC用的是annotation
现在冲突了,我真的不知道怎么解决了........
0 请登录后投票
   发表时间:2011-05-12  
muqingren 写道
这个问题困扰死我了
我事务用的是xml配置的声明事务
MVC用的是annotation
现在冲突了,我真的不知道怎么解决了........

Spring MVC 用annotation
事务用的是xml配置的声明事务 可以的。

我的是
MVC 用annotation ,
service 用@Servcie,
事务用声明事务, 这样事务失败!
0 请登录后投票
   发表时间:2011-05-12  
没认真看 itstarting 的回复!
改了 之后!

@Controller
@Service
声明式事务 是 可以的
0 请登录后投票
   发表时间:2011-05-19  
也遇到了这样的问题。郁闷中。。。。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics