■ rollbackFor 와 noRollbackFor 을 비교
LogAspect.java
package sp.mvc.aop;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut;
@Aspect public class LogAspect {
@Pointcut(value="execution(* sp.mvc.board.controller.*.*(..))") public void logPrint(){}; @AfterThrowing(pointcut="logPrint()", throwing="ex") public void afterThrowing(JoinPoint joinPoint, Exception ex){ System.out.println("***** afterThrowing *****"); System.out.println("method : " + joinPoint.toShortString()); System.out.println("Exception : " + ex.toString()); } } |
BoardController.java - 1
package sp.mvc.board.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView;
import sp.mvc.board.service.BoardService; import sp.mvc.board.vo.BoardVO;
@Controller public class BoardController { @Autowired private BoardService boardService;
@RequestMapping("/boardInsert.sp") @Transactional(propagation=Propagation.REQUIRED, rollbackFor={Exception.class}) public void insertItem(){ BoardVO boardVo1 = new BoardVO(); boardVo1.setBoardSeq(2151); boardVo1.setBoardType("001"); boardVo1.setStep(1); boardVo1.setTitle("spring 3.0 mvc - 2151"); boardVo1.setContent("컨텐츠 내용 입력... 테스트 중..."); boardVo1.setIp("123.123.321.321"); BoardVO boardVo2 = new BoardVO(); boardVo2.setBoardSeq(2152); boardVo2.setBoardType("001"); boardVo2.setStep(1); boardVo2.setTitle("spring 3.0 mvc - 2152"); boardVo2.setContent("컨텐츠 내용 입력... 테스트 중..."); boardVo2.setIp("123.123.321.321"); System.out.println("===== 1 ====="); boardService.insertBoardItem(boardVo1); System.out.println("===== 2 ====="); if(true){ throw new NumberFormatException(); } boardService.insertBoardItem(boardVo2); System.out.println("===== 3 ====="); } } |
● boardVo1, boardVo2 모두 not insert
● 해당 메소드(insertItem())의 트랜잭션은 다음과 같음
=> @Transactional(propagation=Propagation.REQUIRED, rollbackFor={Exception.class})
따라서 rollback에 대한 속성만 있으므로 NumberFormatException에 의해 모두 rollback 됨
◆ 위의 소스를 다음과 같이 수정
BoardController.java - 2
package sp.mvc.board.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView;
import sp.mvc.board.service.BoardService; import sp.mvc.board.vo.BoardVO;
@Controller public class BoardController { @Autowired private BoardService boardService;
@RequestMapping("/boardInsert.sp") @Transactional(propagation=Propagation.REQUIRED, rollbackFor={Exception.class}, noRollbackFor={NumberFormatException.class}) public void insertItem(){ BoardVO boardVo1 = new BoardVO(); boardVo1.setBoardSeq(2151); boardVo1.setBoardType("001"); boardVo1.setStep(1); boardVo1.setTitle("spring 3.0 mvc - 2151"); boardVo1.setContent("컨텐츠 내용 입력... 테스트 중..."); boardVo1.setIp("123.123.321.321"); BoardVO boardVo2 = new BoardVO(); boardVo2.setBoardSeq(2152); boardVo2.setBoardType("001"); boardVo2.setStep(1); boardVo2.setTitle("spring 3.0 mvc - 2152"); boardVo2.setContent("컨텐츠 내용 입력... 테스트 중..."); boardVo2.setIp("123.123.321.321"); System.out.println("===== 1 ====="); boardService.insertBoardItem(boardVo1); System.out.println("===== 2 ====="); if(true){ throw new NumberFormatException(); } boardService.insertBoardItem(boardVo2); System.out.println("===== 3 ====="); } }
|
● boardVo1의 데이터는 insert, boardVo2는 not insert
● 트랜잭션의 속성 중 noRollbackFor={NumberFormatException.class} 이므로 NumberFormatException에 대해서는 rollback을 하지 않기 때문에 boardVo1의 데이터는 정상적으로 insert 됨.
applicationContext.xml
pjt_main_config.xml
결과
요청 URL
http://localhost:8090/spring305/boardInsert.sp
===== 1 ===== ===== 2 ===== ***** afterThrowing ***** method : execution(BoardController.insertItem()) Exception : java.lang.NumberFormatException 2012. 5. 29 오후 2:52:48 org.apache.catalina.core.StandardWrapperValve invoke ...... |