본문 바로가기

IT/Spring + iBatis

[Spring] 선언적 트랜잭션

XML 빈 설정 :

<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>


트랜잭션 적용 :

세번째 insert함수 호출에서 두번째 insert 시도와 동일한 PK로 데이터 입력을 시도하고 있음 

 "java.sql.SQLException: ORA-00001: 무결성 제약 조건(SPRING.PK_USERINFO)에 위배됩니다." Exception 발생

@Override
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public void txTest() throws Exception {
	userDao.insert(new User("testTx1", "testTx1", "testTx1", "testTx1", null));
	userDao.insert(new User("testTx2", "testTx2", "testTx2", "testTx2", null));
	userDao.insert(new User("testTx2", "testTx2", "testTx2", "testTx2", null));
}


결과 :

Exception이 발생한 세번째 시도는 물론 첫번째와 두번째 데이터 입력도 Rollback되었음