복붙노트

[SPRING] 최대 절전 모드 세션 및 트랜잭션 관리와 Spring을 통합하는 방법은 무엇입니까?

SPRING

최대 절전 모드 세션 및 트랜잭션 관리와 Spring을 통합하는 방법은 무엇입니까?

저는 동면과 봄 모두 초보자입니다. 나는 최대 절전 모드 트랜잭션 경계 (적어도 나는 그렇게 생각한다)에 대해 이해했다. 그러나 다음과 같은 몇 가지 메소드를 코딩 한 후에 :

sessionFactory.getCurrentSession().beginTransaction();
//do something here
sessionFactory.getCurrentSession().endTransaction();

나는 그것을 피하기를 원했고, 자동으로 내 메소드 밖에서 처리하기를 원한다. TransactionProxyFactoryBean에 대해 읽었으며 xml 구성이 매우 길다고 생각하고 가능한 모든 클래스에 대해 반복해야하므로 가능한 경우 트랜잭션을 사용하지 않으려합니다.

@Transactional을 사용하려고했지만 전혀 작동하지 않습니다. 내 applicationContext.xml에이 줄이 있습니다.

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

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

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

그리고 이미 @Transactional을 사용하여 서비스 클래스를 표시했지만 "xxx는 활성 트랜잭션이 없으면 유효하지 않습니다"라는 메시지가 나타납니다. 다음은 오류를주는 예제 코드입니다 (단위 테스트 btw에서 실행).

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
{
    "classpath:applicationContext.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class UserServiceTest
{
    @Resource
    private UserService userService;

    @Test
    public void testAddUser()
    {
        User us = new User();
        us.setName("any name");
        userService.addUser(us);
    }
}

이 경우 정확한 오류 메시지는 다음과 같습니다. "org.hibernate.HibernateException : 저장이 활성 트랜잭션없이 유효하지 않습니다."

업데이트 : (예 : 실제 웹 응용 프로그램에서) 단위 테스트 외부에서 userService.addUser () 메서드를 호출하는 시도하고 같은 오류가 있어요.

이것은 내 hibernate.cfg.xml이다.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <!-- all my mapping resources here -->
    </session-factory>
</hibernate-configuration>

userService 클래스는 @Transactional로 표시됩니다. 나는 최대 절전 모드 3.3.2와 봄 2.5.6을 사용하고있다.

이 문제를 해결하는 방법에 대한 조언을받을 수 있습니까?

해결법

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

    1.다음 라인을 삭제하면 스프링 관리 트랜잭션과 충돌합니다 :

    다음 라인을 삭제하면 스프링 관리 트랜잭션과 충돌합니다 :

    <property name="current_session_context_class">thread</property> 
    
  2. ==============================

    2.@hephestos : 매개 변수 current_session_context_class의 값은 세션 (Hibernate 세션)이 바인드되어야하는 컨텍스트를 결정합니다.

    @hephestos : 매개 변수 current_session_context_class의 값은 세션 (Hibernate 세션)이 바인드되어야하는 컨텍스트를 결정합니다.

    기본적으로 현재 실행중인 스레드와 바인딩됩니다. 그러나 "jta"를 사용하여 트랜잭션을 관리하는 경우이 매개 변수의 값을 "jta"로 변경하면 세션이 현재 JTA 트랜잭션 컨텍스트에 바인딩됩니다.

    기본적으로 세션의 컨텍스트를 정의합니다. 추가 정보 : http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/architecture.html#architecture-current-session

  3. from https://stackoverflow.com/questions/4293098/how-to-integrate-spring-with-hibernate-session-and-transaction-management by cc-by-sa and MIT license