Persistence context

  • EntityManager ์ธ์Šคํ„ด์Šค์™€ ์—ฐ๊ด€ ์žˆ์Œ.

  • entity instances๋“ค์˜ ์ง‘ํ•ฉ์ด๋ผ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ์„๋“ฏ.

    • ๊ทธ๋ฆฌ๊ณ  ์ด entity๋“ค์€ uniqueํ•˜๊ฒŒ ๊ตฌ๋ถ„๋  ์ˆ˜ ์žˆ์Œ.

  • ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ ๋‚ด๋ถ€์—์„œ๋Š”, entity๋“ค์˜ lifeCycle์˜ ๊ด€๋ฆฌ๋จ.

  • ์ •๋ฆฌํ•˜์ž๋ฉด..

The persistence context is the first-level cache where all the entities are fetched from the database or saved to the database.

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ˜น์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์ €์žฅํ•˜๊ณ ์žํ•˜๋Š” ๋ชจ๋“  ์—”ํ‹ฐํ‹ฐ๋“ค์ด ์กด์žฌํ•˜๋Š” 1์ฐจ ์บ์‹œ๋ผ ํ•  ์ˆ˜ ์žˆ์Œ.

  • application - persistence context - database

    • application๊ณผ database ์‚ฌ์ด์— ์กด์žฌ.

  • ๊ทธ๋ฆฌ๊ณ  ๋‚ด๋ถ€์— ์กด์žฌํ•˜๋Š” entity๋“ค์˜ ๋ณ€ํ™”๋ฅผ ๊ด€๋ฆฌํ•จ.

  • ๋งŒ์•ฝ์— ํŠธ๋žœ์žญ์…˜ ์ค‘์—, ์—”ํ‹ฐํ‹ฐ๋“ค์˜ ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚œ๋‹ค๋ฉด, ํ•ด๋‹น entity๋Š” dirty ์ƒํƒœ๋กœ marked ๋จ.

  • ๊ทธ๋ฆฌ๊ณ  ํŠธ๋žœ์žญ์…˜์ด ๋๋‚œ๋‹ค๋ฉด (commit ์‹œ์—..), ํ•ด๋‹น ๋ณ€๊ฒฝ๋“ค์ด Database๋กœ flush๋จ

Persistence Context Type

  • persistence context๊ฐ€ ์–ด๋–ค scope๋‚ด์— ์กด์žฌ ํ• ์ง€์— ๋Œ€ํ•œ ์„ค์ •.

  • default๋Š” Transaction-scoped ์ธ ๋“ฏ.

Transaction-Scoped Persistence Context

  • ์˜์†์„ฑ ์ฝ˜ํ…์ŠคํŠธ๋Š”, ํŠธ๋žœ์žญ์…˜ ๋‚ด๋ถ€์— ์กด์žฌํ•˜๋Š” ๊ฒƒ.

  • ํŠธ๋žœ์žญ์…˜ ์ง„ํ–‰ ์‹œ์—, ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๊ฐ€ ์žˆ๋Š” ์ง€ ํ™•์ธํ•ด์„œ, ์žˆ์œผ๋ฉด ์žˆ๋Š” ๊ฑธ ์“ฐ๊ณ  ์—†์œผ๋ฉด ์ƒˆ๋กœ ๋งŒ๋“ฌ.

  • ํŠธ๋žœ์žญ์…˜ ๋‚ด๋ถ€์—์„œ ์˜์†์„ฑ ์ฝ˜ํ…์ŠคํŠธ์— ์†ํ•˜๋Š” ์—”ํ‹ฐํ‹ฐ์˜ ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚  ๊ฒฝ์šฐ, ์ด๊ฑธ ๊ฐ์ง€ํ•˜๊ณ  ์ถ”ํ›„ ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹์‹œ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ flushํ•จ

Extended-Scoped Persistence Context

  • ์—ฌ๋Ÿฌ๊ฐœ์˜ ํŠธ๋žœ์žญ์…˜์—์„œ ํ•˜๋‚˜์˜ ์˜์†์„ฑ ์ฝ˜ํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ.

  • entity persist๋Š” ํŠธ๋žœ์žญ์…˜ ์—†์ด ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, (entity๋ฅผ persistence context์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ..)

  • flush๋Š” ํŠธ๋žœ์žญ์…˜ ์—†์ด ํ•  ์ˆ˜ ์—†์Œ.

    • ์ฆ‰ ์—ฌ๋Ÿฌ๊ฐœ ํŠธ๋žœ์žญ์…˜์—์„œ ํ•˜๋‚˜์˜ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๊ฐ€ ๊ณต์œ ๋˜๋Š” ๋งŒํผ, persist ๊ฐ™์€ ์•ก์…˜์— ํŠธ๋žœ์žญ์…˜ ์กด์žฌ ์—ฌ๋ถ€๊ฐ€ ์ฒดํฌ ๋˜์ง„ ์•Š์ง€๋งŒ, database ๋กœ flush ํ•˜๋Š” ์•ก์…˜์—๋Š” ํŠธ๋žœ์žญ์…˜์ด ํ•„์ˆ˜ ์ธ๋“ฏ.

์ฐธ๊ณ 

Last updated