hibernate-architecture

#hibernate

  • ๋“ค์–ด๊ฐ€๋ฉด์„œ..

  • Spring 5, Hibernate 5, HikariCP ์‚ฌ์šฉ ์ค‘.

  • ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ์˜ ์•„ํ‚คํ…์ณ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ž, ๋งŒ์•ฝ์— A- TransactinoManager๊ฐ€ ์ง„ํ–‰ ์ค‘์ธ ํŠธ๋žœ์žญ์…˜ ์ค‘์— B- SessionFactory๋กœ switch ํ•œ๋‹ค๋ฉด, ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

    • ์—๋Ÿฌ? ์„ฑ๊ณต?..

Architecture

image

SessionFactory

  • Session ์„ ์ƒ์„ฑํ•˜๋Š” ๊ณต์žฅ, (๊ทธ๋งŒํผ ์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ๋งŽ์€ ์ž์›์ด ์†Œ๋ชจ๋˜๋Š” ๋“ฏ.)

  • Thread-safe

    • ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ์—์„œ ์ ‘๊ทผํ•ด๋„, ๋‹ค๋ฅธ ์„ธ์…˜์„ ๊ฑด๋„ค์ค„๋“ฏ.

  • ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๋งค์นญ๋˜๋Š” ๋“ฏ.

Session

  • ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ์— ๋Œ€์‘๋˜๊ณ , ์งฆ์€ ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ๊ฐ€์กŒ์œผ๋ฉฐ, persistant layer์™€ application layer์™€์˜ ์†Œํ†ต์„ ์˜๋ฏธํ•˜๋Š” ๊ฐ์ฒด

  • JDBC connection์„ ๊ฐ์‹ธ๊ณ  ์žˆ๊ณ  ์žˆ์Œ.

  • Not thread-safe

  • Transaction

Transaction

A single-threaded, short-lived object used by the application to specify atomic units of work.

  • atomicํ•œ ์ž‘์—…์„ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” single threaded, short-livedํ•œ ๊ฐ์ฒด

  • JDBC, JTA or CORBA transaction ๋กœ ๋ถ€ํ„ฐ ์ถ”์ƒํ™”๋˜์–ด์žˆ์Œ.

  • ํ•˜๋‚˜์˜ Session์€ ์—ฌ๋Ÿฌ๊ฐœ์— Transaction์— ๊ฑธ์ณ์žˆ์„ ์ˆ˜ ์žˆ์Œ.

HibernateTransactionManager

  • ํ•˜๋‚˜์˜ ํ•˜์ด๋ฒ„๋„คํ‹ฐํŠธ ์„ธ์…˜ ํŒฉํ† ๋ฆฌ๋ฅผ ์œ„ํ•œ PlatformTransactionManager ๊ตฌํ˜„์ฒด

  • ํŠน์ • factory์—์„œ thread์— ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ์„ธ์…˜์„ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š”๊ฑด, ์ž ์žฌ์ ์œผ๋กœ ํ•˜๋‚˜์˜ ์„ธ์…˜์— ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๋ผ๋Š” ๊ฑธ ํ—ˆ์šฉํ•˜๋Š”๊ฑฐ๋‚˜ ๋‹ค๋ฆ„์—†๋Š”๋“ฏ.

  • SessionFactory.getCurrentSession() ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ฃผ์„์— ๋ช…์„ธ๋˜์–ด์žˆ์Œ.

Obtains the current session. The definition of what exactly "current" means controlled by the CurrentSessionContext impl configured for use.

  • ์ฆ‰ Current session์„ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ๊ฒƒ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ Current๋ž€, CurrentSessionContext๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌํ˜„์ฒด์—์„œ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋Š” ์„ธ์…˜์„ ์˜๋ฏธ.

  • Spring์—์„œ๋Š” SpringSessionContext๋ผ๋Š” ๊ตฌํ˜„์ฒด๊ฐ€ ์žˆ๊ณ , SpringSessionContext ๋ฅผ ๋ณด๋ฉด ์ƒ์„ฑ์ž์— ๊ทธ๋ž˜์„œ SessionFactoryImplementor ์ด ์žˆ์Œ.

  • ์ •๋ฆฌํ•˜์ž๋ฉด Spring์—์„œ๋Š” ํ˜„์žฌ ์Šค๋ ˆ๋“œ์— ๋งค์นญ๋˜๋Š” SessionFactory๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๊ณ , ์—ฌ๊ธฐ์„œ ์„ธ์…˜์ด ํ•„์š”ํ•˜๋ฉด Session์„ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ๋ฐฉ์‹

  • ๊ทธ๋ฆฌ๊ณ  TransactionSynchronizationManager ์—์„œ ์ง„ํ–‰๋˜๊ณ  ์žˆ๋Š” ํŠธ๋žœ์žญ์…˜์—์„œ ์ž์› ๋™๊ธฐํ™” (Session..) ์ด ๋˜์–ด์žˆ๋Š”๊ฐ€ ํ™•์ธ๋„ ํ•˜๋Š”๋“ฏ.

  • ์ปค์Šคํ…€ isolation level์„ ์ง€์›ํ•˜๊ณ , timoeout option๋„ ์ ์šฉํ•˜๋Š”๋“ฏ.

  • ์ด ํŠธ๋žœ์žญ์…˜ ๋งค๋‹ˆ์ €๋Š” (HibernateTransactionManage) ๋Š” ํŠธ๋žœ์žญ์…˜ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ์—, ํ•˜๋‚˜์˜ ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ์„ธ์…˜ ํŒฉํ† ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ ํ•ฉํ•จ.

  • ๊ทธ๋Ÿฌ๋‚˜ Datasource์— ์ง์ ‘์ ์ธ ์ ‘๊ทผ๋„ ์ง€์›ํ•จ.

  • ์ •๋ฆฌํ•˜์ž๋ฉด HibernateTransactionManager ๋Š” SessionFactory๋ฅผ ๊ฐ€์ง€๊ณ  Session์„ ํ•ธ๋“ค๋งํ•˜๋ฉด์„œ, ์›์ž์ ์ธ ์ž‘์—… ๋‹จ์œ„์ธ Transaction์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค.

