인프런 커뮤니티 질문&답변
@Transactional rollback이 안되네요
작성
·
2.1K
0
안녕하세요. 강의 너무 잘 듣고 있습니다.
@Transactional을 붙이면 종료하면서 rollback이 되어 테이블에 커밋되지 않는다고 이해했습니다.
로그상에는 rollback이 되었다고 나오는데 쿼리 조회해보면 데이터가 조회가 되네요..
로그는 다음과 같습니다.
2021-09-21 16:23:07.767 INFO 15576 --- [ main] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [DefaultTestContext@3700ec9c testClass = MemberServiceIntegrationTest, testInstance = hello.hellospring.service.MemberServiceIntegrationTest@27dc627a, testMethod = 회원가입@MemberServiceIntegrationTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@2002348 testClass = MemberServiceIntegrationTest, locations = '{}', classes = '{class hello.hellospring.HelloSpringApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@4d14b6c2, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@2657d4dd, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@387a8303, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@7eecb5b8, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@5dafbe45, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@71623278], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]; transaction manager [org.springframework.jdbc.support.JdbcTransactionManager@570ba13]; rollback [true] 2021-09-21 16:23:09.376 INFO 15576 --- [ main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test: [DefaultTestContext@3700ec9c testClass = MemberServiceIntegrationTest, testInstance = hello.hellospring.service.MemberServiceIntegrationTest@27dc627a, testMethod = 회원가입@MemberServiceIntegrationTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@2002348 testClass = MemberServiceIntegrationTest, locations = '{}', classes = '{class hello.hellospring.HelloSpringApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@4d14b6c2, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@2657d4dd, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@387a8303, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@7eecb5b8, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@5dafbe45, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@71623278], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]] 2021-09-21 16:23:09.428 INFO 15576 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2021-09-21 16:23:09.466 INFO 15576 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
어떤 문제일까요?
답변 4
1
o_o0122님, 혹시 JDBCMemberRepository 의 save 메소드에서 conn 변수를 얻어올 때, getConnection()이 아닌 dataSource.getConnection() 을 이용하지 않으셨나요?
정확한 원인은 모르겠지만 아마도 Connection 이 동일하게 유지되는 경우에만 이 코드가 동작하는 것 같습니다. 즉 Transaction Manager 가 관리하는 경우에만 Transactional annotation이 동작하는 것 같습니다.
0
김영한
지식공유자
안녕하세요. o_o0122님
전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.
구글 드라이브 업로드 방법은 다음을 참고해주세요.
주의: 업로드시 권한 문제 꼭 확인해주세요
추가로 다음 내용도 코멘트 부탁드립니다.
1. 실행 방법을 알려주세요.
2. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.
감사합니다.
0
o_o0122
질문자
네 말씀해주신대로 되는 것 같습니다.
데이터도 저장됩니다.
참고로 잘 안되서 jdbc template으로 넘어갔는데 jdbc template으로 하면 rollback이 됩니다.
0
요청사항에 대해 확인해주셔서 감사합니다.
하나만 더 여쭤볼게요,
@Rollback(false)를 추가하면 결과가 어떻게 될까요?
데이터 저장 유무와 커밋 메시지가 출력되는지 확인해주시면 감사하겠습니다.






답변 감사합니다!
당연히 식별해서 테스트 해보았습니다.