본문 바로가기

프레임워크/Spring

[프레임워크] <Spring>〈Maven〉Transaction

Transaction

◎ 더 이상 쪼갤 수 없는 최소 작업 단위를 의미한다.

비즈니스 메서드의 실행 중 문제가 생겼을 경우 이전 상태로 롤백하기 위해 사용되는 것이다.


Transaction을 사용하기 위한 추가 스키마

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

<?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:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation= "http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

</beans>

트랜잭션을 하기 위한 설정

advisor

  ⇨ == aspect == 위빙

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
	<property name="url" value="jdbc:mysql://localhost:3306/lee" />
	<property name="username" value="root" />
	<property name="password" value="1234" />
</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="*" /> // 모든 문에 대해 트랜잭션
        <tx:method name="select*" read-only="true" /> //SELECT문에 대해 트랜잭션
  	</tx:attributes>
</tx:advice>

// 위빙/결합 처리
<aop:config>
	<aop:pointcut id="aPointcut" expression="execution(* com.spring.biz..*Impl.*(..))" /> 
 	<aop:advisor pointcut-ref="aPointcut" advice-ref="txAdvice" />
</aop:config>