SpringMVC+Hibernate4 導致事務失效不提交的可能原因

原文:http://blog.csdn.net/running_snail_/article/details/8888506

1.對於用annotation方式的事務註解和bean配置,spring的配置文件 與springMVC的配置文件對包的重複掃描裝配會照成失效

在主容器中(applicationContext.xml),將Controller的註解排除掉 
<context:component-scan base-package="com"> 
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
</context:component-scan> 

而在springMVC配置文件中將Service註解給去掉 
<context:component-scan base-package="com"> 
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
  </context:component-scan> 

因爲spring的context是父子容器,所以會產生衝突,由ServletContextListener產生的是父容器,springMVC產生的是子容器,子容器Controller進行掃描裝配時裝配了@Service註解的實例,而該實例理應由父容器進行初始化以保證事務的增強處理,所以此時得到的將是原樣的Service(沒有經過事務加強處理,故而沒有事務處理能力。)


2.Hibernate4中session開啓的方式sessionFactory.openSession()導致,應該用getCurrentSession()開啓

具體區別請參考

Hibernate4之getCurrentSession和openSession



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章