πŸ’»
Albert's Til
GitHub
  • 맀일맀일 μ‘°κΈˆμ”© μ„±μž₯ν•˜κΈ°
    • README
    • CS
      • Network
      • HTTP
        • NO-CACHE
      • 였λ₯˜ μ½”λ“œ
      • ORM 도ꡬ
      • Design Pattern
        • CQRS Pattern
          • Event Sourcing and CQRS pattern
        • Builder Pattern
    • DB
      • MySQL
        • Timeline
        • Pagination
        • Index
        • Database Performance Optimization Strategies
        • B+ tree
        • MySQL Connectors VS MySQL Shell(Scripting) VS MySQL Workbench
        • MySQL Storage Engine Architecture
      • Normalization & Denormalization
      • JPA
        • @Transactional
        • Why JPA?
        • About JPA
        • N+1 Issue
        • Index
        • ElementCollection&CollectionTable
        • orphanRemoval
        • CascadeType
        • Use Subselect
        • Dynamic Instance Creation
        • Paging
        • Order
        • Spefication
        • mappedBy
      • MongoDB
        • ObjectId
      • Why MySQL?
      • ACID properties of transactions
      • Between JPA and JDBC
      • Identifiers in Hibernate/JPA
    • Java
      • Jackson de/serialize
      • Collections.singletonList() vs List.of()
      • Manage dependencies in Gradle
      • Logging Level
      • Bean Validation
      • JVM Internals
        • Threads
          • Frame
        • Shared Between Threads
          • Classloader
            • Class Loader Hierarchy
            • Loading Linking Initialization
      • Java Collection Framework
      • Annotation
      • Generic
      • λ””λ―Έν„° 법칙
    • Spring
      • Caching
      • Spring Integration Overview
        • ThreadPollTaskExecutor
        • Messaging Bridge
        • Channel Adapter
        • Poller
        • Configuration and @EnableIntegration
        • Message Endpoints
        • Message Channels
      • HATEOAS
      • @Autowired vs Constructor Dependency Injection
      • Spring Security
        • JWT 토큰 μ‚¬μš©ν•œ 인가
        • OAuth 2 Login
        • OAuth 2 인증
        • 인가
        • 인증
        • PasswordEncoder
      • IoC Container
      • Filter,Interceptor,AOP,Argument Resolver
      • Spring Annotation
      • About Spring
    • Kafka
      • Error Channel
    • Infra
      • Scale Up || Scale Out
      • Docker
        • Dockerfile
        • Docker Hub Deploy
        • Command
      • Cloud μœ ν˜•
        • Infrastructure as a Service
        • Platform as a Service
        • Software as a Service
      • 무쀑단 배포
        • μ—”μ§„μ—‘μŠ€(Nginx)
      • μ½”λ“œ μžλ™ 배포
        • Technical
      • AWS EC2
        • PEM(Privacy Enhanced Mail) ν‚€
      • AWS RDS
      • AWS S3
    • CodeSquad
      • Spring Boot Project 1μ£Όμ°¨ 회고
      • Spring Boot Project 2μ£Όμ°¨ 회고
      • Spirng Boot Project 3μ£Όμ°¨ 회고
      • Spring Boot Project 4μ£Όμ°¨ 회고
    • Foody Moody ν”„λ‘œμ νŠΈ
      • Query Performance Comparison
      • HeartCount Asynchronous Issue
      • DeferredResult
      • ResponseBodyEmitter
      • SseEmitter (Spring)
      • Server-Sent Events (SSE)
      • 기술 μŠ€νƒ 적용 이유
      • NO-CACHE(HTTP)
      • Transactional
    • DDD
      • AggregateId
    • Test
      • RestAssured
    • Coding and Algorithmic Problems
      • 819. Most Common Word
      • 344. Reverse String
      • 125. Valid Palindrome
      • 937. Reorder Data in Log Files
    • Node
      • Async... Await...
      • Custom Transactional Decorator Challenger
    • Python
      • Python Basic Grammar
        • Comments and Input/Output
        • Variable
        • Data type
        • Operations and syntax
        • List,Tuple,Dictionary,Set
        • Function
        • Conditional statement
        • Loop
    • HTML
      • HTML Basic
      • HTML Basic Tags
      • HTML Form Tags
      • HTML Table Tags
    • CSS
      • CSS Basic
      • CSS Practice
Powered by GitBook
On this page
  • Atomicity
  • Consistency
  • Isolation
  • Durability
  • νŠΈλžœμž­μ…˜ 처리

Was this helpful?

  1. 맀일맀일 μ‘°κΈˆμ”© μ„±μž₯ν•˜κΈ°
  2. DB

ACID properties of transactions

