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?