Spring / Hibernate异常:没有活动事务,createCriteria无效 [英] Spring/Hibernate Exception: createCriteria is not valid without active transaction
问题描述
21 sept。 2011 14:29:29 com.sun.xml.ws.server.sei.EndpointMethodHandler调用
GRAVE:org.hibernate.HibernateException:如果没有活动事务,createCriteria无效
我似乎已经开始交易......但发生了一些错误。
[jmedia] 21 sept。 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 使用名称[com.cellfish.mediadb.ws.encoder.MediaDBFeeds.testTransaction]创建新事务:PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly ; ''
[jmedia] 21 sept。 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 为Hibernate事务
[jmedia] 21 sept打开新的会话[org.hibernate.impl.SessionImpl@26b20a31] 。 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 准备Hibernate Session的JDBC连接[org.hibernate.impl.SessionImpl@26b20a31]
[jmedia] 21 sept 。 2011年11月29日14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 将Hibernate事务公开为JDBC事务[jdbc:mysql:// xxxxxxxx,MySQL-AB JDBC Driver]
[ jmedia] 21 sept。 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 找到Hibernate事务的线程绑定Session [org.hibernate.impl.SessionImpl@26b20a31]
[jmedia] 21日2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 参与现有事务
[jmedia] 21 sept。 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 参与事务失败 - 将现有事务标记为仅回滚
[jmedia] 21 sept。 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 在Session [org.hibernate.impl.SessionImpl@26b20a31]上设置Hibernate事务仅回滚
[jmedia] 21日2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 启动事务回滚
[jmedia] 21 sept。 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 回滚Session上的Hibernate事务[org.hibernate.impl.SessionImpl@26b20a31]
[jmedia] 21 sept 。 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - 在事务
21 sept之后关闭Hibernate Session [org.hibernate.impl.SessionImpl@26b20a31]。 2011 14:29:29 com.sun.xml.ws.server.sei.EndpointMethodHandler调用
这里我的applicationContext:
< context:annotation-config />
<! - 由Spring管理的包列表 - >
< context:component-scan base-package =.../>
< bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// xxxx/>
< property name =usernamevalue =xxx/>
< property name =passwordvalue =xxx/>
< / bean>
< bean id =sessionFactory
class =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =packagesToScanvalue =xxxx/>
< property name =dataSourceref =dataSource/>
< property name =configLocationvalue =classpath:hibernate.cfg.xml/>
< / bean>
< tx:注解驱动的事务管理器=transactionManager/>
< bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>
和hibernate.cfg.xml:
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate Configuration DTD // EN
http://www.hibernate.org/dtd/hibernate-configuration- 3.0.dtd>
< hibernate-configuration>
<! - 一个SessionFactory实例被列为/ jndi / name - >
< session-factory>
< property name =dialect> org.hibernate.dialect.MySQLInnoDBDialect< / property>
< property name =show_sql> true< / property>
< property name =hibernate.format_sql> true< / property>
< property name =hibernate.connection.pool_size> 1< / property>
< property name =hibernate.jdbc.batch_size> 20< / property>
<! - 将getCurrentSession()方法绑定到线程。 - >
< property name =current_session_context_class>线程< / property>
< property name =hibernate.cache.provider_class> net.sf.ehcache.hibernate.SingletonEhCacheProvider< / property>
< property name =hibernate.cache.use_second_level_cache> true< / property>
< property name =hibernate.cache.use_query_cache> true< / property>
<! - Lucene Search - >
< property name =hibernate.search.default.directory_provider> org.hibernate.search.store.RAMDirectoryProvider< / property>
< / session-factory>
我在tomcat6上部署了这个webapp 。
我希望你能帮我解决这个问题。
干杯
解决方案好的,我发现问题了!我从休眠配置中删除了这一行。
Spring管理事务,并且它不需要会话持久化在一个hibernate线程中。<! - 将getCurrentSession()方法绑定到线程。 - >
< property name =current_session_context_class>线程< / property>
I spent few days on an spring-hibernate-transaction issue. I create a simple webservice with jaxws + spring + hibernate, it works fine but when I call a web methode which use a transactional bean spring threw the following error:
21 sept. 2011 14:29:29 com.sun.xml.ws.server.sei.EndpointMethodHandler invoke GRAVE: org.hibernate.HibernateException: createCriteria is not valid without active transaction
I seems the transaction has been started ... but something wrong happened.
[jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Creating new transaction with name [com.cellfish.mediadb.ws.encoder.MediaDBFeeds.testTransaction]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '' [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Opened new Session [org.hibernate.impl.SessionImpl@26b20a31] for Hibernate transaction [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@26b20a31] [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Exposing Hibernate transaction as JDBC transaction [jdbc:mysql://xxxxxxxx, MySQL-AB JDBC Driver] [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Found thread-bound Session [org.hibernate.impl.SessionImpl@26b20a31] for Hibernate transaction [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Participating in existing transaction [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Participating transaction failed - marking existing transaction as rollback-only [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Setting Hibernate transaction on Session [org.hibernate.impl.SessionImpl@26b20a31] rollback-only [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Initiating transaction rollback [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@26b20a31] [jmedia] 21 sept. 2011 14:29:29 [http-8080-1] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Closing Hibernate Session [org.hibernate.impl.SessionImpl@26b20a31] after transaction 21 sept. 2011 14:29:29 com.sun.xml.ws.server.sei.EndpointMethodHandler invoke
Here my applicationContext :
<context:annotation-config/> <!-- List of packages managed by Spring --> <context:component-scan base-package="..." /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://xxxx"/> <property name="username" value="xxx"/> <property name="password" value="xxx"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="packagesToScan" value="xxxx"/> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
And the hibernate.cfg.xml :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- a SessionFactory instance listed as /jndi/name --> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <property name="show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.connection.pool_size">1</property> <property name="hibernate.jdbc.batch_size">20</property> <!-- Bind the getCurrentSession() method to the thread. --> <property name="current_session_context_class">thread</property> <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.use_query_cache">true</property> <!-- Lucene Search --> <property name="hibernate.search.default.directory_provider">org.hibernate.search.store.RAMDirectoryProvider</property> </session-factory>
I deployed this webapp on tomcat6. I hope can you help me to resolve that issue.
Cheers
解决方案Ok I found the problem ! I removed this line from the hibernate configuration. Spring manages the transaction, and it doesn't need of that the session is holding in a hibernate thread.
<!-- Bind the getCurrentSession() method to the thread. --> <property name="current_session_context_class">thread</property>
这篇关于Spring / Hibernate异常:没有活动事务,createCriteria无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!