Atomicity

  • 데이터에 λŒ€ν•œ λͺ¨λ“  변경은 단일 μž‘μ—…μΈ κ²ƒμ²˜λŸΌ μˆ˜ν–‰λ©λ‹ˆλ‹€.

  • 즉, λͺ¨λ“  λ³€κ²½ 사항이 μˆ˜ν–‰λ˜κ±°λ‚˜ 아무 것도 μˆ˜ν–‰λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, ν•œ κ³„μ •μ—μ„œ λ‹€λ₯Έ κ³„μ •μœΌλ‘œ μžκΈˆμ„ μ΄μ²΄ν•˜λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μ›μžμ„± 속성은 ν•œ κ³„μ •μ—μ„œ 좜금이 μ„±κ³΅μ μœΌλ‘œ 이루어지면 ν•΄λ‹Ή 계정이 λ‹€λ₯Έ κ³„μ •μœΌλ‘œ μž…κΈˆλ˜λ„λ‘ ν•©λ‹ˆλ‹€.

  • μ˜ˆμ‹œ:

    • μ€ν–‰μ—μ„œ ν•œ κ³„μ’Œμ—μ„œ λ‹€λ₯Έ κ³„μ’Œλ‘œ λˆμ„ μ΄μ²΄ν•˜λŠ” 경우,

    • 좜금과 μž…κΈˆ λͺ¨λ‘ μ„±κ³΅μ μœΌλ‘œ 이루어져야 ν•©λ‹ˆλ‹€. λ§Œμ•½ μž…κΈˆ 도쀑 였λ₯˜κ°€ λ°œμƒν•œλ‹€λ©΄, μΆœκΈˆλ„ μ·¨μ†Œλ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€. @Transactional μ–΄λ…Έν…Œμ΄μ…˜ 덕뢄에 두 μ—°μ‚° 쀑 ν•˜λ‚˜λΌλ„ μ‹€νŒ¨ν•˜λ©΄ 전체 νŠΈλžœμž­μ…˜μ΄ λ‘€λ°±λ©λ‹ˆλ‹€.

    @Transactional
    public void transferMoney(String fromAccountId, String toAccountId, int amount) {
        Account fromAccount = accountRepository.findById(fromAccountId);
        Account toAccount = accountRepository.findById(toAccountId);
        
        fromAccount.debit(amount);  // 좜금
        toAccount.credit(amount);   // μž…κΈˆ
        
        accountRepository.save(fromAccount);
        accountRepository.save(toAccount);
    }

Consistency

  • λ°μ΄ν„°λŠ” νŠΈλžœμž­μ…˜μ΄ μ‹œμž‘λ  λ•Œμ™€ 끝날 λ•Œ μΌκ΄€λœ μƒνƒœμž…λ‹ˆλ‹€.

  • 예λ₯Ό λ“€μ–΄ ν•œ κ³„μ’Œμ—μ„œ λ‹€λ₯Έ κ³„μ’Œλ‘œ μžκΈˆμ„ μ΄μ²΄ν•˜λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ 일관성 속성은 각 거래의 μ‹œμž‘κ³Ό μ’…λ£Œ μ‹œ 두 κ³„μ’Œμ˜ 총 자금 κ°€μΉ˜κ°€ λ™μΌν•˜λ„λ‘ ν•©λ‹ˆλ‹€.

  • μ˜ˆμ‹œ:

    • μ‚¬μš©μžμ˜ κ³„μ’Œ μž”μ•‘μ΄ 항상 0 이상이어야 ν•œλ‹€λŠ” λΉ„μ¦ˆλ‹ˆμŠ€ κ·œμΉ™,

    • μž”μ•‘μ΄ μΆœκΈˆμ•‘λ³΄λ‹€ 적으면 μ˜ˆμ™Έκ°€ λ°œμƒν•˜κ³  νŠΈλžœμž­μ…˜μ€ λ‘€λ°±λ©λ‹ˆλ‹€. 이둜 인해 λ°μ΄ν„°μ˜ 일관성(κ³„μ’Œ μž”μ•‘μ΄ 0 이상이어야 함)이 μœ μ§€λ©λ‹ˆλ‹€.

    @Transactional
    public void withdrawMoney(String accountId, int amount) {
        Account account = accountRepository.findById(accountId);
        if(account.getBalance() - amount < 0) {
            throw new InsufficientFundsException("μž”μ•‘ λΆ€μ‘±!");
        }
        account.debit(amount);
        accountRepository.save(account);
    }

