티스토리 뷰
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();
}
});
}
}
- #한국사능력검정시험
- #k드라마
- #공무원준비
- #드라마추천
- #해커스한국사
- #한능검단기합격
- #한능검
- #psat
- #티빙
- #개발꿀팁
- #판타지로맨스
- #셸스크립트
- #드라마ost
- #큐넷
- #공무원시험
- #공시생
- #한능검심화
- #시스템관리자
- #tvn드라마
- #한능검2주
- #드라마촬영지
- #한능검공부법
- #에듀윌한국사
- #한능검기본
- #코딩초보
- #9급공무원
- #bash
- #mbc드라마
- #최태성한국사
- #자동화
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |