미해결
스프링 DB 2편 - 데이터 접근 활용 기술
spring webflux 와 spring data jpa Transaction 사용 시 문제
토이프로젝트를 하다 어쩌다 보니 spring webflux를 사용하게 되고 spring data jpa 는 reactive 환경에서 사용하지 못한다는 것을 모른 채 개발하다가...@SpringBootTest 에서 @Transactional 을 쓴 테스트에서 롤백이 되지 않는 현상을 발견했습니다.. TRACE 로 설정해 두고 실행시켜본 결과...```2024-01-11T00:54:00.058+09:00 INFO 18764 --- [ Test worker] c.s.o.s.StockMarketDataServiceTest : Started StockMarketDataServiceTest in 10.371 seconds (process running for 11.483)2024-01-11T00:54:00.124+09:00 DEBUG 18764 --- [ Test worker] o.s.orm.jpa.JpaTransactionManager : Creating new transaction with name [com.stock.oppenheimer.service.StockMarketDataServiceTest.testAddByStockName]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT2024-01-11T00:54:00.125+09:00 DEBUG 18764 --- [ Test worker] o.s.orm.jpa.JpaTransactionManager : Opened new EntityManager [SessionImpl(2129901484<open>)] for JPA transaction2024-01-11T00:54:00.132+09:00 DEBUG 18764 --- [ Test worker] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@7b5f886d]2024-01-11T00:54:00.606+09:00 DEBUG 18764 --- [ Test worker] o.s.orm.jpa.JpaTransactionManager : Found thread-bound EntityManager [SessionImpl(2129901484<open>)] for JPA transaction2024-01-11T00:54:00.606+09:00 DEBUG 18764 --- [ Test worker] o.s.orm.jpa.JpaTransactionManager : Participating in existing transaction2024-01-11T00:54:00.606+09:00 TRACE 18764 --- [ Test worker] o.s.t.i.TransactionInterceptor : Getting transaction for [com.stock.oppenheimer.service.StockMarketFacadeService.addStockMarketData]2024-01-11T00:54:00.607+09:00 DEBUG 18764 --- [ Test worker] o.s.orm.jpa.JpaTransactionManager : Found thread-bound EntityManager [SessionImpl(2129901484<open>)] for JPA transaction2024-01-11T00:54:00.607+09:00 DEBUG 18764 --- [ Test worker] o.s.orm.jpa.JpaTransactionManager : Participating in existing transaction2024-01-11T00:54:00.607+09:00 TRACE 18764 --- [ Test worker] o.s.t.i.TransactionInterceptor : Getting transaction for [com.stock.oppenheimer.service.StockDataService.addStockData]2024-01-11T00:54:00.628+09:00 TRACE 18764 --- [ Test worker] o.s.t.i.TransactionInterceptor : Completing transaction for [com.stock.oppenheimer.service.StockDataService.addStockData]2024-01-11T00:54:00.629+09:00 TRACE 18764 --- [ Test worker] o.s.t.i.TransactionInterceptor : Completing transaction for [com.stock.oppenheimer.service.StockMarketFacadeService.addStockMarketData]2024-01-11T00:54:02.705+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT2024-01-11T00:54:02.706+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Opened new EntityManager [SessionImpl(576234319<open>)] for JPA transaction2024-01-11T00:54:02.706+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@183cb66b]2024-01-11T00:54:02.706+09:00 TRACE 18764 --- [ctor-http-nio-2] o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]2024-01-11T00:54:02.749+09:00 TRACE 18764 --- [ctor-http-nio-2] o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]2024-01-11T00:54:02.749+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Initiating transaction commit2024-01-11T00:54:02.749+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Committing JPA transaction on EntityManager [SessionImpl(576234319<open>)]2024-01-11T00:54:02.758+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Closing JPA EntityManager [SessionImpl(576234319<open>)] after transaction2024-01-11T00:54:02.759+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Creating new transaction with name [com.stock.oppenheimer.service.MarketDataService.fetchMarketData]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT2024-01-11T00:54:02.759+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Opened new EntityManager [SessionImpl(222446599<open>)] for JPA transaction2024-01-11T00:54:02.759+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@7a4f5e33]2024-01-11T00:54:02.759+09:00 TRACE 18764 --- [ctor-http-nio-2] o.s.t.i.TransactionInterceptor : Getting transaction for [com.stock.oppenheimer.service.MarketDataService.fetchMarketData]2024-01-11T00:54:02.760+09:00 TRACE 18764 --- [ctor-http-nio-2] o.s.t.i.TransactionInterceptor : Completing transaction for [com.stock.oppenheimer.service.MarketDataService.fetchMarketData]2024-01-11T00:54:02.761+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Initiating transaction commit2024-01-11T00:54:02.761+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Committing JPA transaction on EntityManager [SessionImpl(222446599<open>)]2024-01-11T00:54:02.761+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Closing JPA EntityManager [SessionImpl(222446599<open>)] after transaction2024-01-11T00:54:03.536+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT2024-01-11T00:54:03.537+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Opened new EntityManager [SessionImpl(1744980953<open>)] for JPA transaction2024-01-11T00:54:03.537+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@364f6c3d]2024-01-11T00:54:03.537+09:00 TRACE 18764 --- [ctor-http-nio-2] o.s.t.i.TransactionInterceptor : Getting transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]2024-01-11T00:54:03.546+09:00 TRACE 18764 --- [ctor-http-nio-2] o.s.t.i.TransactionInterceptor : Completing transaction for [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]2024-01-11T00:54:03.546+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Initiating transaction commit```이러한 상황을 경험하게 되었습니다.2024-01-11T00:54:02.706+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Opened new EntityManager [SessionImpl(576234319<open>)] for JPA transaction2024-01-11T00:54:02.759+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Opened new EntityManager [SessionImpl(222446599<open>)] for JPA transaction2024-01-11T00:54:03.537+09:00 DEBUG 18764 --- [ctor-http-nio-2] o.s.orm.jpa.JpaTransactionManager : Opened new EntityManager [SessionImpl(1744980953<open>)] for JPA transaction기존에 쓰이던 엔티티매니저가 없어지고 계속 새로운 엔티티 매니저가 오픈이 되고 있었습니다. 2024-01-11T02:01:18.229+09:00 DEBUG 25524 --- [ Test worker] o.s.orm.jpa.JpaTransactionManager : Rolling back JPA transaction on EntityManager [SessionImpl(1495319789<open>)]하지만 마지막에 롤백 되는 엔티티메니저는 [Test worker thread] 에서 소환한 엔티티매니저 단 하나였습니다. 해당 부분을 소환하는 코드는 이렇습니다.. @Transactional
public Flux<MarketData> saveMarketData(MktDataDTO marketDataDTO, StockData savedStockData) {
MarketData marketData = conversionService.convert(marketDataDTO, MarketData.class);
marketData.setStockData(savedStockData);
marketDataRepository.save(marketData);
return Flux.just(marketData);
} 골이 당겨 오네요 ㅜㅜㅜ..r2dbc 로 마이그레이션을 하지 않고 transaction 설정을 잘 이용해서 이 상황을 해결할 방법이 있을지 한번 여쭤봅니다.코드 전문은 kimseunghyun-kr/oppenheimer at VolumeProfile (github.com)여기 있습니다..