
Contents
트랜잭션이란 ?트랜잭션이란 ?
더 이상 쪼갤 수 없는 업무 처리의 최소 단위이다.
예를 들어 계좌이체하는 은행어플을 만든다고 하자.
먼저, 설계할 때 한 설계도(class) 안에 모든 정보를 넣으면 나중에 오류가 있을 때 한 설계도 안에서 하나씩 확인해보며 찾아야해서 효율적이지 않다. 그리고 깔끔하지도 않다.
그렇기 때문에 꼭 필요한 유저 데이터와 계좌 데이터는 따로 만들어줘야 한다.
그럼 그 데이터들로 입출금, 즉 이체를 해야 하는데 이 메소드를 어디에 넣을까 ?
메인설계도도 아니고 유저데이터와 계좌데이터도 아니다.
계좌이체만 관리할 수 있는 파일을 하나 새로 만들어줘야한다.
그 클래스가 바로 트랜잭션이다.
이렇게 트랜잭션으로 만들어서 관리하면 디버깅할 때 해당 트랜잭션으로 이동하여 디버깅하면 손쉽게 해결할 수 있어 효율적이다.
아래에서 확인해보자.
유저 데이터
public class User {
private final int id;
private String name;
private String email;
public User(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
계좌 데이터
// 객체의 상태를 변경, 객체의 상태를 확인
public class Account {
private final int id;
private long balance;
private int userId;
public boolean 잔액부족하니(long amount) {
if (balance < amount) {
return true;
}
return false;
}
// 메서드는 하나의 책임만 가지는 게 좋다.
public void 출금(long amount) {
this.balance = this.balance - amount;
}
public void 입금(long amount) {
this.balance = this.balance + amount;
}
public Account(int id, long balance, int userId) {
this.id = id;
this.balance = balance;
this.userId = userId;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", balance=" + balance +
", userId=" + userId +
'}';
}
}
서비스 데이터 (입금 데이터)
import ch04.example.model.Account;
// 트랜잭션 관리
public class BankService {
public static void 출금(Account withrawAccount, long amount) {
if (amount <= 0) {
System.out.println("0원 이하 금액을 출금할 수 없습니다.");
return;
}
if (withrawAccount.잔액부족하니(amount)) {
System.out.println("잔액이 부족합니다.");
return;
}
withrawAccount.출금(amount);
}
public static void 이체(Account senderAccount, Account receiverAccount, long amount) {
if (amount <= 0) {
System.out.println("0원 이하 금액을 이체할 수 없습니다.");
return;
}
if (senderAccount.잔액부족하니(amount)) {
System.out.println("잔액이 부족합니다.");
return;
}
senderAccount.출금(amount);
receiverAccount.입금(amount);
}
}
메인
import ch04.example.model.Account;
import ch04.example.model.User;
public class BankApp {
public static void main(String[] args) {
// 1. 고객 3명 만들기
User ssar = new User(1, "ssar", "ssar@nate.com");
User cos = new User(2, "cos", "cos@nate.com");
User love = new User(3, "love", "love@nate.com");
// 2. 계좌 3개 만들기
Account ssarAccount = new Account(1111, 1000L, 1);
Account cosAccount = new Account(2222, 1000L, 2);
Account loveAccount = new Account(3333, 1000L, 3);
// 3. 고객에게 정보를 받기 (amount)
long amount = 100L;
// 4. 이체 (ssar -> cos 100원)
BankService.이체(ssarAccount, cosAccount, amount);
// 5. 이체 (ssar -> love 100원)
BankService.이체(ssarAccount, loveAccount, amount);
// 6. 이체 (cos -> love 100원)
BankService.이체(cosAccount, loveAccount, amount);
// 7. 객체 상태 확인
System.out.println(ssarAccount);
System.out.println(cosAccount);
System.out.println(loveAccount);
// 8. 출금
BankService.출금(ssarAccount, amount);
BankService.출금(loveAccount, amount);
}
}
이렇게 트랜잭션을 잘 나누어서 만든다면 효율적이고 깔끔하게 만들 수 있고 관리하기도 용이하다.
Share article