Spring AOP Starter Kit(2) - Transaction

jadeco·2023년 3월 26일
0

1. 개념

Transaction은 데이터베이스의 일련의 작업들을 논리적으로 하나의 단위로 묶어서 처리하는 것입니다. Transaction을 구현하기 위해서는 데이터베이스 연결을 생성하고, SQL을 실행하고, 커밋 또는 롤백하는 등의 작업을 수행해야 합니다.

Spring AOP를 이용하여 Transaction을 구현할 경우, Aspect 클래스에서 @Around 어노테이션을 이용하여 메서드 실행 전과 후에 트랜잭션을 시작하고 커밋 또는 롤백하도록 구현합니다.

2. 사용요소

Transaction을 구현하기 위해서는 다음과 같은 요소들이 필요합니다.

  • Transaction Manager: 트랜잭션을 시작하고 종료하는 기능을 제공합니다.
  • Platform Transaction Manager: 데이터베이스에 따라 다르게 구현되는 트랜잭션 관리자입니다.
  • Transactional Annotation: 트랜잭션을 적용할 메서드를 표시하기 위해 사용되는 어노테이션입니다.
  • Aspect: 트랜잭션을 시작하고 종료하는 기능을 구현하는 Aspect 클래스입니다.

3. 사용법

Spring AOP를 이용하여 Transaction을 구현하기 위해 다음과 같은 순서로 작업을 수행합니다.

  1. 의존성 추가
  2. Transaction Manager 설정
  3. Transactional Annotation 추가
  4. Aspect 클래스 작성
  5. XML 설정 파일에 Aspect 설정 추가

1. 의존성 추가

<!-- 
	AspectJ Weaver 
	AOP를 사용하기 위해서 추가함.
-->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.8.8</version>
</dependency>

2. Transaction Manager 설정(root-context.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: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-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


		<!-- 트랜잭션 처리를 위한 트랜잭션 매니저 추가 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
</beans>

3. Transaction Adivce 설정(root-context.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: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-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


		<!-- 트랜잭션 처리를 위한 트랜잭션 매니저 추가 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
  
  	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="select*" read-only="true"/>
			<tx:method name="register*" rollback-for="Exception"/>
			<tx:method name="modify*" rollback-for="Exception"/>
			<tx:method name="delete*" rollback-for="Exception"/>
		</tx:attributes>
	</tx:advice>
</beans>

4. AOP Aspect 설정(root-context.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: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-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


		<!-- 트랜잭션 처리를 위한 트랜잭션 매니저 추가 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
  
  	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="select*" read-only="true"/>
			<tx:method name="register*" rollback-for="Exception"/>
			<tx:method name="modify*" rollback-for="Exception"/>
			<tx:method name="delete*" rollback-for="Exception"/>
		</tx:attributes>
	</tx:advice>
  
  	<aop:config proxy-target-class="true">
		<aop:pointcut expression="execution(* com.kh.spring..*Impl.*(..))" id="serviceMethod"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
	</aop:config>
</beans>

5. autoproxy 설정(servlet-context.xml)

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

	<aop:aspectj-autoproxy/>
</beans:beans>

6. AOP 동작 확인(에와 발생시키기)

	@Repository
	public class MemberStoreLogic implements MemberStore{
    		@Override
            public int insertMember(Member member) {
                sqlSession.insert("memberMapper.insertMember",member); // 성공
                int result = sqlSession.insert("memberMapper.insertMember",member); // 실패
                return result;
            }
    }
profile
당신도요

0개의 댓글