Search
Duplicate
โ˜•

connection ์–ป๋Š” ์‹œ์ 

โ€ข
TransacitonInterceptoor
โ—ฆ
invokeWithinTransaction
โ€ข
TAS
โ—ฆ
createTransactionIfNecessary
โ—ฆ
tm.getTransaction
โ€ข
APTM
โ—ฆ
startTransaction
โ—ฆ
doBegin
โ€ข
JTM
โ—ฆ
getJpaDialect().beginTransaction
โ€ข
AJD
โ—ฆ
entityManager.getTransaction().begin
โ€ข
TI(TransactionImpl)
โ—ฆ
this.transactionDriverControl.begin
โ€ข
LCMI(LogicalConnectionManagedImpl)
โ—ฆ
getConnectionForTransactionManagement
โ—ฆ
getPhysicalConnection
โ—ฆ
acquireConnectionIfNeeded
โ—ฆ
jdbcConnectionAccess.obtainConnection
โ€ข
HikariDataSource
โ—ฆ
getConnection

service ์— transactional ์ด ์—†์„ ์‹œ

repository ํ˜ธ์ถœํ•˜๋ฉด connection ํš๋“
2๋ฒˆ ํ˜ธ์ถœํ•˜๋ฉด ๋‘˜ ๋‹ค ์ปค๋„ฅ์…˜์ด ๋‹ค๋ฆ„ ๊ทผ๋ฐ ๊ฐ™์Œ
hikari proxy connection ์€ ๋‹ค๋ฅธ๋ฐ ๊ฒฐ๊ตญ jdbc connection ์€ ๊ฐ™์Œ!
์žˆ์œผ๋ฉด hikari proxy connection ๊นŒ์ง€ ๊ฐ™๋‹ค! ์™œ๋ƒ๋ฏ„ ์ด๋ฏธ service ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ „์— ์ปค๋„ฅ์…˜์„ ํš๋“ํ•˜๊ฑฐ๋“ !
โ†’ ์•„๋งˆ hikari proxy connection ์€ ๋…ผ๋ฆฌ์  ์ปค๋„ฅ์…˜, jdbc connection ์€ ๋ฌผ๋ฆฌ์  ์ปค๋„ฅ์…˜
1.
ํŠธ๋žœ์žญ์…˜ ๋™๊ธฐํ™”
a.
๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ธฐ์ˆ ๊ณผ ํŠธ๋žœ์žญ์…˜ ์„œ๋น„์Šค ์‚ฌ์ด ์ข…์†์„ฑ ์ œ๊ฑฐ
b.
DataSourceUtils.getConnection(dataSource);
c.
ํ›„์— ์‹ค์ œ ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์‹œ ๋ฐ›์•„์˜จ ๋™๊ธฐํ™” ๋งค๋‹ˆ์ €์— ์žˆ๋Š” connection ์ด์šฉ
d.
์—ฌ๋Ÿฌ ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ ์‹คํ–‰ ์‹œ ์›๋ž˜๋Š” ๊ฐ ๋ฉ”์„œ๋“œ๋งˆ๋‹ค ๋ฐ›์•„์˜จ connection์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„ฃ์–ด์„œ ๊ฐ™์€ connection ์ž„์„ ๋ณด์žฅํ•ด์•ผ ํ–ˆ๋Š”๋ฐ ํŠธ๋žœ์žญ์…˜ ๋™๊ธฐํ™” ๋งค๋‹ˆ์ €๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„ฃ์„ ํ•„์š” ์—†์ด connection ์„ ๊ฐ™๊ฒŒ ๋ณด์žฅ ๊ฐ€๋Šฅํ•˜๋‹ค!
2.
ํŠธ๋žœ์žญ์…˜ ์ถ”์ƒํ™”
a.
get transaction ์„ ์šฉ์ดํ•˜๊ฒŒ
b.
jdbc, jpa, hibernate ์ด ๋ชจ๋‘ ํŠธ๋žœ์žญ์…˜์„ ์–ป์–ด์˜ค๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋‹ค.
c.
ํ”Œ๋žซํผ ํŠธ๋žœ์žญ์…˜ ๋งค๋‹ˆ์ €๋กœ ์ถ”์ƒํ™”!
1.
CrudMethodMetadataPopulatingMethodInterceptor ์—์„œ TransactionSynchronizationManager.getResource(method); ํ˜ธ์ถœ
๊ทผ๋ฐ ์ฒ˜์Œ์ด๋ฉด null ์ด๋ผ bindResource() ํ˜ธ์ถœ
2.
TransactionInterceptor โ†’ PlatformTransactionManager(getTransaction()) โ†’ TSM(getResource) ์ˆœ์œผ๋กœ ํ˜ธ์ถœ
3.
transaction ์ด ์—†์œผ๋‹ˆ๊นŒ PTM(startTransaction()) ํ˜ธ์ถœ, connection ์‹œ์ž‘!
init ํ•  ๋•Œ Pool ์—์„œ JdbcConnection ์„ ์ƒ์„ฑํ•œ๋‹ค. async ๋กœ ๋„๋Š” ๊ฒƒ ๊ฐ™๋‹ค,,
init ํ•  ๋•Œ ๊ณ„์† connection ๋ถ€๋ฅธ ์ด์œ !
โ†’ select name, value from information_schema.settions where name in (?, ?, ?, ?); ์„ธํŒ… ํ™•์ธํ•˜๋ ค๊ณ !
โ†’ schema, data scripts ๋„ฃ๊ธฐ ์œ„ํ•ด!

