개발서적/토비 스프링 3.1-Vol.1

[토비의 스프링 - Vol.1] xml 트랜잭션 설정 방법

기본적으로 필요한 정보 (dataSource, transactionManager 필요)

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource" >
  <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/springbook?serverTimezone=UTC" />
  <property name="username" value="root"/>
  <property name="password" value="root" />
</bean>

 

 

1. bean으로 설정하는 방법

<bean id="transactionAdvice" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="transactionAttributes">
            <props>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

 

2.  tx와 aop태그를 이용하는 방법 (*beans 태그에 tx, aop관련 설정이 있어야한다)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            https://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx.xsd">

....

	<!--
        tx태그를 사용할 경우
        - transactionManager는 트랜잭션 매니저 빈 아이디가 transactionManager가 있으면 생략 가능
    -->
    <tx:advice id="transactionAdvice">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:advisor advice-ref="transactionAdvice" pointcut="bean(*Service)"/>
    </aop:config>