hibernate-architecture
#hibernate
๋ค์ด๊ฐ๋ฉด์..
Spring 5, Hibernate 5, HikariCP ์ฌ์ฉ ์ค.
ํ์ฌ ํ๋ก์ ํธ ํ๊ฒฝ์ ์ํคํ ์ณ๊ฐ ๋ค์๊ณผ ๊ฐ๋ค๊ณ ์๊ฐํ์, ๋ง์ฝ์ A- TransactinoManager๊ฐ ์งํ ์ค์ธ ํธ๋์ญ์ ์ค์ B- SessionFactory๋ก switch ํ๋ค๋ฉด, ์ด๋ป๊ฒ ๋ ๊น?
์๋ฌ? ์ฑ๊ณต?..
Architecture

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
doGetTransaction()
ํ์ฌ ํธ๋์ญ์ ์ํ์ ๋ํ ํธ๋์ญ์ ๊ฐ์ฒด๋ฅผ ๋ฐํ
๋ฐํ๋ ๊ฐ์ฒด์๋ ํธ๋์ญ์ ๊ด๋ฆฌ์์์ ํ์ฌ getTransaction ํธ์ถ ์ ์ ์ด๋ฏธ ์์๋ ํธ๋์ญ์ ์ ๋ํ ์ ๋ณด๊ฐ ํฌํจ๋์ด ์์.
์ฆ ์ด ์ ์ ์์ํ ํธ๋์ญ์ ๊ฐ์ฒด ๊ฐ ์์ผ๋ฉด ๊ทธ๊ฑธ ๋ฐํํ ๋ฏ.
๋ฐํ๋๋ ๊ฐ์ฒด๋
SessionHolder
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.
์ด์ ์ ํธ๋์ญ์ ๊ฐ์ฒด๊ฐ ์์์ ๊ฒฝ์ฐ์๋ง ํธ์ถ๋จ. ์ฆ ์ง์ง ์๋ก์ด ํธ๋์ญ์ ์ผ ๋๋ง
ํน์ ์ด์ ํธ๋์ญ์ ์ด ์ค๋จ๋์์ ๊ฒฝ์ฐ์ ํธ์ถ๋๊ธฐ๋ํจ.
doSuspend()
ํ์ฌ ํธ๋์ญ์ ์ ์ค๋จ.
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?