복붙노트

[SPRING] Spring Hibernate 트랜잭션 관리

SPRING

Spring Hibernate 트랜잭션 관리

방금 봄과 동면을 사용하여 프로젝트를 시작했습니다. 내 DAO 계층 클래스는 HibernateDaoSupport를 확장합니다. 특수 효과를 사용하지 않습니다. 이전에는 스트럿츠를 사용 했으므로 Session 클래스에서 제공하는 getTransaction, commit, rollback .. 메서드를 사용했습니다. 내 요구 사항은 모든 DAO 클래스에 대해 매우 간단합니다. 예외가있는 경우 롤백합니다. 그렇지 않으면 커밋합니다. 봄 거래 관리를 소개하는 가장 간단한 방법을 제안하십시오.

해결법

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

    1.당신의 질문에서 몇 가지 명확하지 않습니다. 나의 설명은 아래의 가정에 기초한다.

    당신의 질문에서 몇 가지 명확하지 않습니다. 나의 설명은 아래의 가정에 기초한다.

    여기에 봄 환경 설정이 있습니다.

        <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:hsql://localhost:9001" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>
    
    <bean id="mySessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="mappingResources">
            <list>
                <value>product.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.HSQLDialect
            </value>
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory" />
    </bean> 
    
    <tx:annotation-driven transaction-manager="transactionManager"  />
    

    이것이 설정되면 아래와 같이 DAO 메소드에서 봄 트랜잭션 주석을 사용할 수 있습니다. 예외가 발생하면 Spring은 트랜잭션 시작, 트랜잭션 커밋 또는 트랜잭션 롤백을 처리합니다. 비즈니스 서비스가 있다면 DAO 대신 서비스에서 트랜잭션 주석을 사용하는 것이 이상적입니다.

    @Transactional(propagation=Propagation.REQUIRED)
    public class MyTestDao extends HibernateDaoSupport {    
    public void saveEntity(Entity entity){
        getHibernateTemplate().save(entity);
    }
    @Transactional(readOnly=true)
    public Entity getEntity(Integer id){
        return getHibernateTemplate().get(Entity.class, id);
    }
     }
    

    아래의 코드는 annotation보다는 AOP에 대한 Spring의 지원을 사용하여 트랜잭션 관리가 어떻게 달성 될 수 있는지를 보여준다.

        <!-- Define your 'myDatasource' bean and 'mySessionFactory' bean as shown in previous code snippet -->
    <!--  Then follow the steps shown below -->
    
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />   
    
    <!-- this is the dao object that we want to make transactional -->
    <bean id="testDao" class="com.xyz.daos.MyTestDao" />
    
    <!-- the transactional advice  -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- all methods starting with 'get' are read-only -->
            <tx:method name="get*" read-only="true" />
            <!-- other methods use the default transaction settings (see below) -->
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    
    <!-- ensure that the above transactional advice runs for any execution of 
        a method in 'daos' package-->
    <aop:config>
        <aop:pointcut id="allDaoMethods"
            expression="execution(* com.xyz.daos.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethods" />
    </aop:config>
    

    자세한 내용은 - 봄 선언적 트랜잭션

  2. from https://stackoverflow.com/questions/13087928/spring-hibernate-transaction-management by cc-by-sa and MIT license