`

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

阅读更多

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>

 

 

 

 

 

 

 

 

分享到:
评论
11 楼 fs08ab 2015-07-24  
itstarting 写道
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.

itstarting大神和fengzhiyin博主好,我有一事不明,两位在问题分析中的描述,我理解为是:在tm注册之前,spring扫描了@Service标注的类。我的第一反应是,将@Service延迟加载,但为什么最终的解决方法是将@Controller延迟加载了呢
10 楼 fs08ab 2015-07-24  
大神和博主好,我有一事不明,两位在问题分析中的描述,我理解为是:在tm注册之前,spring扫描了@Service标注的类。我的第一反应是,将@Service延迟加载,但为什么最终的解决方法是将@Controller延迟加载了呢
9 楼 osacar 2011-05-19  
也遇到了这样的问题。郁闷中。。。。
8 楼 JuJuo 2011-05-12  
没认真看 itstarting 的回复!
改了 之后!

@Controller
@Service
声明式事务 是 可以的
7 楼 JuJuo 2011-05-12  
muqingren 写道
这个问题困扰死我了
我事务用的是xml配置的声明事务
MVC用的是annotation
现在冲突了,我真的不知道怎么解决了........

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

我的是
MVC 用annotation ,
service 用@Servcie,
事务用声明事务, 这样事务失败!
6 楼 muqingren 2011-03-21  
这个问题困扰死我了
我事务用的是xml配置的声明事务
MVC用的是annotation
现在冲突了,我真的不知道怎么解决了........
5 楼 itstarting 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.
4 楼 jacktea 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>


3 楼 yangwn 2010-08-02  
请LZ说的清除一些可以吗?
这个问题,我现在也遇到了,很是郁闷~~
2 楼 乐之者耶 2010-07-19  
我也遇到这样的问题,郁闷了好几天了

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

这个是如何处理? 可否写清楚一点?
1 楼 icanfly 2010-07-17  
这样好像能插入,但是事务不回滚?

相关推荐

Global site tag (gtag.js) - Google Analytics