복붙노트

[SPRING] 봄, 최대 절전 모드 및 선언적 트랜잭션 구현 : 활성 트랜잭션이 없음

SPRING

봄, 최대 절전 모드 및 선언적 트랜잭션 구현 : 활성 트랜잭션이 없음

나는 선언적 트랜잭션 작업을하려고합니다.

이것은 내 spring.xml 파일입니다.

<?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"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation=
       "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="jdbc:h2:tcp://my/db/path" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="data" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <context:component-scan base-package="test" />

    <tx:annotation-driven/>

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

</beans>

그리고 이것은 제 컨트롤러 구현입니다.

//file TestController.java
public interface TestController {

    public List<Test> findAll();

}

//file TestControllerImp.java
@Controller
public class TestControllerImp implements TestController{

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * @return the sessionFactory
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * @param sessionFactory the sessionFactory to set
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory=sessionFactory;
    }

    @Transactional
    public List<Test> findAll() {
        return sessionFactory.getCurrentSession().createQuery("from Test").list();
    }

}

둘 다 test라는 패키지 안에 있습니다.

이것은 내 시도입니다 :

TestController tc=context.getBean(TestController.class);
List<Test> list=tc.findAll();

그러나 이것은 예외를 던집니다.

transactionManager가 작동하지 않는 이유는 무엇입니까? @ Transactional annotation으로 모든 트랜잭션이 스프링 프레임 워크에 의해 관리되기를 바랍니다. 내가 무엇을 할 수 있을지?

모두에게 감사드립니다.

해결법

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

    1.Spring이 트랜잭션을 관리 할 때 필요없는 다음 라인을 제거하십시오 :

    Spring이 트랜잭션을 관리 할 때 필요없는 다음 라인을 제거하십시오 :

    <prop key="hibernate.current_session_context_class">thread</prop> 
    <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> 
    

    사실, hibernate.current_session_context_class를 설정하면 Spring 트랜잭션 관리가 효과적으로 비활성화된다. AbstractSessionFactoryBean.setExposeTransactionAwareSessionFactory () javadoc :

  2. ==============================

    2.클래스가 이와 같은 인터페이스를 구현할 때 @Controller와 같은 스테레오 타입 어노테이션이 적용된 클래스를 사용하면 이상한 동작을 얻을 수 있다는 것을 상기하는 것 같습니다.

    클래스가 이와 같은 인터페이스를 구현할 때 @Controller와 같은 스테레오 타입 어노테이션이 적용된 클래스를 사용하면 이상한 동작을 얻을 수 있다는 것을 상기하는 것 같습니다.

    나는 이것에 대한 해결 방법을 100 % 확신하지는 못했지만 다음 중 하나 또는 둘 모두를 시도해보십시오 :

    하나 또는 둘 다 또는 그 트릭을해야하지만 둘 다 이상적입니다. 나는 다른 질문에서 전에 이것을 보았고, 결코 그것을 일으키는 원인의 바닥에 도달하지 못했습니다.

  3. from https://stackoverflow.com/questions/4138663/spring-hibernate-and-declarative-transaction-implementation-there-is-no-active by cc-by-sa and MIT license