论坛首页 Java企业应用论坛

spring + hibernate 申明式事务不起作用

浏览 13825 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-04-18   最后修改:2012-04-18
一个基于spring springmvc hibernate的框架,在调用getSession().save(entity)时,对象没有更新到数据库中,也没有任何错误信息,判定是事务没有提交。
Service代码
@Transactional(readOnly = true)
public class UserManager {

	private UserDao userDao;

	@Transactional(readOnly = false)
	public void saveUser(User entity) {
		
		userDao.save(entity);
	}
	.....
	}

applicationContext配置文件
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close" lazy-init="false">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- Connection Pooling Info -->
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="defaultAutoCommit" value="false" />
		<property name="timeBetweenEvictionRunsMillis" value="3600000" />
		<property name="minEvictableIdleTimeMillis" value="3600000" />
	</bean>

	
	<!-- 事务管理器配置,单数据源事务 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 使用annotation定义事务 -->
	<context:annotation-config />
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />


不知道为什么事务不起作用
   发表时间:2012-04-18  
把相应的debug信息发上来
0 请登录后投票
   发表时间:2012-04-18  
是不是没有在 UserManager类上@Service 注解
0 请登录后投票
   发表时间:2012-04-18  
yxb1990 写道
是不是没有在 UserManager类上@Service 注解

加了的,后来发现好像是OpenSessionInViewFilter配置的问题,开始配的是

<!-- Hibernate Open Session In View filter-->
	<filter>
		<filter-name>hibernateOpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>hibernateOpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

后来改成

<!-- Hibernate Open Session In View filter-->
	<filter>
		<filter-name>hibernateOpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>hibernateOpenSessionInViewFilter</filter-name>
		<url-pattern>*do</url-pattern>
	</filter-mapping>

运行时可以看到生成的insert语句了,但不知道为什么跑完后值还是没进到数据库中。
0 请登录后投票
   发表时间:2012-04-18   最后修改:2012-04-18
怀疑你springMVC 部分配置错了,请在SpringMVC的配置文件中 将@Service、@Repository排除。

可以参考 最新SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结  进行排除
0 请登录后投票
   发表时间:2012-04-18  
这个还真没遇到过,sql语句都出来了,不应该啊。你在log4j.properties文件加上log4j.logger.org.springframework.transaction=DEBUG
看下debug信息,看看事务有没有提交
0 请登录后投票
   发表时间:2012-04-18  
yxb1990 写道
这个还真没遇到过,sql语句都出来了,不应该啊。你在log4j.properties文件加上log4j.logger.org.springframework.transaction=DEBUG
看下debug信息,看看事务有没有提交

没有看到事务的提交
DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter                           
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'sessionFactory'                              
DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Opening single Hibernate Session in OpenSessionInViewFilter                                 
DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session                                                                               
DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 5467163753476096                                                                               
DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'springServlet' processing GET request for [/zlzk-ex/open/save.do]               
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Looking up handler method for path /open/save.do                            
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Returning handler method [public java.lang.String com.zoomlion.examples.miniweb.web.account.OpenController.list(java.lang.Integer,org.springframework.ui.Model,javax.servlet.http.HttpServletRequest)]
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'openController'                              
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Last-Modified value for [/zlzk-ex/open/save.do] is: -1                                                       
DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - executing identity-insert immediately                                                                        
DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)                                                   
DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection                                                                               
DEBUG [org.hibernate.SQL] - insert into acct_user (email, login_name, name, password, salt) values (?, ?, ?, ?, ?)                                                       
l, login_name, name, password, salt) values (?, ?, ?, ?, ?)                                                                               
DEBUG [org.hibernate.id.IdentifierGeneratorHelper] - Natively generated identity: 20                                                                               
DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)                                                  
                                                                               
