Java/Spring

트랜잭션 제어

amungstudy 2023. 8. 11. 15:48

메세지보내면 점수가 추가되는 것처럼 동일한 트랜잭션에서 처리가 되어야 하는 경우 사용.

 

1. root-context에 bean추가

2. namespace에 tx추가

 

3. <!-- 트랜잭션 제어. id값 변경되면 안됨. -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="ds"/>
</bean>
<!-- annotation으로 하나의 작업처리가 될 method 또는 class를 스캔 -->
<!-- @Transactional -->
<tx:annotation-driven/>

 

4. 필요한 메소드에 @Transactional   붙여준다.

@Transactional
@Override
public void addMessage(MessageVO vo) throws Exception {
System.out.println("addMessage Service 시작");
System.out.println("addMessage Service " + vo);
// 발신자 포인트 증가
UserVO uv = new UserVO();
uv.setUid(vo.getSender());
uv.setUpoint(10);
userDAO.updatePoint(uv);

// 메세지 등록
messageDAO.create(vo);
System.out.println("addMessage Service 종료");

}

AOP로 동작하니까 servlet-context에  <aop:aspectj-autoproxy /> 있어야함.