[Tip] Spring AOP로 트랜젝션 설정하기

2020. 5. 25. 10:17Spring

1. XML에 AOP 트랜젝션을 설정하기

dataSource 설정과 같은 LEVEL의 XML에 설정해야만 AOP 기반의 트랜젝션이 정상 동작함을 확인함..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?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:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    <bean id="dataSource" class="com.bridgetec.common.SecurityBasicDataSource" destroy-method="close" depends-on="precompareDataSourceAccount">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:TEST" />
        <property name="username" value="TEST" />
        <property name="password" value="TEST" />
        <property name="defaultAutoCommit" value="true" />
        <property name="initialSize" value="5" />
        <property name="maxTotal" value="30" />
        <property name="maxIdle" value="5" />
        <property name="maxWaitMillis" value="30000" />
        <property name="validationQuery" value="SELECT 1 FROM DUAL" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
    </bean>
    
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*AccessLog" propagation="REQUIRES_NEW" rollback-for="Exception"/>
            <tx:method name="*AccessList" propagation="REQUIRES_NEW" rollback-for="Exception"/>
            <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut id="requiredTx" expression="execution(* com.test..service.*Service.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="requiredTx" />
    </aop:config>
    
</beans>
cs

 

 

2. Service 클래스 작성하기

Service 클래스의 메서드에서 try-catch로 예외를 처리하게 되면 트랜젝션이 동작하지 않음. Controller까지 예외가 전달되도록 구조를 잡아야 함.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Service
public class TestService {
    
    Logger logger = LoggerFactory.getLogger(TestService.class);
    
    @Autowired
    private TestDAO testDao;
    
    
    public void insert(Map<String, Object> paramMap) throws Exception {
        int insertCount = testDao.insert(paramMap);
        if (insertCount <= 0)
            throw new Exception();
    }
}
cs