복붙노트

[SPRING] Spring / Hibernate 예외 : createCriteria가 활성 트랜잭션없이 유효하지 않습니다.

SPRING

Spring / Hibernate 예외 : createCriteria가 활성 트랜잭션없이 유효하지 않습니다.

나는 봄 - 최대 절전 모드 트랜잭션 문제로 며칠을 보냈다. jaxws + spring + hibernate를 사용하여 간단한 웹 서비스를 만들고, 잘 작동하지만 트랜잭션 bean 스프링을 사용하는 웹 메서드를 호출하면 다음 오류가 발생합니다.

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

거래가 시작된 것 같지만 뭔가 잘못 됐어.

[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

여기 내 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>

그리고 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>

나는이 webapp을 tomcat6에 배치했다. 그 문제를 해결하도록 도와 주시면 좋겠습니다.

건배

해결법

  1. ==============================

    1.문제가 발견되었습니다. 최대 절전 모드 구성에서이 줄을 제거했습니다. Spring은 트랜잭션을 관리하며, 세션이 최대 절전 스레드로 유지 될 필요가 없다.

    문제가 발견되었습니다. 최대 절전 모드 구성에서이 줄을 제거했습니다. Spring은 트랜잭션을 관리하며, 세션이 최대 절전 스레드로 유지 될 필요가 없다.

    <!-- Bind the getCurrentSession() method to the thread. -->
    <property name="current_session_context_class">thread</property>
    
  2. from https://stackoverflow.com/questions/7500030/spring-hibernate-exception-createcriteria-is-not-valid-without-active-transacti by cc-by-sa and MIT license