Spring and Hibernate’s getCurrentSession() ErrorS!
2010-11-21 21:35
471 查看
If you are using Spring to wrap a Hibernate
The solution to this is to set the property
<property name="exposeTransactionAwareSessionFactory"><value>false</value></property>
in the Spring config. This will return the “raw”
[/code]
A sample Spring config is shown below.
SessionFactoryand you are not using Spring-managed transactions, you may run into an issue. The reason is that Spring by default will wrap Hibernate’s
SessionFactoryimplementation and delegate to its own transactional version. If you are just using the simple
ThreadLocal-based session-per-request functionality, then when you attempt to open the
Session, you will get an
IllegalStateExceptionthrown, with the error message
"No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here". This happens because Spring’s
SessionFactoryUtilschecks if the Session is bound to Spring’s transactional support, and by default throws an error if it is not.
The solution to this is to set the property
<property name="exposeTransactionAwareSessionFactory"><value>false</value></property>
in the Spring config. This will return the “raw”
SessionFactoryinstead of the proxied one. A snippet of code from
AbstractSessionFactoryBeanshows where the check is done:
[code] /** * Wrap the given SessionFactory with a transaction-aware proxy, if demanded. * @param rawSf the raw SessionFactory as built by <code>buildSessionFactory()</code> * @return the SessionFactory reference to expose * @see #buildSessionFactory() * @see #getTransactionAwareSessionFactoryProxy */ protected SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf) { if (isExposeTransactionAwareSessionFactory()) { return getTransactionAwareSessionFactoryProxy(rawSf); } else { return rawSf; } }
[/code]
A sample Spring config is shown below.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/> <property name="username" value="sa"/> <property name="password" value=""/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property [b]name="exposeTransactionAwareSessionFactory"><value>false</value></property>[/b] <property name="annotatedPackages"> <list> <value>uk.co.researchkitchen.hibernate</value> </list> </property> <property name="annotatedClasses"> <list> <value>uk.co.researchkitchen.hibernate.Product</value> <value>uk.co.researchkitchen.hibernate.ProductDescription</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.HSQLDialect hibernate.show_sql=true hibernate.hbm2ddl.auto=create hibernate.current_session_context_class=thread </value> </property> </bean> </beans>
http://www.theresearchkitchen.com/blog/archives/73
相关文章推荐
- 由openSession、getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持
- 解决spring整合hibernate后不能用getCurrentSession()的问题
- Spring3与hibernate4整合,sessionFactory.getCurrentSession()方法获取不到Session对象异常解决办法
- 由openSession、getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持
- 关于spring+hibernate中的OpenSession()和getCurrentSession()的区别
- spring mvc ioc getCurrentSession出错: HibernateException: No Session found
- hibernate openSession and getCurrentSEssion
- spring管理hibernate4 transaction getCurrentSession为什么报错?
- 【转】Spring与Hibernate集成中的session问题(getSession()、getCurrentSession()与openSession() )
- 【转】Spring与Hibernate集成中的session问题(getSession()、getCurrentSession()与openSession() )
- Hibernate整合进spring--使用hibernateTemplate.getSessionFactory().getCurrentSession()理解
- spring和hibernate整合使用getCurrentSession方法
- Hibernate整合进spring--使用hibernateTemplate.getSessionFactory().getCurrentSession()理解
- spring+Hibernate openSession()和getCurrentSession()
- Hibernate getCurrentSession 与 openSession() 的区别
- hibernate openSession() 与 getCurrentSession()
- Hibernate关于Session管理的openSession和getCurrentSession的理解
- 【Hibernate】SessionFactory中的getCurrentSession()和openSession()的区别
- hibernate openSession和getCurrentSession
- Hibernate 中的 openSession和getCurrentSession 方法的区别