복붙노트

[SPRING] Spring 트랜잭션과 hibernate.current_session_context_class

SPRING

Spring 트랜잭션과 hibernate.current_session_context_class

나는 Hibernate 4와 Spring Transactions를 사용하는 Spring 3.2 애플리케이션을 가지고있다. 모든 메소드가 훌륭하게 작동했으며 엔티티를 저장하거나 검색하기 위해 데이터베이스에 올바르게 액세스 할 수있었습니다. 그런 다음, 나는 멀티 스레딩을 도입했다. 그리고 각 쓰레드가 db에 접근하고 있었기 때문에 나는 Hibernate로부터 다음과 같은 에러를 얻고 있었다.

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

웹에서 스레드 를 내 Hibernate 구성에 추가해야한다고 읽었지 만 지금은 db에 액세스하려고 할 때마다 다음과 같이됩니다.

org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction

그러나 내 서비스 메소드에는 @Transactional 주석이 달려 있으며 thread 를 추가하기 전에는 모두 잘 작동하고있었습니다.

@Transactional 주석이 달린 메소드가 있지만 트랜잭션이없는 이유는 무엇입니까? 이 문제를 어떻게 해결할 수 있습니까?

다음은 내 Hibernate 구성 (세션 컨텍스트 속성 포함)이다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<!-- Hibernate session factory -->
<bean
    id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
    <property name="dataSource" >
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties" >
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop> 
            <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>  
        </props>
    </property>   
    <property name="annotatedClasses" >
        <list>
            ...
        </list>
    </property> 
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

해결법

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

    1.스프링과 스프링을 사용할 때, JTA를 사용하지 않는 한 결코 hibernate.current_session_context_class 프로퍼티를 사용하지 마십시오.

    스프링과 스프링을 사용할 때, JTA를 사용하지 않는 한 결코 hibernate.current_session_context_class 프로퍼티를 사용하지 마십시오.

    Spring은 기본적으로 자신의 CurrentSessionContext 구현 (SpringSessionContext)을 설정하지만, 직접 설정하는 경우에는 그렇지 않습니다. 기본적으로 적절한 트랜잭션 통합을 중단합니다.

    이 설정을 변경하는 유일한 이유는 JTA가 관리하는 트랜잭션을 사용하고자 할 때마다, 그런 다음이를 JTA와 올바르게 통합하도록 설정해야하기 때문입니다.

  2. from https://stackoverflow.com/questions/18832889/spring-transactions-and-hibernate-current-session-context-class by cc-by-sa and MIT license