선언적 transction 관련 문의드립니다.
- 작성자 :
- 김*율
- 작성일 :
- 2010-01-27 11:33:22
- 조회수 :
- 4,798
- 구분 :
- 실행환경
- 진행상태 :
- 완료
Q
spring의 선언적 트랜잭션 방법인 aop방식과 annotation방식 2개를 테스트했습니다.
그런데 rollback이 되질 않네요. 무엇이 문제인지 알고 싶습니다.
아래 코드 및 환경설정 첨부합니다.
- SampleController
@RequestMapping(value="/sample/inputSampleAction.do")
public String inputSampleAction(ModelMap model) throws Exception {
try {
sampleService.insertSampleInfo(sample);
} catch(Exception e) {
log.error("inputSampleAction exception : " + e);
throw e;
}
return "page";
}
- SampleService
- SampleServiceImpl
@Transactional(rollbackFor=Exception.class)
public void insertSampleInfo(Sample emp) throws Exception {
sampleDao.insertSampleReplInfo(emp);
int i = 1/0;//임의 Exception 발생
sampleDao.insertSampleInfo(emp);
}
- 환경xml(1)
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<tx:annotation-driven transaction-manager="txManager" />
- 환경xml(2)
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com..domain.logic.*Impl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
위의 두가지 방법으로 테스트를 해보았는데 transaction이 동작하지 않네요.
멍청한 질문인지는 모르겠지만 혹시나 해서 defaultAutoCommit을 false로 하면..
commit이 되지를 않구요(당연한건가;;)
이상태로 아래처럼 프로그래밍적 트랜잭션 처리를 하면 rollback이 잘 동작합니다.
@Resource(name="txManager")
PlatformTransactionManager txManager;
DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
TransactionStatus txStatus = txManager.getTransaction(txDefinition);
public void insertSampleInfo(Sample emp) throws Exception {
try {
sampleDao.insertSampleReplInfo(emp);
//int i=1/0;
sampleDao.insertSampleInfo(emp);
txManager.commit(txStatus);
} catch(Exception e) {
txManager.rollback(txStatus);
throw e;
}
}
그럼 답변 부탁드립니다.
그런데 rollback이 되질 않네요. 무엇이 문제인지 알고 싶습니다.
아래 코드 및 환경설정 첨부합니다.
- SampleController
@RequestMapping(value="/sample/inputSampleAction.do")
public String inputSampleAction(ModelMap model) throws Exception {
try {
sampleService.insertSampleInfo(sample);
} catch(Exception e) {
log.error("inputSampleAction exception : " + e);
throw e;
}
return "page";
}
- SampleService
- SampleServiceImpl
@Transactional(rollbackFor=Exception.class)
public void insertSampleInfo(Sample emp) throws Exception {
sampleDao.insertSampleReplInfo(emp);
int i = 1/0;//임의 Exception 발생
sampleDao.insertSampleInfo(emp);
}
- 환경xml(1)
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<tx:annotation-driven transaction-manager="txManager" />
- 환경xml(2)
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com..domain.logic.*Impl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
위의 두가지 방법으로 테스트를 해보았는데 transaction이 동작하지 않네요.
멍청한 질문인지는 모르겠지만 혹시나 해서 defaultAutoCommit을 false로 하면..
commit이 되지를 않구요(당연한건가;;)
이상태로 아래처럼 프로그래밍적 트랜잭션 처리를 하면 rollback이 잘 동작합니다.
@Resource(name="txManager")
PlatformTransactionManager txManager;
DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
TransactionStatus txStatus = txManager.getTransaction(txDefinition);
public void insertSampleInfo(Sample emp) throws Exception {
try {
sampleDao.insertSampleReplInfo(emp);
//int i=1/0;
sampleDao.insertSampleInfo(emp);
txManager.commit(txStatus);
} catch(Exception e) {
txManager.rollback(txStatus);
throw e;
}
}
그럼 답변 부탁드립니다.
A
안녕하세요.. 김종율님..
위의 두 예제에서는 "rollbackFor=Exception.class" 부분을 제외하시면 .. 처리될 것 같습니다..
Spring의 transaction 처리 방식은..
기본적으로 unchekced exceptions에 대해서만 rollback이 되도록 되어 있습니다.
(unchecked exceptions이란... RuntimeException을 상속하여 정의된 exception)
다만, 이 규칙을 추가로 제어하는 것이 rollback-for 또는 no-rollback-for입니다.
그럼.. 즐거운 하루되십시오.
감사합니다.
위의 두 예제에서는 "rollbackFor=Exception.class" 부분을 제외하시면 .. 처리될 것 같습니다..
Spring의 transaction 처리 방식은..
기본적으로 unchekced exceptions에 대해서만 rollback이 되도록 되어 있습니다.
(unchecked exceptions이란... RuntimeException을 상속하여 정의된 exception)
다만, 이 규칙을 추가로 제어하는 것이 rollback-for 또는 no-rollback-for입니다.
그럼.. 즐거운 하루되십시오.
감사합니다.