AOP를 이용한 Transaction 처리
1. API ( pom.xml )
- 버전은 현재 project의 spring 버전과 동일하게
- AOP API도 필요
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
2. Transaction 관리자 객체 생성
- API에 있는 클래스의 객체를 생성 하므로 ***-context.xml에 작성
- TransactionManager의 id 속성의 값은 꼭 transactionManager 로 지정
- 다른 이름으로 지정 시 error 발생 할 수 있음
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="username" value="#{database['oracle.user']}" />
<property name="password" value="#{database['oracle.pw']}" />
<property name="url" value="#{database['oracle.url']}" />
<property name="driverClassName" value="#{database['oracle.driver']}" />
</bean>
.....
<!-- transaction 관리자 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
3. Trasaction 설정
A. XML 설정
- AOP 를 이용
- Exception 발생시 RollBack 을 실행하는것이 Advice
- Service class의 메서드 들이 Point cut
- Joint point 는 Service의 메서드들이 실행 후 Exception 발생 했을 때 적용 이라 After-Thrwoing
**-context.xml
<!-- Advice 생성 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--모든 메서드들에서 예외가 발생하면 rollback 실행 -->
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!-- Aop 설정 -->
<aop:config>
<aop:pointcut expression="execution(* com.iu.s5..*Service.*(..))" id="requriedTx"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="requriedTx" />
</aop:config>
B. Annotation 설정