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?