Spring + Hibernate 4 + Glassfish 사용 JTA Transaction

오늘 오후 부터 프로젝트 의 transaction 을 Glassfish 에 맡 기 는 JTA 관 리 를 시도 합 니 다. 이후 JMS 에 사 용 될 예정 이 므 로 JDBC 와 크로스 데이터 소스 를 구성 해 야 합 니 다.그러나 아무 도 이렇게 해 본 적 이 없 는 지, 아니면 완전한 설정 과정 을 내 려 놓 을 가치 가 없 는 지, JBoss 의 공식 문서, Spring 의 공식 문서, SOF 는 사용 가능 한 설정 제안 이 없다.Google 과 시 도 를 통 해 반나절 이 채 지나 지 않 아 설정 이 성공 적 으로 완료 되 었 습 니 다.
  • Spring 3.1.1
  • Hibernate 4.1.1
  • Glassfish 3.1.2

  • 모두 최신 버 전 일 것 입 니 다. Spring 프로필 은 다음 과 같 습 니 다.
     
    
      
      
      
      
       
       
       
       
    02
    <tx:annotation-driven /><!--   Spring @Transaction        -->
    03
    <tx:jta-transaction-manager /><!--   Spring  JTA   -->

    04
     

    05
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    06
    	...

    07
    	<property name="hibernateProperties">

    08
    		<value>

    09
    	

    10
    hibernate.current_session_context_class=jta<!-- 1 -->

    11
    hibernate.transaction.manager_lookup_class
    =org.hibernate.transaction.SunONETransactionManagerLookup
    12
    		</value>

    13
    	</property>

    14
    </bean>

    15
    ...

    키 설정 은 1 과 2 에 있 습 니 다: Hibernate 공식 문서 와 여기 설명 에 따 르 면:
    When configuring Hibernate’s transaction factory, chooseorg.hibernate.transaction.JTATransactionFactory if you use JTA directly (BMT), and org.hibernate.transaction.CMTTransactionFactory in a CMT session bean. Remember to also set hibernate.transaction.manager_lookup_class. Ensure that your hibernate.current_session_context_class is either unset (backwards compatibility), or is set to "jta".
    ⋯⋯
    See the Javadocs for the org.hibernate.context.spi.CurrentSessionContext interface for a detailed discussion of its contract. It defines a single method, currentSession(), by which the implementation is responsible for tracking the current contextual session. Out-of-the-box, Hibernate comes with three implementations of this interface:
  • org.hibernate.context.internal.JTASessionContext: current sessions are tracked and scoped by a JTAtransaction. The processing here is exactly the same as in the older JTA-only approach. See the Javadocs for details.
  • org.hibernate.context.internal.ThreadLocalSessionContext:current sessions are tracked by thread of execution. See the Javadocs for details.
  • org.hibernate.context.internal.ManagedSessionContext: current sessions are tracked by thread of execution. However, you are responsible to bind and unbind a Session instance with static methods on this class: it does not open, flush, or close a Session.

  • hibenate. current 를 설정 해 야 합 니 다.session_context_class 는 jta 이 며, 동시에 hibenate. transaction. manager 를 설정 합 니 다.lookup_class。그런데 여기 서 한참 을 지체 한 게 바로 히 베 네 이 트. transaction. manager 입 니 다.lookup_class 의 설정 은 "설정 이 필요 합 니 다" 라 고 만 말 하지만 구체 적 인 값 은 제시 되 지 않 았 습 니 다. 문서 에 Transaction Manager Lookup 인터페이스의 실현 클래스 목록 이 있 지만 Glassfish 도 없습니다. N 오래된 구 글 을 거 쳐 여기 서 신기 한 실현 클래스 를 찾 았 습 니 다. org. hibenate. transaction. sunonetransaction. sunonetransaction Manager Lookup.Javadoc 에 따 르 면 'for Sun ONE Application Server 7 and above' 의 검색 전략 을 실 현 했 습 니 다. GF 의 전신 이 Sun AS 라 고 생각해 보 세 요. 사용 할 수 있 을 것 같 습 니 다.해 보 니 워 크 ~ 여기 남 겨 진 문 제 는 하 이 버 네 이 트 4.1.1 소스 코드 에서 org. hibenate. transaction. sunonetransaction. sunonetransaction Manager Lookup (위의 자바 doc 도 3.6 의 자바 doc) 을 찾 지 못 했 고 심지어 전체 org. hibenate. transaction 패키지 아래 에는 Transaction Manager Lookup 인터페이스 만 정의 되 어 있 습 니 다.그래서 도대체 어떻게 작 동 하 는 지 저 는 최종 답 을 찾 지 못 했 습 니 다. 만약 에 아 는 사람 이 있다 면 여기 서 답 을 남 겼 으 면 좋 겠 습 니 다. 2012 - 4 - 16 보: 오늘 서버 를 시작 할 때 우연히 warning, hibenate. transaction. manager 를 던 진 것 을 발 견 했 습 니 다.lookup_class 는 Hibernate 4 에서 deprecated 되 었 습 니 다.
       
    
      
      
      
      
    
      
      
      
      
       
       
       
       
    1
    hibernate.transaction.jta.platform=
    org. hibernate. service. jta. platform. internal. sunOne Jta Platform 을 사용 해 야 합 니 다.
    2012 - 4 - 17 보충: 오늘 Hibernate 의 자동 flush 가 실 효 된 것 을 발 견 했 습 니 다. GF 시작 로 그 를 보고 새로운 warning 을 발 견 했 습 니 다.
    JTASessionContext being used with JDBCTransactionFactory; auto-flush will not operate correctly with getCurrentSession()
    문 서 를 찾 아 보 니 위의 1, 2 곳 에 TransactionFactory 를 지정 해 야 합 니 다. 그렇지 않 으 면 Hibernate 는 JDBCtransactionFactory:
      
    
      
      
      
      
    
      
      
      
      
       
       
       
       
    1
    hibernate.transaction.factory_class=
    org. hibernate. transaction. JTaTransactionFactory 를 기본적으로 사용 합 니 다.
    문제 해결.이번 경험 역시 app server 가 시 작 될 때 대량의 log 를 출력 하지만 그 중에서 warning 등급 과 이상 의 정 보 를 발견 하 는 통찰력 을 키 워 야 합 니 다. 이렇게 문 제 를 해결 하 는 데 적은 노력 으로 큰 효 과 를 거 둘 것 입 니 다. 2012 - 4 - 22 보충: 오늘 또 문제 가 생 겼 습 니 다. Query. iterate () 방법 을 사용 할 때 Hibernate 는 이상 을 던 집 니 다.
    org.hibernate.HibernateException: proxy handle is no longer valid
    여기 서 의 토론 에 따 르 면 JTA Transaction Factory 를 CMTTransaction Factory 로 바 꾸 었 습 니 다. 문제 해결:
      
    
      
      
      
      
    
      
      
      
      
       
       
       
       
    1
    hibernate.transaction.factory_class=
    org. hibenate. transaction. CMT TransactionFactory

    좋은 웹페이지 즐겨찾기