|
| 1 | +package com.wafflestudio.seminar.spring2023.week2 |
| 2 | + |
| 3 | +import com.wafflestudio.seminar.spring2023.QueryCounter |
| 4 | +import com.wafflestudio.seminar.spring2023.QueryCounter.Result |
| 5 | +import com.wafflestudio.seminar.spring2023.playlist.repository.PlaylistRepository |
| 6 | +import com.wafflestudio.seminar.spring2023.song.repository.AlbumRepository |
| 7 | +import org.assertj.core.api.Assertions.assertThat |
| 8 | +import org.hibernate.LazyInitializationException |
| 9 | +import org.junit.jupiter.api.Test |
| 10 | +import org.junit.jupiter.api.assertDoesNotThrow |
| 11 | +import org.junit.jupiter.api.assertThrows |
| 12 | +import org.springframework.beans.factory.annotation.Autowired |
| 13 | +import org.springframework.boot.test.context.SpringBootTest |
| 14 | +import org.springframework.transaction.annotation.Transactional |
| 15 | +import java.util.concurrent.Executors |
| 16 | + |
| 17 | +@SpringBootTest |
| 18 | +class PersistenceContextTest @Autowired constructor( |
| 19 | + private val albumRepository: AlbumRepository, |
| 20 | + private val playlistRepository: PlaylistRepository, |
| 21 | + private val queryCounter: QueryCounter, |
| 22 | +) { |
| 23 | + private val threadPool = Executors.newFixedThreadPool(4) |
| 24 | + |
| 25 | + @Test |
| 26 | + fun `영속성 컨텍스트 없이 지연로딩`() { |
| 27 | + val album = albumRepository.findById(1L).get() |
| 28 | + |
| 29 | + assertThrows<LazyInitializationException> { |
| 30 | + println(album.artist.name) |
| 31 | + } |
| 32 | + |
| 33 | + val (_, queryCount) = queryCounter.count { |
| 34 | + albumRepository.findById(1L).get() |
| 35 | + } |
| 36 | + |
| 37 | + // 영속성 컨텍스트가 없기 때문에 캐싱 되지 않음. |
| 38 | + assertThat(queryCount).isEqualTo(1) |
| 39 | + } |
| 40 | + |
| 41 | + @Transactional // 기본적으로 Transactional이 영속성 컨텍스트 생존 범위 |
| 42 | + @Test |
| 43 | + fun `영속성 컨텍스트 안에서 지연로딩`() { |
| 44 | + val album = albumRepository.findById(1L).get() |
| 45 | + |
| 46 | + assertDoesNotThrow { |
| 47 | + println(album.artist.name) |
| 48 | + } |
| 49 | + |
| 50 | + val (_, queryCount) = queryCounter.count { |
| 51 | + albumRepository.findById(1L).get() |
| 52 | + } |
| 53 | + |
| 54 | + // 영속성 컨텍스트가 있기 때문에 캐싱됨. |
| 55 | + assertThat(queryCount).isEqualTo(0) |
| 56 | + } |
| 57 | + |
| 58 | + @Transactional // 기본적으로 Transactional이 영속성 컨텍스트 생존 범위, 그러나 스레드 간에 영속성 컨텍스트를 공유하지 않는다. |
| 59 | + @Test |
| 60 | + fun `스레드 단위의 엔티티 캐싱`() { |
| 61 | + // 첫 번째 쿼리: DB 조회 후 영속성 컨텍스트에 캐시 |
| 62 | + val (threadName, queryCount) = queryCounter.count { |
| 63 | + playlistRepository.findById(1L).get() |
| 64 | + |
| 65 | + Thread.currentThread().name |
| 66 | + } |
| 67 | + |
| 68 | + println("First query executed by Thread($threadName)") |
| 69 | + |
| 70 | + assertThat(queryCount).isEqualTo(1) |
| 71 | + |
| 72 | + // 두 번째 쿼리: 첫 번째 쿼리와 같은 스레드에서 발생. 캐싱된 데이터 조회 |
| 73 | + val (threadName2, queryCount2) = queryCounter.count { |
| 74 | + playlistRepository.findById(1L).get() |
| 75 | + |
| 76 | + Thread.currentThread().name |
| 77 | + } |
| 78 | + |
| 79 | + println("Second query executed by Thread($threadName2)") |
| 80 | + |
| 81 | + assertThat(queryCount2).isEqualTo(0) |
| 82 | + |
| 83 | + // 세 번째 쿼리: 이전 쿼리들과 다른 스레드에서 발생. 즉, 다른 영속성 컨텍스트를 갖고 있기 때문에 DB 조회. |
| 84 | + val taskByOtherThread = threadPool.submit<Result<String>> { |
| 85 | + queryCounter.count { |
| 86 | + playlistRepository.findById(1L).get() |
| 87 | + |
| 88 | + Thread.currentThread().name |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + val (threadName3, queryCount3) = taskByOtherThread.get() |
| 93 | + |
| 94 | + println("Third query executed by Thread($threadName3)") |
| 95 | + |
| 96 | + assertThat(queryCount3).isEqualTo(1) |
| 97 | + } |
| 98 | +} |
0 commit comments