@Transactional
@Transactional ์ด๋?
@Transactional์ ํด๋์ค๋ ๋ฉ์๋์ ๋ถ์ฌ์ค ๊ฒฝ์ฐ, ํด๋น ๋ฒ์ ๋ด ๋ฉ์๋๊ฐ ํธ๋์ญ์ ์ด ๋๋๋ก ๋ณด์ฅํด์ค๋ค
์ ์ธ์ ํธ๋์ญ์ ์ด๋ผ๊ณ ๋ ํ๋๋ฐ, ์ง์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ํ์ ์์ด ์ ์ธ๋ง์ผ๋ก๋ ๊ด๋ฆฌ๋ฅผ ์ฉ์ดํ๊ฒ ํด์ฃผ๊ธฐ ๋๋ฌธ์ด๋ค
ํนํ๋ SpringBoot์์๋ ์ ์ธ์ ํธ๋์ญ์ ์ ํ์ํ ์ฌ๋ฌ ์ค์ ์ด ์ด๋ฏธ ๋์ด์์ด ๋ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋ค
@Transactional(readOnly=true)๊ฐ ๊ผญ ํ์ํ๊ฐ?
์จ์ฃผ๋๊ฒ ์ข๋ค!
why?
์กฐํํ ๋ฐ์ดํฐ๋ฅผ return ํ๋ค๊ณ ํด๋
์๋์น ์๊ฒ ๋ฐ์ดํฐ๊ฐ ๋ณ๊ฒฝ๋๋ ์ผ
์ ์ฌ์ ์ ๋ฐฉ์งํด์ค๋ค์์์น ๋ชปํ Entity์ ๋ฑ๋ก, ๋ณ๊ฒฝ, ์ญ์ ์๋ฐฉ
Entity๋ฅผ
์ฝ๊ธฐ ์ ์ฉ
์ผ๋ก ์กฐํ ์, ๋ณ๊ฒฝ ๊ฐ์ง๋ฅผ ์ํ snapshot์ ์ ์งํ์ง ์์๋ ๋๊ณ , ์์์ฑ ์ปจํ ์คํธ๋ฅผ flush ํ์ง ์์๋ ๋ผ ์ฑ๋ฅ์ ์ต์ ํ ํ ์ ์๋คํธ๋์ญ์ ์
readOnly=true
์ต์ ์ ์ฃผ๋ฉด ์คํ๋ง ํ๋ ์์ํฌ๊ฐ Hibernate session flush mode๋ฅผ MANUAL๋ก ์ค์ ํ๋ค์ด๋ ๊ฒ ํ๋ฉด ๊ฐ์ ๋ก ํ๋ฌ์๋ฅผ ํธ์ถํ์ง ์๋ ํ ํ๋ฌ์๊ฐ ์ผ์ด๋์ง ์๋๋ค!
๋ฐ๋ผ์ ํธ๋์ญ์ ์ ์ปค๋ฐํ๋๋ผ๋ ์์์ฑ ์ปจํ ์คํธ๊ฐ ํ๋ฌ์ ๋์ง ์์์ ์ํฐํฐ์ ๋ฑ๋ก, ์์ , ์ญ์ ์ด ๋์ํ์ง ์๋๋ค
๋ํ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ์์์ฑ ์ปจํ ์คํธ๋ ๋ณ๊ฒฝ ๊ฐ์ง๋ฅผ ์ํ ์ค๋ ์ท์ ๋ณด๊ดํ์ง ์์ผ๋ฏ๋ก ์ฑ๋ฅ์ด ํฅ์๋๋ค
ํด๋น ์ต์ ์ธ ๊ฒฝ์ฐ CUD ์์ ์ด ๋์ํ์ง ์๊ณ , ์ค๋ ์ท ์ ์ฅ, ๋ณ๊ฒฝ ๊ฐ์ง(dirty check)์ ์์ ์ ์ํํ์ง ์์ ์ฑ๋ฅ์ด ํฅ์๋๋ค
Dirty check
์ด๋?์ํ ๋ณ๊ฒฝ๊ฒ์ฌ
์์์ฑ ์ปจํ ์คํธ์ ์ฐ๊ฒฐ๋จ
JPA์์๋ ํธ๋์ญ์ ์ด ๋๋๋ ์์ ์ ๋ณํ๊ฐ ์๋ ๋ชจ๋ ์ํฐํฐ ๊ฐ์ฒด๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋์ผ๋ก ๋ฐ์ํด์ค๋ค
JPA์์๋ ์ํฐํฐ๋ฅผ ์กฐํํ๋ฉด ํด๋น ์ํฐํฐ์ ์ฒ์ ์กฐํ ์ํ ๊ทธ๋๋ก ์ค๋ ์ท์ ๋ง๋ค์ด ๋๋๋ค
๊ทธ๋ฆฌ๊ณ ํธ๋์ญ์ ์ด ๋๋๋ ์์ ์๋ ์ด ์ค๋ ์ท๊ณผ ๋น๊ตํด์ ๋ค๋ฅธ ์ ์ด ์๋ค๋ฉด Update Query๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก ์ ๋ฌํ๋ค
MySQL
์ด์คํ ๊ตฌ์ฑ(Master - Slave)
์ DB๊ฐ master์ slave๋ก ๋๋์ด์ ธ ์๋ค๋ฉด readOnly = true๋ก ์๋ ๊ฒฝ์ฐ์๋ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก master๊ฐ ์๋ slave๋ฅผ ํธ์ถํ๊ฒ ๋๋ค์ํฉ์ ๋ฐ๋ผ DB ์๋ฒ์ ๋ถํ๋ฅผ ์ค์ด๊ณ ์ฝ๊ฐ์ ์ต์ ํ๋ฅผ ํ ์ ์๋ค
@Transactional(readOnly=true) annotation ์๋ค๋ฉด ์ฝ๋๋ฅผ ์ ํ๋ ์ฌ๋๋ค์ด ์ง๊ด์ ์ผ๋ก ๋ณด๊ธฐ์ ํด๋น ๋ฉ์๋๋ READ์ ๋ํ ๋์๋ง ์ํํ ๊ฒ์ด๋ผ๊ณ ์์ํ ์ ์๋ค
Spring @Transactional vs javax @Transactional
javax @Transactional
์๋ฐ ํ์ค ํ์ฅ ํจํค์ง
์๋ฐ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณต
Spring @Transactional
org.springframework์์ ์ ๊ณต
Spring Container๊ฐ ๊ด๋ฆฌํด์ค๋ค
org.springframework.transaction.annotation.Transactional
์ ์ฌ์ฉํด์ผ ํ๋ ์ด์๋ง์ Option๋ค์ ์ ๊ณตํ๋ค
isolation
: the underlying database isolation levelnoRollbackFor
andnoRollbackForClassName
: the list of JavaException
classes that can be triggered without triggering a transaction rollbackrollbackFor
androllbackForClassName
: the list of JavaException
classes that trigger a transaction rollback when being thrownpropagation
: the transaction propagation type given by the[Propagation](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html)
Enum. For instance, if the transaction context can be inherited (e.g.,REQUIRED
) or a new transaction context should be created (e.g.,REQUIRES_NEW
) or if an exception should be thrown if no transaction context is present (e.g.,MANDATORY
) or if an exception should be thrown if a current transaction context is found (e.g.,NOT_SUPPORTED
).readOnly
: whether the current transaction should only read data without applying any changes.timeout
: how many seconds should the transaction context be allowed to run until a timeout exception is thrown.value
ortransactionManager
: the name of the SpringTransactionManager
bean to be used when binding the transaction context.
Last updated
Was this helpful?