Isolation

  • νŠΈλžœμž­μ…˜μ˜ 쀑간 μƒνƒœλŠ” λ‹€λ₯Έ νŠΈλžœμž­μ…˜μ— 보이지 μ•ŠμŠ΅λ‹ˆλ‹€. κ·Έ κ²°κ³Ό λ™μ‹œμ— μ‹€ν–‰λ˜λŠ” νŠΈλžœμž­μ…˜μ΄ μ§λ ¬ν™”λ˜λŠ” κ²ƒμ²˜λŸΌ λ³΄μž…λ‹ˆλ‹€.

  • 예λ₯Ό λ“€μ–΄ ν•œ κ³„μ •μ—μ„œ λ‹€λ₯Έ κ³„μ •μœΌλ‘œ μžκΈˆμ„ μ΄μ²΄ν•˜λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ 격리 속성은 λ‹€λ₯Έ νŠΈλžœμž­μ…˜μ΄ ν•œ 계정 λ˜λŠ” λ‹€λ₯Έ κ³„μ •μ—μ„œ 이체된 μžκΈˆμ„ λ³Ό 수 μžˆλ„λ‘ ν•˜μ§€λ§Œ λ‘˜ λ‹€μ—μ„œ λ³Ό 수 없도둝 ν•©λ‹ˆλ‹€.

  • μ˜ˆμ‹œ:

    • λ™μ‹œμ— λ™μΌν•œ μƒν’ˆμ„ κ΅¬λ§€ν•˜λŠ” 두 μ‚¬μš©μž,

    • SERIALIZABLE 격리 μˆ˜μ€€ 덕뢄에 ν•œ μ‚¬μš©μžκ°€ μƒν’ˆμ„ κ΅¬λ§€ν•˜λŠ” λ™μ•ˆ λ‹€λ₯Έ μ‚¬μš©μžλŠ” ν•΄λ‹Ή μƒν’ˆμ˜ 재고λ₯Ό μ‘°νšŒν•˜κ±°λ‚˜ μˆ˜μ •ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

    @Transactional(isolation = Isolation.SERIALIZABLE)
    public void purchaseItem(String itemId) {
        Item item = itemRepository.findById(itemId);
        if(item.getStock() > 0) {
            item.decrementStock();
            itemRepository.save(item);
        } else {
            throw new OutOfStockException("재고 μ—†μŒ!");
        }
    }

Durability

  • νŠΈλžœμž­μ…˜μ΄ μ„±κ³΅μ μœΌλ‘œ μ™„λ£Œλœ ν›„ 데이터에 λŒ€ν•œ λ³€κ²½ 사항은 μœ μ§€λ˜λ©° μ‹œμŠ€ν…œ 였λ₯˜κ°€ λ°œμƒν•˜λ”λΌλ„ μ‹€ν–‰ μ·¨μ†Œλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

  • 예λ₯Ό λ“€μ–΄ ν•œ κ³„μ •μ—μ„œ λ‹€λ₯Έ κ³„μ •μœΌλ‘œ μžκΈˆμ„ μ΄μ²΄ν•˜λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ 내ꡬ성 속성은 각 계정에 적용된 λ³€κ²½ 사항이 μ·¨μ†Œλ˜μ§€ μ•Šλ„λ‘ ν•©λ‹ˆλ‹€

  • μ˜ˆμ‹œ:

    • μ£Όλ¬Έ 정보 μ €μž₯

    • μ‚¬μš©μžκ°€ 주문을 ν•œ ν›„, μ‹œμŠ€ν…œμ— λ¬Έμ œκ°€ λ°œμƒν•˜λ”λΌλ„ κ·Έ μ£Όλ¬Έ μ •λ³΄λŠ” λ°μ΄ν„°λ² μ΄μŠ€μ— μ•ˆμ „ν•˜κ²Œ μ €μž₯λ©λ‹ˆλ‹€. λ°μ΄ν„°λ² μ΄μŠ€κ°€ μ£Όλ¬Έ 정보λ₯Ό λ””μŠ€ν¬μ— μ €μž₯ν•¨μœΌλ‘œμ¨ 지속성을 보μž₯ν•©λ‹ˆλ‹€.

    @Transactional
    public void placeOrder(Order order) {
        orderRepository.save(order);
    }

νŠΈλžœμž­μ…˜ 처리

νŠΈλžœμž­μ…˜ 처리 μ‹œμŠ€ν…œμ„ μ‚¬μš©ν•˜λ©΄ μ• ν”Œλ¦¬μΌ€μ΄μ…˜ ν”„λ‘œκ·Έλž˜λ¨Έκ°€ νŠΈλžœμž­μ…˜ κ΄€λ¦¬μ˜ μ„ΈλΆ€ μ‚¬ν•­μœΌλ‘œλΆ€ν„° μ• ν”Œλ¦¬μΌ€μ΄μ…˜ ν”„λ‘œκ·Έλž¨μ„ λ³΄ν˜Έν•˜μ—¬ λΉ„μ¦ˆλ‹ˆμŠ€λ₯Ό μ§€μ›ν•˜λŠ” μ½”λ“œ μž‘μ„±μ— 집쀑할 수 μžˆμŠ΅λ‹ˆλ‹€.

  • νŠΈλžœμž­μ…˜μ˜ λ™μ‹œ 처리λ₯Ό κ΄€λ¦¬ν•©λ‹ˆλ‹€.

  • 데이터 곡유λ₯Ό κ°€λŠ₯ν•˜κ²Œ ν•©λ‹ˆλ‹€.

  • λ°μ΄ν„°μ˜ 무결성을 보μž₯ν•©λ‹ˆλ‹€.

  • νŠΈλžœμž­μ…˜ μ‹€ν–‰μ˜ μš°μ„  μˆœμœ„λ₯Ό κ΄€λ¦¬ν•©λ‹ˆλ‹€.

Last updated 1 year ago

Was this helpful?

ACID properties of transactions
Logo
Transaction processing
Logo
Introduction to Transactions in Java and Spring | BaeldungBaeldung
Introduction to Transactions | Baeldung on Computer ScienceBaeldung on Computer Science
Using Transactions for Read-Only Operations | BaeldungBaeldung
Logo
Logo
Logo