Skip to content

Latest commit

ย 

History

History
84 lines (65 loc) ยท 4.08 KB

README.md

File metadata and controls

84 lines (65 loc) ยท 4.08 KB

ํŠธ๋žœ์žญ์…˜์ด๋ž€ ์—ฌ๋Ÿฌ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋‹ค๊ฐ€ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ์„ ๊ฒฝ์šฐ ์ด์ „ ์ƒํƒœ๋กœ ๋กค๋ฐฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
๋” ์ด์ƒ ์ชผ๊ฐค ์ˆ˜ ์—†๋Š” ์ตœ์†Œ ์ž‘์—… ๋‹จ์œ„์ด๋ฉฐ, commit์œผ๋กœ ์„ฑ๊ณตํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” rollback์œผ๋กœ ์‹คํŒจ ์ดํ›„ ์ทจ์†Œ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก Transaction ACID

- ์›์ž์„ฑ Atomicity: ํŠธ๋žœ์žญ์…˜ ๋‚ด์˜ ์ž‘์—…๋“ค์€ ๋ชจ๋‘ ์„ฑ๊ณต ๋˜๋Š” ๋ชจ๋‘ ์‹คํŒจํ•œ๋‹ค.
- ์ผ๊ด€์„ฑ Consistency: ๋ชจ๋“  ํŠธ๋žœ์žญ์…˜์€ ์ผ๊ด€์„ฑ ์žˆ๋Š” DB ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•œ๋‹ค. (ex: DB์˜ ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ ์กฐ๊ฑด ํ•ญ์ƒ ๋งŒ์กฑ)
- ๊ฒฉ๋ฆฌ์„ฑ Isolation: ๋™์‹œ์— ์‹คํ–‰๋˜๋Š” ํŠธ๋žœ์žญ์…˜๋“ค์€ ์„œ๋กœ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค. (ex: ๋™์‹œ์— ๊ฐ™์€ ๋ฐ์ดํ„ฐ ์ˆ˜์ • X)
- ์ง€์†์„ฑ Durability: ํŠธ๋žœ์žญ์…˜์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋๋‚˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋Š” ํ•ญ์ƒ ๊ธฐ๋ก๋˜์–ด์•ผ ํ•œ๋‹ค.

์Šคํ”„๋ง์€ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ จ 3๊ฐ€์ง€ ํ•ต์‹ฌ ๊ธฐ์ˆ ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

1. ํŠธ๋žœ์žญ์…˜ ๋™๊ธฐํ™”

ํŠธ๋žœ์žญ์…˜ ๋™๊ธฐํ™”๋ž€ ํŠธ๋žœ์žญ์…˜์„ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ Connection ๊ฐ์ฒด๋ฅผ ํŠน๋ณ„ํ•œ ์ €์žฅ์†Œ์— ๋ณด๊ด€ํ•ด๋‘๊ณ  ํ•„์š”ํ•  ๋•Œ ๊บผ๋‚ด์“ธ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.
JDBC๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž‘์—…์„ ํ•˜๋‚˜์˜ ํŠธ๋žœ์žญ์…˜์œผ๋กœ ๊ด€๋ฆฌํ•˜๋ ค๋ฉด Connection ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋“ฑ ๋ถˆํ•„์š”ํ•œ ์ž‘์—…๋“ค์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๋™๊ธฐํ™” ์ €์žฅ์†Œ๋Š” ์ž‘์—… ์Šค๋ ˆ๋“œ๋งˆ๋‹ค Connection ๊ฐ์ฒด๋ฅผ ๋…๋ฆฝ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ , ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ๋„ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

// ๋™๊ธฐํ™” ์‹œ์ž‘
TransactionSynchronizeManager.initSynchronization();
Connection c = DataSourceUtils.getConnection(dataSource);
... // ์ž‘์—… ์ง„ํ–‰
// ๋™๊ธฐํ™” ์ข…๋ฃŒ
DataSourceUtils.releaseConnection(c, dataSource);
TransactionSynchronizeManager.unbindResource(dataSource);
TransactionSynchronizeManager.clearSynchronization();

ํ•˜์ง€๋งŒ JDBC๊ฐ€ ์•„๋‹Œ Hibernate์™€ ๊ฐ™์€ ๊ธฐ์ˆ ์„ ์“ด๋‹ค๋ฉด

2. ํŠธ๋žœ์žญ์…˜ ์ถ”์ƒํ™”

Spring์€ ํŠธ๋žœ์žญ์…˜ ๊ธฐ์ˆ ์˜ ๊ณตํ†ต์ ์„ ๋‹ด์€ ํŠธ๋žœ์žญ์…˜ ์ถ”์ƒํ™” ๊ธฐ์ˆ ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋ฅผ ์ด์šฉํ•จ์œผ๋กœ์จ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๊ฐ ๊ธฐ์ˆ ๋งˆ๋‹ค(JDBC, JPA, Hibernate ๋“ฑ) ์ข…์†์ ์ธ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ์ผ๊ด€๋˜๊ฒŒ ํŠธ๋žœ์žญ์…˜์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


Spring์ด ์ œ๊ณตํ•˜๋Š” ํŠธ๋žœ์žญ์…˜ ๊ฒฝ๊ณ„ ์„ค์ •์„ ์œ„ํ•œ ์ถ”์ƒ ์ธํ„ฐํŽ˜์ด์Šค๋Š” PlatformTransactionManager์ž…๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ๋งŒ์•ฝ JDBC์˜ ๋กœ์ปฌ ํŠธ๋žœ์žญ์…˜์„ ์ด์šฉํ•œ๋‹ค๋ฉด DataSourceTxManager๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์ด๋กœ์จ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์ˆ ๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ PlatformTransactionManager๋ฅผ ํ†ตํ•ด ๋‹ค์Œ์˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ํŠธ๋žœ์žญ์…˜์„ ๊ณต์œ ํ•˜๊ณ , ์ปค๋ฐ‹ํ•˜๊ณ , ๋กค๋ฐฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

public Object invoke(MethodInvoation invoation) throws Throwable {
	TransactionStatus status = this.transactionManager.getTransaction(new DefaultTransactionDefinition());

	try {
		Object ret = invoation.proceed();
		this.transactionManager.commit(status);
		return ret;
	} catch (Exception e) {
		this.transactionManager.rollback(status);
		throw e;
	}
}

3. AOP๋ฅผ ์ด์šฉํ•œ ํŠธ๋žœ์žญ์…˜ ๋ถ„๋ฆฌ

Spring์—์„œ๋Š” ๋งˆ์น˜ ํŠธ๋žœ์žญ์…˜ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ๋ณด์ด๊ธฐ ์œ„ํ•ด
ํ•ด๋‹น ๋กœ์ง์„ ํด๋ž˜์Šค ๋ฐ–์œผ๋กœ ๋นผ๋‚ด์„œ ๋ณ„๋„์˜ ๋ชจ๋“ˆ๋กœ ๋งŒ๋“œ๋Š” AOP(Aspect Oriented Programming, ๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)๋ฅผ ๊ณ ์•ˆ ๋ฐ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋ฅผ ์ ์šฉํ•œ ํŠธ๋žœ์žญ์…˜ ์–ด๋…ธํ…Œ์ด์…˜(@Transactional)์„ ์ง€์›ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด๋ฅผ ์ ์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ํ•ต์‹ฌ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚จ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Service
@RequiredArgsConstructor
@Transactional
public class UserService {

   private final UserRepository userRepository;

   public void addUsers(List<User> userList) {
       for (User user : userList) {
           if (isEmailNotDuplicated(user.getEmail())) {
               userRepository.save(user);
           }
       }
   }
}