Transaction ์ด ๋‹ค๋ฅด๋ฉด connection ์ด ๋‹ค๋ฅด๋‹ค!

โ†’ ๋ฐ˜์€ ๋งž๊ณ  ๋ฐ˜์€ ํ‹€๋ฆฌ๋‹ค.
connection ์ด ๋ฐ˜ํ™˜์ด ์ด๋ฏธ ๋˜์—ˆ์œผ๋ฉด ๋˜ ๋˜‘๊ฐ™์€ connection ์„ ์‚ฌ์šฉํ•œ๋‹ค.
// @Transactional - service ๋ฉ”์„œ๋“œ์— traction ์—†๋Š” ์ƒํƒœ public void test() { memberRepository.save(); // connection ์“ฐ๊ณ  ๋ฐ˜ํ™˜ memberRepository.save(); // ์œ„์™€ ๋˜‘๊ฐ™์€ connection ์“ฐ๊ณ  ๋ฐ˜ํ™˜ }
Java
๋ณต์‚ฌ
์ด๊ฒŒ connection pool ์ด list ๋กœ ๋˜์–ด์žˆ๊ธด ํ•จ. ๊ทธ๋ž˜์„œ ๊ทธ๋Ÿฐ๊ฑด์ง€ connection ๊ฐ€์ ธ์˜ค๋Š” ์‹œ์ ์„ ํ™•์ธํ•ด๋ณด์ž
์•„๋‹ˆ๋ฉด ๋˜‘๊ฐ™์€ ์“ฐ๋ ˆ๋“œ๋ผ์„œ ๊ทธ๋Ÿด์ง€๋„?
์ƒˆ๋กœ์šด Transaction ์ด๋ฉด ๋‹ค๋ฅธ connection ์„ ์‚ฌ์šฉํ•œ๋‹ค.
@Transactional public void test() { memberRepository.save(); anotherService.test2(); } @Transactional(propagation = Propagation.REQUIRES_NEW) public void test2() {};
Java
๋ณต์‚ฌ
์•„์ง test() ์—์„œ connection ์ด ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์€ ์ƒํƒœ, REQUIRES_NEW ๋กœ ์ƒˆ๋กœ์šด ํŠธ๋žœ์žญ์…˜์„ ์—ด๋ฉด connection ์ด ๋‹ค๋ฅด๋‹ค.