복붙노트

[SPRING] current_session_context_class 등록 정보 사용 최대 절전 모드 3 최대 절전 모드 4

SPRING

current_session_context_class 등록 정보 사용 최대 절전 모드 3 최대 절전 모드 4

프로덕션 환경에서 잘 작동하는 Spring과 Hibernate3에 대한 어플리케이션이 있습니다. 다음은 Spring의 applicationContext.xml에있는 세션 팩토리에 대한 설정이다.

       <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingDirectoryLocations">
        <list>
            <value>classpath:/hibernate</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
            <prop key="hibernate.max_fetch_depth">2</prop>
            <prop key="hibernate.autocommit">false</prop>
            <prop key="hibernate.current_session_context_class ">thread</prop>
                            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.jdbc.batch_size">20</prop>
        </props>
    </property>
</bean>

<bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean 
    below) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
        <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="count*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="validate*" propagation="SUPPORTS"
            read-only="true" />
        <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="login" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
</tx:advice>

<!-- ensure that the above transactional advice runs for any execution of 
    an operation defined by the service interfaces -->
<aop:config>
    <aop:pointcut id="projectServiceOperation"
        expression="execution(* com.service.project.IProjectService.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="projectServiceOperation" />
</aop:config>

그것은 생산에서 잘 작동합니다.

이제 다른 프로젝트를 위해 우리는 Hibernate4로 마이그레이션 중이다. 우리는 Hierrnate 4의 SessionFactory, TransacionManager 등을 사용하는 것을 제외하고는 동일한 구성을 복사했습니다. org.springframework.orm.hibernate4. * 패키지. 그러나 "저장은 활성 트랜잭션없이 유효하지 않습니다"라는 예외를 제공하기 시작했습니다. 조금만 검색 한 후 많은 사람들이 문제에 직면 한 것 같았고 몇몇 사람들은 사용하지 말라고 제안했습니다.

        <prop key="hibernate.current_session_context_class ">thread</prop>

재산과 그것이 효과. 그것은 또한 나를 위해 일했습니다. 게시물에서 수집 할 수있는 모든 정보는 컨텍스트 세션 및 스레드 전략과 관련이 있으며 Spring의 세션 관리 전략을 방해합니다. 그러나 구체적인 답을 찾을 수있는 곳은 없습니다.             또한 Hibernate3에서는 작동하고 Hibernate4에서는 작동하지 않는 이유는 무엇입니까? 차이점은 무엇이며 변경된 사항은 무엇입니까? 다른 모든 구성은 동일합니다. @Transactional이 아니라 오래된 학교 XML 방식을 사용하고 있습니다.

    Can somebody point me to clear explaination for this behavioural difference in Hibernate3 and Hibernate4?  

해결법

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

    1.그것은 Spring의 버전에 달려 있지만 일반적으로 Spring을 사용하는 동안 그 속성을 사용하는 것은 피해야합니다 (JTA를 트랜잭션에 사용하지 않으면 이것을 구성해야합니다).

    그것은 Spring의 버전에 달려 있지만 일반적으로 Spring을 사용하는 동안 그 속성을 사용하는 것은 피해야합니다 (JTA를 트랜잭션에 사용하지 않으면 이것을 구성해야합니다).

    Hibernate 3.1에서 문맥 세션 (contextual sessions)이라고 불리는 것이 있으며,이를 위해 Hibernate는 CurrentSessionContext 인터페이스를 제공한다. 이것에 대한 구현이 몇개 있습니다 (thread는 ThreadLocalSessionContext의 생략 형입니다). Spring은 SpringSessionContext 클래스의이 인터페이스를 독자적으로 구현한다.

    Spring은 기본적으로 CurrentSessionContext의 Springs 구현으로 속성을 설정합니다.이 설정이 다른 것 (JTA가 아님)으로 설정되어 있으면 최대 절전 모드 세션 (따라서 트랜잭션)을 관리하는 스프링 기능이 중단됩니다.

    Spring의 이전 버전에서는 (최대 절전 모드를 사용하기 위해 Spring을 업그레이드했다는 가정하에) 최대 절전 모드 3과 결합하여 세션을 가져 오는 것과 관련된 다른 트릭이 발생했습니다 (이전 버전 3.x 버전의 최대 절전 모드와의 하위 호환성으로 인해). 이로 인해 Spring은 해당 속성의 가치에 덜 의존하게되었습니다. (Spring은 SessionFactory를위한 프록시를 생성하고 기본적으로 세션을 관리 할 수있는 getSession 메소드를 가로 챘다.)

  2. from https://stackoverflow.com/questions/19875485/using-current-session-context-class-property-hibernate-3-hibernate-4 by cc-by-sa and MIT license