스프링

스프링(Spring) - Transaion(트랜잭션)

낙산암 2022. 1. 17. 17:46


@Log4j
@AllArgsConstructor
@Controller
public class TxTestController {  


 @Inject
 private TxTestService service;

 @GetMapping("/tx/{num}")
 public void transiotn(@PathVariable("num") int num) throws SQLException {
 
if(num == 1){
log.info("transionTest1");
service.transionTest1();
}else if(num == 2){
log.info("transionTest2");
service.transionTest2();
}else if(num == 3){
log.info("transionTest3");
service.transionTest3();
}else if(num == 4){
log.info("transionTest4");
service.transionTest4();
}else if(num == 5){
log.info("transionTest5");
service.transionTest5();
}else if(num == 6){
log.info("transionTest6");
service.transionTest6();
}else if(num == 7){
log.info("transionTest7");
service.transionTest7();
}  
 
 }  
}

 

 


@Log4j
@Service
@AllArgsConstructor
public class TxTestService {

@Inject
private BoardMapper mapper;

@Transactional
public void transionTest1() {

log.info("transionTest1()테스트 ");
BoardVO boardVO = new BoardVO();
boardVO.setBcontent("트랜잭션1");
boardVO.setBname("트랜잭션1");
boardVO.setBtitle("트랜잭션1");

mapper.insert(boardVO);

boardVO.setBcontent("트랜잭션2");
boardVO.setBname("트랜잭션2");
boardVO.setBtitle("트랜잭션2");

mapper.insert(boardVO);
}

public void transionTest2() {

log.info("transionTest2()테스트 ");
BoardVO boardVO = new BoardVO();
boardVO.setBcontent("트랜잭션1");
boardVO.setBname("트랜잭션1");
boardVO.setBtitle("트랜잭션1");

mapper.insert(boardVO);

boardVO.setBcontent("트랜잭션2");
boardVO.setBname("트랜잭션2");
boardVO.setBtitle("트랜잭션2");

// 일부러 트랜잭션을 위한 것
boardVO = null;
mapper.insert(boardVO);
}

@Transactional
public void transionTest3() {

log.info("transionTest3()테스트 ");
BoardVO boardVO = new BoardVO();

boardVO.setBcontent("트랜잭션1");
boardVO.setBname("트랜잭션1");
boardVO.setBtitle("트랜잭션1");

mapper.insert(boardVO);

boardVO.setBcontent("트랜잭션2");
boardVO.setBname("트랜잭션2");
boardVO.setBtitle("트랜잭션2");

// 일부러 에러를 내게 함 //트랜잭션을 위한 것
boardVO = null;
mapper.insert(boardVO);
}

//uncheckedExeption(롤백 함)
@Transactional
public void transionTest4() {
BoardVO boardVO = new BoardVO();
boardVO.setBcontent("트랜잭션4");
boardVO.setBname("트랜잭션4");
boardVO.setBtitle("트랜잭션4");

mapper.insert(boardVO);

throw new RuntimeException("RuntimeException for rollback");
}

//CheckedExeption 테스트(롤백 안함)
@Transactional
public void transionTest5() throws SQLException {
BoardVO boardVO = new BoardVO();
boardVO.setBcontent("트랜잭션5");
boardVO.setBname("트랜잭션5");
boardVO.setBtitle("트랜잭션5");

mapper.insert(boardVO);

throw new SQLException("SQLException for rollback");
}

//@Transactional의 rollbackFor 옵션을 이용하면 Rollback이 되는 클래스를 지정가능함.
// Exception예외로 롤백을 하려면 다음과 같이 지정하면됩니다. @Transactional(rollbackFor = Exception.class) 
// 여러개의 예외를 지정할 수도 있습니다. @Transactional(rollbackFro = {RuntimeException.class, Exception.class})
@Transactional(rollbackFor = Exception.class) 
public void transionTest6() throws SQLException {
BoardVO boardVO = new BoardVO();
boardVO.setBcontent("트랜잭션6");
boardVO.setBname("트랜잭션6");
boardVO.setBtitle("트랜잭션6");

mapper.insert(boardVO);

throw new SQLException("SQLException for rollback");
}

@Transactional(rollbackFor = SQLException.class) 
public void transionTest7() throws SQLException {
BoardVO boardVO = new BoardVO();
boardVO.setBcontent("트랜잭션7");
boardVO.setBname("트랜잭션7");
boardVO.setBtitle("트랜잭션7");

mapper.insert(boardVO);

throw new SQLException("SQLException for rollback");
}

}