From 602545de7ac203d393f96ccbaf748c5571439dba Mon Sep 17 00:00:00 2001 From: haerong22 Date: Mon, 18 Jul 2022 03:23:55 +0900 Subject: [PATCH] #13 spring transaction - exception : rollback, commit --- .../src/main/resources/application.properties | 6 +- .../exception/RollbackTest.java | 73 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 spring-transaction/src/test/java/com/example/springtransaction/exception/RollbackTest.java diff --git a/spring-transaction/src/main/resources/application.properties b/spring-transaction/src/main/resources/application.properties index 4f64932e..9bfd8232 100644 --- a/spring-transaction/src/main/resources/application.properties +++ b/spring-transaction/src/main/resources/application.properties @@ -1 +1,5 @@ -logging.level.org.springframework.transaction.interceptor=TRACE \ No newline at end of file +logging.level.org.springframework.transaction.interceptor=TRACE +logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG +#JPA log +logging.level.org.springframework.orm.jpa.JpaTransactionManager=DEBUG +logging.level.org.hibernate.resource.transaction=DEBUG \ No newline at end of file diff --git a/spring-transaction/src/test/java/com/example/springtransaction/exception/RollbackTest.java b/spring-transaction/src/test/java/com/example/springtransaction/exception/RollbackTest.java new file mode 100644 index 00000000..e1ed345b --- /dev/null +++ b/spring-transaction/src/test/java/com/example/springtransaction/exception/RollbackTest.java @@ -0,0 +1,73 @@ +package com.example.springtransaction.exception; + +import lombok.extern.slf4j.Slf4j; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.transaction.annotation.Transactional; + +@SpringBootTest +public class RollbackTest { + + @Autowired + RollbackService rollbackService; + + @Test + void runTimeException() { + Assertions.assertThatThrownBy(() -> rollbackService.runtimeException()) + .isInstanceOf(RuntimeException.class); + } + + @Test + void checkedException() { + Assertions.assertThatThrownBy(() -> rollbackService.checkedException()) + .isInstanceOf(MyException.class); + } + + @Test + void rollbackFor() { + Assertions.assertThatThrownBy(() -> rollbackService.rollbackFor()) + .isInstanceOf(MyException.class); + } + + @TestConfiguration + static class RollbackTestConfig { + + @Bean + RollbackService rollbackService() { + return new RollbackService(); + } + } + + @Slf4j + static class RollbackService { + + // 런타임 예외 발생 -> 롤백 + @Transactional + public void runtimeException() { + log.info("call runtimeException"); + throw new RuntimeException(); + } + + // 체크 예외 발생 -> 커밋 + @Transactional + public void checkedException() throws MyException { + log.info("call checkedException"); + throw new MyException(); + } + + // 체크 예외 rollbackFor 지정 -> 롤백 + @Transactional(rollbackFor = MyException.class) + public void rollbackFor() throws MyException { + log.info("call rollbackFor"); + throw new MyException(); + } + } + + static class MyException extends Exception { + + } +}