INFO  [com.zoomlion.examples.miniweb.dao.account.UserDao] - save entity: com.zoomlion.examples.miniweb.entity.account.User@fb77dd[loginName=kimi,password=abc,salt=1111,name=kimimo,email=ben@springside.org.cn,groupList=[],id=20]
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Invoking afterPropertiesSet() on bean with name 'redirect:/login'                         
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Rendering view [org.springframework.web.servlet.view.RedirectView: name 'redirect:/login'; URL [/login]] in DispatcherServlet with name 'springServlet'
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request                                                                               
DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Closing single Hibernate Session in OpenSessionInViewFilter                                 
DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session                                                                               
DEBUG [org.hibernate.jdbc.ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]                  
DEBUG [org.hibernate.jdbc.ConnectionManager] - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'springServlet' processing GET request for [/zlzk-ex/login]                      
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Looking up handler method for path /login                                   
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Returning handler method [public java.lang.String com.zoomlion.examples.miniweb.web.LoginController.login()]
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'loginController'                             
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Last-Modified value for [/zlzk-ex/login] is: -1                                                              
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Invoking afterPropertiesSet() on bean with name 'login'                                   
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Rendering view [org.springframework.web.servlet.view.JstlView: name 'login'; URL [/WEB-INF/views/login.jsp]] in DispatcherServlet with name 'springServlet'
DEBUG [org.springframework.web.servlet.view.JstlView] - Forwarding to resource [/WEB-INF/views/login.jsp] in InternalResourceView 'login'                                
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request                                                                               


0 请登录后投票
   发表时间:2012-04-18  
kimimo 写道
yxb1990 写道
这个还真没遇到过,sql语句都出来了,不应该啊。你在log4j.properties文件加上log4j.logger.org.springframework.transaction=DEBUG
看下debug信息,看看事务有没有提交

没有看到事务的提交
DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter                           
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'sessionFactory'                              
DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Opening single Hibernate Session in OpenSessionInViewFilter                                 
DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session                                                                               
DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 5467163753476096                                                                               
DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'springServlet' processing GET request for [/zlzk-ex/open/save.do]               
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Looking up handler method for path /open/save.do                            
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Returning handler method [public java.lang.String com.zoomlion.examples.miniweb.web.account.OpenController.list(java.lang.Integer,org.springframework.ui.Model,javax.servlet.http.HttpServletRequest)]
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'openController'                              
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Last-Modified value for [/zlzk-ex/open/save.do] is: -1                                                       
DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - executing identity-insert immediately                                                                        
DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)                                                   
DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection                                                                               
DEBUG [org.hibernate.SQL] - insert into acct_user (email, login_name, name, password, salt) values (?, ?, ?, ?, ?)                                                       
l, login_name, name, password, salt) values (?, ?, ?, ?, ?)                                                                               
DEBUG [org.hibernate.id.IdentifierGeneratorHelper] - Natively generated identity: 20                                                                               
DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)                                                  
                                                                               
INFO  [com.zoomlion.examples.miniweb.dao.account.UserDao] - save entity: com.zoomlion.examples.miniweb.entity.account.User@fb77dd[loginName=kimi,password=abc,salt=1111,name=kimimo,email=ben@springside.org.cn,groupList=[],id=20]
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Invoking afterPropertiesSet() on bean with name 'redirect:/login'                         
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Rendering view [org.springframework.web.servlet.view.RedirectView: name 'redirect:/login'; URL [/login]] in DispatcherServlet with name 'springServlet'
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request                                                                               
DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Closing single Hibernate Session in OpenSessionInViewFilter                                 
DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session                                                                               
DEBUG [org.hibernate.jdbc.ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]                  
DEBUG [org.hibernate.jdbc.ConnectionManager] - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'springServlet' processing GET request for [/zlzk-ex/login]                      
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Looking up handler method for path /login                                   
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Returning handler method [public java.lang.String com.zoomlion.examples.miniweb.web.LoginController.login()]
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'loginController'                             
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Last-Modified value for [/zlzk-ex/login] is: -1                                                              
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Invoking afterPropertiesSet() on bean with name 'login'                                   
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Rendering view [org.springframework.web.servlet.view.JstlView: name 'login'; URL [/WEB-INF/views/login.jsp]] in DispatcherServlet with name 'springServlet'
DEBUG [org.springframework.web.servlet.view.JstlView] - Forwarding to resource [/WEB-INF/views/login.jsp] in InternalResourceView 'login'                                
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request                                                                               




