language/Spring
[Java/Spring] Spring Transaction / 스프링 트랜잭션 처리
zincah
2022. 3. 23. 12:54
반응형
스프링의 트랜잭션 : 스프링에서 트랜잭션 처리를 컨테이너가 자동으로 처리하도록 설정하는 것을 말합니다.
- 스프링 트랜잭션 설정에는 AOP가 사용됩니다.
- XML 기반의 AOP 설정만 사용할 수 있고 어노테이션 설정은 사용할 수 없습니다.
- <aop:aspect> 엘리먼트를 사용하지 못하고 <aop:advcie> 엘리먼트를 사용해야 합니다.
1. 트랜잭션 네임스페이스 추가 및 관리자 등록
트랜잭션을 사용하기 위해서는 applicationContext.xml 파일에 네임스페이스를 추가해줘야 합니다.
스프링이 제공하는 모든 트랜잭션 관리자는 트랜잭션 관리에 필요한 commit(), rollback() 메서드를 가지고 있습니다.
- applicationContext.xml에 트랜잭션 관리자 등록
(원래 사용하던 프로젝트에 트랜잭션 처리를 해보는 것이므로 현재 aop설정이 다 되어있고 JdbcTemplate이 빈으로 등록이 되어 있는 상태입니다. )
<context:component-scan base-package="zinc.spring.web"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- datasource : connection -->
<context:property-placeholder location="classpath:config/database.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- spring jdbc 설정 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- transaction 설정 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">
</property>
</bean>
* 주의할 점 : DataSourceTransactionManager를 <bean>으로 등록했다고 자동으로 트랜잭션이 관리되는 것은 아니고 실질적으로 트랜잭션을 관리해주는 것이 있습니다.
어드바이스는 트랜잭션 관리자가 가지고 있는 메서드를 호출, 트랜잭션의 실질적인 관리 기능을 제공합니다. 어드바이스는 비즈니스 메서드 실행 전이나 후에 동작하여 비즈니스 메서드와 무관하게 공통기능을 제공합니다.
(ex. 예외 처리, 트랜잭션 처리..)
2. 트랜잭션 어드바이스 등록
- applicationContext.xml에 어드바이스 추가
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
- <tx:method name="get*" read-only="true/> : get으로 시작하는 method를 트랜잭션 관리대상에서 제외한다는 뜻입니다. 나머지 메소드들은 밑에 구문을 통해서 트랜잭션 관리대상에 포함됩니다.
<tx:method> 엘리먼트 속성
속성 | 의미 |
name | 트랜잭션이 적용될 메서드 이름 지정 |
read-only | 읽기 전용 여부를 지정 (기본 값 false) |
no-rollback-for | 트랜잭션을 롤백하지 않을 예외 지정 |
rollback-for | 트랜잭션을 롤백할 예외 지정 |
3. AOP 설정을 통한 트랜잭션 적용
비즈니스 메서드 실행 후에 트랜잭션 관리 어드바이스가 동작하도록 AOP 설정을 추가해줍니다.
<aop:aspect> 엘리먼트를 사용하지 않고 <aop:advisor> 엘리먼트를 사용해줍니다. (같은 기능)
<context:component-scan base-package="zinc.spring.web"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- datasource : connection -->
<context:property-placeholder location="classpath:config/database.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- spring jdbc 설정 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- transaction 설정 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* zinc.spring.web..*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
결론 : 트랜잭션은 메서드 관리됨으로 예외가 발생하면 예외가 발생한 메소드 전체가 Rollback 처리됩니다.
반응형