LifeCycle

  1. doGetTransaction()

  • ํ˜„์žฌ ํŠธ๋žœ์žญ์…˜ ์ƒํƒœ์— ๋Œ€ํ•œ ํŠธ๋žœ์žญ์…˜ ๊ฐœ์ฒด๋ฅผ ๋ฐ˜ํ™˜

  • ๋ฐ˜ํ™˜๋œ ๊ฐœ์ฒด์—๋Š” ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ์ž์—์„œ ํ˜„์žฌ getTransaction ํ˜ธ์ถœ ์ „์— ์ด๋ฏธ ์‹œ์ž‘๋œ ํŠธ๋žœ์žญ์…˜์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Œ.

    • ์ฆ‰ ์ด ์ „์— ์‹œ์ž‘ํ•œ ํŠธ๋žœ์žญ์…˜ ๊ฐ์ฒด ๊ฐ€ ์žˆ์œผ๋ฉด ๊ทธ๊ฑธ ๋ฐ˜ํ™˜ํ• ๋“ฏ.

    • ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด๋Š” SessionHolder

  1. doBegin()

This method gets called when the transaction manager has decided to actually start a new transaction. Either there wasn't any transaction before, or the previous transaction has been suspended.

  • ์ด์ „์˜ ํŠธ๋žœ์žญ์…˜ ๊ฐ์ฒด๊ฐ€ ์—†์—ˆ์„ ๊ฒฝ์šฐ์—๋งŒ ํ˜ธ์ถœ๋จ. ์ฆ‰ ์ง„์งœ ์ƒˆ๋กœ์šด ํŠธ๋žœ์žญ์…˜์ผ ๋•Œ๋งŒ

  • ํ˜น์€ ์ด์ „ ํŠธ๋žœ์žญ์…˜์ด ์ค‘๋‹จ๋˜์—ˆ์„ ๊ฒฝ์šฐ์— ํ˜ธ์ถœ๋˜๊ธฐ๋„ํ•จ.

  1. doSuspend()

  • ํ˜„์žฌ ํŠธ๋žœ์žญ์…˜์„ ์ค‘๋‹จ.

  1. doResume()

  • ํ˜„์žฌ ํŠธ๋žœ์žญ์…˜์„ ์žฌ์‹œ์ž‘.

SessionFactory

  • Session getCurrentSession()

  • Obtains the current session. The definition of what exactly "current" means controlled by the org.hibernate.context.spi.CurrentSessionContext impl configured for use.

    • current session์€ CurrentSessionContext์—์„œ ๊ด€๋ฆฌ๋˜๋Š” ์„ธ์…˜์„ ์˜๋ฏธ.

    • ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด ์ค‘์—, SpringSessionContext ๊ฐ€ ์žˆ์Œ.

    • ์•„๋งˆ๋„ ์Šคํ”„๋ง์—์„œ ์„ธ์…˜์€ ์ด ๊ตฌํ˜„์ฒด๋ฅผ ํ†ตํ•ด ๊ด€๋ฆฌ๋˜๋Š” ๋“ฏ.

SpringSessionContext

  • Session getCurrentSession()

  • Retrieve the Spring-managed Session for the current thread, if any.

    • ํ˜„์žฌ ์Šค๋ ˆ๋“œ์—์„œ ์Šคํ”„๋ง์ด ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š” ์„ธ์…˜์ด ์žˆ๋‹ค๋ฉด, ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ.

    • ์Šคํ”„๋ง์ด ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š”์ง€๋Š”.. TransactionSynchronizationManager.getResource()์—์„œ ํŒ๋‹จ๋˜๋Š” ๋“ฏ.

TransactionSynchronizationManager

Central delegate that manages resources and transaction synchronizations per thread. To be used by resource management code but not by typical application code.

  • ํ˜„์žฌ ์ง„ํ–‰๋˜๋Š” ํŠธ๋žœ์žญ์…˜์„ ๊ด€๋ฆฌํ•˜๋Š” ๋งค๋‹ˆ์ € ์ธ๋“ฏ

  • ์Šค๋ ˆ๋“œ ๋ณ„๋กœ ์žˆ๋Š” ๋“ฏ.

Reference

Last updated

Was this helpful?