Spring
<Spring> 트랜잭션처리하기
나의 오류는 누군가 겪었던 오류
2023. 12. 13. 23:27
spring이 제공하는 TransactionManager클래스를 이용
→ DML 구문을 실행하면 기본적으로 commit(); 처리를 함
→ 같은 세션에서 DML구문을 실행할때 RuntimeException이 발생을 하면 rollback(); 처리
트랜잭션설정하기
springconfigutation.xml에 txnamespace를 등록,
트랜잭션매니저를 bean으로 등록,
root-context.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
</beans>
<!-- 트랜잭션처리 bean등록하기 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="proxyDataSource"/>
</bean>
<!-- 어노테이션방식으로 트랜잭션처리하기 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
- 어노테이션방식
- 적용할 메소드의 선언부에 @Transational
- @Transactional(옵션설정)
- AOP방식을 이용해서 처리하기 때문에 AOP설정이 되어있어야함
- xml 방식
- tx:config 태그로 트랜잭션에 대한 설정
- aop:config 태그로 적용할 메소드를 설정
트랜잭션 옵션설정
- propagation : 트랜잭션을 생성하는 방법에 대한 설정 default 이미 시작된 트랜잭션이 있으면 참여, 없으면 트랜잭션 생성
- isolation : 트랜잭션에서 수정된 내용을 다른 트랜잭션에서 사용할 수 있는지 여부
- timeout
- read-only : select문에 사용
- rollback-for, rollbackfor : rollback을 처리할 기준(Exception)
- no-rollback-for