Home > Spring > ๐Ÿƒ[Spring] `@EnableTransactionManagement`์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

๐Ÿƒ[Spring] `@EnableTransactionManagement`์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?
Spring Framework

๐Ÿƒ[Spring] @EnableTransactionManagement์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

  • @EnableTransactionManagement๋Š” Spring Framework์—์„œ ์„ ์–ธ์  ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์• ๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
    • ์ด ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฉ”์„œ๋“œ ๋‹จ์œ„์˜ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ ๊ธฐ๋Šฅ.

1๏ธโƒฃ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ํ™œ์„ฑํ™”.

  • @EnableTransactionManagement๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ์—์„œ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ จ ๋นˆ์„ ์ƒ์„ฑํ•˜๊ณ  ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค.
  • @Transactional ์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ๋ฉ”์„œ๋“œ์˜ ํŠธ๋žœ์žญ์…˜ ๊ฒฝ๊ณ„๋ฅผ ์ •์˜ํ•˜๊ณ  ์ž๋™์œผ๋กœ ํŠธ๋žœ์žญ์…˜์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ํ”„๋ก์‹œ ๊ธฐ๋ฐ˜ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ.

  • Spring์€ AOP(Aspect-Oriented Programming)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠธ๋žœ์žญ์…˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • @Transactional์ด ๋ถ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด Spring์ด ์ž๋™์œผ๋กœ ํ”„๋ก์‹œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘, ์ปค๋ฐ‹, ๋กค๋ฐฑ ๋“ฑ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•.

@Configuration
@EnableTransactionManagement
public class AppConfig {
    
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
  • ์œ„์˜ ์ฝ”๋“œ์—์„œ:
      1. @EnableTransactionManagement: ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”.
      1. PlatformTransactionManager : Spring์ด ํŠธ๋žœ์žญ์…˜์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ํŠธ๋žœ์žญ์…˜ ๋งค๋‹ˆ์ €๋ฅผ ์ •์˜.

3๏ธโƒฃ ๋™์ž‘ ์›๋ฆฌ.

1๏ธโƒฃ @Transactional ์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด:

  • Spring์€ AOP ํ”„๋ก์‹œ๋ฅผ ํ†ตํ•ด ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘.
  • ๋ฉ”์„œ๋“œ ์‹คํ–‰ ํ›„ ์ •์ƒ์ ์œผ๋กœ ์™„๋ฃŒ๋˜๋ฉด ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹.
  • ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํŠธ๋žœ์žญ์…˜ ๋กค๋ฐฑ.

2๏ธโƒฃ Spring์€ PlatformTransactionManager๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŠธ๋žœ์žญ์…˜์„ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค.

4๏ธโƒฃ ์˜ˆ์ œ.

1๏ธโƒฃ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ํ™œ์„ฑํ™”.

@Configuration
@EnableTransactionManagement
public class TransactionConfig {
    
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

2๏ธโƒฃ ์„œ๋น„์Šค ํด๋ž˜์Šค์—์„œ ํŠธ๋žœ์žญ์…˜ ์‚ฌ์šฉ.

@Service
public class UserService {
    
    @Transactional
    public void createUser(User user) {
        // ํŠธ๋žœ์žญ์…˜ ์•ˆ์—์„œ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ
        userRepository.save(user);
        
        if (user.getName().equals("error")) {
            throw new RuntimeException("Rollback test");
        }
    }
}
  • ์œ„ ์ฝ”๋“œ์—์„œ:
    • @Transactional : createUser ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ํŠธ๋žœ์žญ์…˜์ด ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ํŠธ๋žœ์žญ์…˜์ด ๋กค๋ฐฑ๋ฉ๋‹ˆ๋‹ค.

5๏ธโƒฃ ์˜ต์…˜.

  • @EnableTransactionManagement์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ mode

  • ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ๋ชจ๋“œ๋ฅผ ์„ค์ •.
  • ๊ธฐ๋ณธ๊ฐ’์€ AdviceMode, PROXY, AOP ํ”„๋ก์‹œ๋ฅผ ์‚ฌ์šฉ.
    @EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
    

2๏ธโƒฃ proxyTargetClass

  • CGLIB ๊ธฐ๋ฐ˜ ํ”„๋ก์‹œ๋ฅผ ์ƒ์„ฑํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์„ค์ •.
  • ๊ธฐ๋ณธ๊ฐ’์€ false(์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜ ํ”„๋ก์‹œ ์‚ฌ์šฉ).
    @EnableTransactionManagement(proxyTargetClass = true)
    

6๏ธโƒฃ ์š”์•ฝ.

  • @EnableTransactionManagement๋Š” Spring์—์„œ ์„ ์–ธ์  ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”ํ•˜๋Š” ์• ๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
  • @Transactional ์• ๋…ธํ…Œ์ด์…˜๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์„œ๋“œ ๋‹จ์œ„์˜ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ„๋‹จํžˆ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Spring AOP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠธ๋žœ์žญ์…˜ ๊ฒฝ๊ณ„๋ฅผ ์„ค์ •ํ•˜๊ณ , ํŠธ๋žœ์žญ์…˜ ๋งค๋‹ˆ์ €๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŠธ๋žœ์žญ์…˜์„ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค.