티스토리 뷰

Spring JDBC 6-4 Transaction 관리

Transaction Namespace 등록

Spring에서는 Transaction 처리를 AOP를 이용함.

AOP Advice 클래스처럼 작동됨

TransactionManager Advisor로 동작함

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

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

</beans>

DataSource 설정

<beans >

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />

<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />

<property name="username" value="sa" />

<property name="password" value="sa" />

</bean>

</beans>

TransactionManager 설정

<beans ...>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

<!--... property 설정 ...-->

</bean>

<bean id="txManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

</beans>

Transaction 정책 설정

Transaction 정책을 설정하며 TransactionManager AOP Advice 클래스로 사용하기 위해 설정

<beans ...>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<!--... property 설정 ...-->

</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="get*" read-only="true"/>

<tx:method name="*"/>

</tx:attributes>

</tx:advice>

</beans>

AOP를 이용한 Trasaction의 적용

TransactionManager 객체를 Advice로 사용하는 AOP 설정을 추가

<beans ...>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<!--... property 설정 ...-->

</bean>

<bean id="txManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">

</tx:advice>

<aop:config>

<aop:pointcut id="txPointcut"

expression="execution(* com.multicampu.biz..*(..))"/>

<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>

</aop:config>

</beans>

프로그램에서의 TransactionManager 사용

AOP 설정으로 선언적 처리방법도 있지만 프로그램에서 직접 TransactionManager 를 이용하는 방법도 존재함.

TransactionTemplate Bean을 등록하고 이것을 비즈니스 객체에 Injection 시킴.

비즈니스 객체에서 주입된 TransactionTemplate 객체를 이용해 execute()메서드에 TransactionCallback 타입의 객체를 등록하고 doInTransaction() 추상 메서드를 overriding .

doInTransaction() 메서드 안에서 수행되는 구문이 정상 실행되면 자동 Commit, 실패하면 Rollback.

<beans ...>

<bean id="txManager" class="org.springframework">

<property name="dataSource" ref="dataSource" />

</bean>

<bean id="transactionTemplate" class="org.springframework...">

<property name="transactionManager" ref="txManager" />

</bean>

<bean id="userService" class="com.multicampus.biz.user.UserServiceImpl">

<property name="transactionTemplate" ref="transactionTemplate" />

</bean>

</beans>

TransactionManager를 사용하는 자바코드

public class UserServiceImpl implements UserService{

public Object creditTransfer() {

return transactionTemplate.execute(new TransactionCallback() {

public Object doInTransaction(TransactionStatus status) {

return transfer();

}

});

}

}

댓글