怀疑你springMVC 部分配置错了,请在SpringMVC的配置文件中 将@Service、@Repository排除。 
可以参考 最新SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结  进行排除

 

在**-servlet.xml中

 

  1. <context:component-scan base-package="cn.javass.demo.web.controller">  
  2.     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
0 请登录后投票
   发表时间:2012-04-18  
jinnianshilongnian 写道
kimimo 写道
yxb1990 写道
这个还真没遇到过,sql语句都出来了,不应该啊。你在log4j.properties文件加上log4j.logger.org.springframework.transaction=DEBUG
看下debug信息,看看事务有没有提交

没有看到事务的提交
DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter                           
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'sessionFactory'                              
DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Opening single Hibernate Session in OpenSessionInViewFilter                                 
DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session                                                                               
DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 5467163753476096                                                                               
DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'springServlet' processing GET request for [/zlzk-ex/open/save.do]               
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Looking up handler method for path /open/save.do                            
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Returning handler method [public java.lang.String com.zoomlion.examples.miniweb.web.account.OpenController.list(java.lang.Integer,org.springframework.ui.Model,javax.servlet.http.HttpServletRequest)]
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'openController'                              
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Last-Modified value for [/zlzk-ex/open/save.do] is: -1                                                       
DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - executing identity-insert immediately                                                                        
DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)                                                   
DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection                                                                               
DEBUG [org.hibernate.SQL] - insert into acct_user (email, login_name, name, password, salt) values (?, ?, ?, ?, ?)                                                       
l, login_name, name, password, salt) values (?, ?, ?, ?, ?)                                                                               
DEBUG [org.hibernate.id.IdentifierGeneratorHelper] - Natively generated identity: 20                                                                               
DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)                                                  
                                                                               
INFO  [com.zoomlion.examples.miniweb.dao.account.UserDao] - save entity: com.zoomlion.examples.miniweb.entity.account.User@fb77dd[loginName=kimi,password=abc,salt=1111,name=kimimo,email=ben@springside.org.cn,groupList=[],id=20]
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Invoking afterPropertiesSet() on bean with name 'redirect:/login'                         
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Rendering view [org.springframework.web.servlet.view.RedirectView: name 'redirect:/login'; URL [/login]] in DispatcherServlet with name 'springServlet'
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request                                                                               
DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Closing single Hibernate Session in OpenSessionInViewFilter                                 
DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session                                                                               
DEBUG [org.hibernate.jdbc.ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]                  
DEBUG [org.hibernate.jdbc.ConnectionManager] - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'springServlet' processing GET request for [/zlzk-ex/login]                      
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Looking up handler method for path /login                                   
DEBUG [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] - Returning handler method [public java.lang.String com.zoomlion.examples.miniweb.web.LoginController.login()]
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'loginController'                             
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Last-Modified value for [/zlzk-ex/login] is: -1                                                              
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Invoking afterPropertiesSet() on bean with name 'login'                                   
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Rendering view [org.springframework.web.servlet.view.JstlView: name 'login'; URL [/WEB-INF/views/login.jsp]] in DispatcherServlet with name 'springServlet'
DEBUG [org.springframework.web.servlet.view.JstlView] - Forwarding to resource [/WEB-INF/views/login.jsp] in InternalResourceView 'login'                                
DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request                                                                               




怀疑你springMVC 部分配置错了,请在SpringMVC的配置文件中 将@Service、@Repository排除。 
可以参考 最新SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结  进行排除

 

在**-servlet.xml中

 

 

  1. <context:component-scan base-package="cn.javass.demo.web.controller">  
  2.     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  

改了,事务还是没起作用

0 请登录后投票
   发表时间:2012-04-18  
你怎么改的 贴上来 看看
0 请登录后投票
论坛首页 Java企业应用版

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