정말 오랜만에 블로그를 작성한다.
하루에 글 하나씩 올리기로 했는데....
초등학교 여름방학때 밀린 일기의 첫글같은 느낌..
최대한 자주 올리도록 노력해보자!
오늘 작성할 글은 개발카테고리에 새로 추가한 Spring 에 관련된 내용이다.
어제 APM (Application Performance Management) 를 처음 사용하며,
외부에서 어떻게 운영중인 어플리케이션에 접근하여 트렌젝션을 실시간으로 추적할 수 있는지에 대한 의문으로
트랜젝션 자체를 다시 찾아보던 중 나중에 스프링-mabtis연동 시 xml에 트랜젝션을 설정하여 서비스영역에서 처리할 수
있게 세팅하는 방법이 있어, 기록해놓으면 유용하겠다 싶었다.
@transactional 어노테이션으로 트랜잭션을 주는 방식도 있긴하지만, xml 설정을 함으로
service영역에서의 트랜잭션 처리를 할 수 있다.
현재 방식으로 insert 쿼리 여러개를 선언하고 중간에 오류를 발생시키면 오류나기 전에 있던
데이터들은 insert가 되어진다.
이부분을 막고자 트랜잭션설정을 주면 되는데,
트랜잭션 처리를위한 추가 dependency를 pom.xml에 등록해주도록 하자
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
기존 mvc-config.xml에도 아래와 같이 변경.
<context:component-scan base-package="com.spring.web" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
이어서 application-config.xml에 내용 추가 및 수정을 해주도록 하자
<bean>태그가 아닌 전체 xml을 감싸고 있는 <bean> 태그가 있을 텐데 다음과 같이
변경 해주도록 하자
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
aop 부분과 tx 부분만 추가 되었다.
마지막으로 트랜잭션 설정을 하면 마무리 된다.
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/>
</bean>
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceOperation" expression="execution(public * com.spring.web..service.*Service.*(..))" /> <aop:advisor id="transactionAdvisor" pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="remove*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
그럼 귀차니즘을 극복하며 틈틈히 블로그 글을 작성할 것을 다짐하며 포스팅을 마치겠다.
출처 : https://roqkffhwk.tistory.com/112