김영한의 스프링/데이터베이스
-
[Spring] 스프링 트랜잭션 전파김영한의 스프링/데이터베이스 2024. 10. 4. 14:16
트랜잭션 전파는 이미 기존에 실행 중인 트랜잭션에 추가로 트랜잭션이 수행하면 어떻게 동작할지 결정하는 것이다. 예시를 통해 트랜잭션 전파에서 사용되는 개념을 천천히 알아보자! (참고! 지금부터 사용하는 트랜잭션 전파의 옵션은 기본 옵션인 REQUIRED 기준으로 설명된다! -> 이해가 안된다면 그냥 넘어가자 ㅎㅎ) 트랜잭션 전파와 관련된 여러가지 개념현재 상황은 기존에 실행 중인 트랜잭션이 있는데 갑자기 추가로 다른 트랜잭션이 생긴 과정이다. 이때 기존에 실행되고 있는 트랜잭션을 외부 트랜잭션이라고 부른다. 그에 반에 추가로 수행되는 트랜잭션은 내부 트랜잭션이라고 부른다. 이름이 이렇게 붙여진 이유를 생각하자면 처음 실행된 트랜잭션은 둘 중 상대적으로 밖에 위치 하고 있기에 외부 트랜잭션이라고 부르고 추..
-
[Spring] 스프링의 예외 처리김영한의 스프링/데이터베이스 2024. 10. 2. 18:58
이전 포스트에서 트랜잭션 관련 문제를 해결하였지만 마지막 예외 누수 문제에 대해 해결하지 못하였다. 간단히 요약하면 다음과 같이 서비스 계층은 리포지토리가 던지는 SQLExeption 예외를 의존하게 된다. - 문제의 Service 예외 누수@Transactionalpublic void accountTransfer(String fromId,String toId,int money) throws SQLException { bizLogic(fromId,toId,money);} 해당 예외 의존 때문에 만약 Repository의 인터페이스를 만들어도 문제가 발생한다. public interface MemberRepositoryEx { Member save(Member member) throws SQ..
-
[Spring] 스프링의 트랜잭션 문제 해결김영한의 스프링/데이터베이스 2024. 10. 2. 16:13
트랜잭션데이터를 단순히 파일에 저장해도 상관없는데 우리는 왜 굳이 데이터베이스에 저장을 할까? 가장 큰 이유는 트랜잭션 개념을 지원하기 때문이다. 그렇다면 트랜잭션은 무엇인가? 이제 부터 트ind07152.tistory.com 이전 포스트에서 트랜잭션의 적용은 서비스 계층에서 시작해야한다는 사실을 알았다. 트랜잭션을 시작하기 위해서는 트랜잭션 안에서는 같은 커넥션은 사용해야한다. 가장 간단한 해결 방법은 서비스 층에서 데이터베이스 커넥션을 만들고 파라미터로 레포지토리 계층에 전달하여 같은 커넥션을 사용하도록 만들면 된다! 다음은 코드 예시이다.- Servicepublic void accountTransfer(String fromId,String toId,int money) throws SQLExcepti..
-
트랜잭션김영한의 스프링/데이터베이스 2024. 9. 12. 21:12
데이터를 단순히 파일에 저장해도 상관없는데 우리는 왜 굳이 데이터베이스에 저장을 할까? 가장 큰 이유는 트랜잭션 개념을 지원하기 때문이다. 그렇다면 트랜잭션은 무엇인가? 이제 부터 트랜잭션에 대해 알아보자 트랜잭션 탄생트랜잭션을 해석하면 거래이다. 쉽게 생각하면 데이터베이스에서 트랜젝션은 하나의 거래를 안전하게 처리하도록 보장해주는 것이다. 예를 들어 A라는 사람이 B에게 2000원 계좌이체를 한다고 가정하자. 그러면 A의 잔고는 2000원 줄어들고 B의 잔고는 2000원 늘어야한다. 하지만 만약 A의 잔고는 2000원 줄어들었는데 B의 잔고는 그대로라면 심각한 문제이다. 이 과정에서 트랜잭션을 이용하면 만약 A의 잔고가 줄어들고 B가 늘어나지 않았으면 이전에 A의 잔고가 줄어든 것을 무효화 시킨다. 즉..
-
커넥션 풀과 데이터소스김영한의 스프링/데이터베이스 2024. 9. 11. 21:16
커넥션 풀커넥션을 만드는 일은 과정도 복잡하고 시간도 많이 소모 되는 일이다. 그래서 커넥션을 미리 생성해두고 관리하는 방법이 커넥션 풀이다. 커넥션 풀의 이해데이터베이스 커넥션을 획득을 할 때 어떤 과정이 일어날까?애플리케이션 로직은 DB 드라이버를 통해 커넥션을 조회한다.DB 드라이버는 DB와 TCP/IP 커넥션을 연결한다. -> 이 과정에서 3 WAY HANDSHAKE 연결을 한다.(쉽게 생각하면 네트워크 연결 준비라고 생각하면된다.)DB 드라이버는 TCP/IP 커넥션이 연결되면 아이디와 패스워드 기타 부가정보를 DB에 전달한다.인증을 완료하고 내부에 DB 세션을 생성한다.DB는 커녁션 생성이 완료 되었다는 응답을 보내고 DB 드라이버는 커넥션 객체를 생성해서 클라이언트에 반환한다.크게 이렇게 5..
-
JDBC김영한의 스프링/데이터베이스 2024. 8. 31. 20:38
JDBC 등장 이유 우리는 애플리케이션을 개발할 때 중요한 데이터는 데이터베이스에 대부분 보관을 한다. 클라이언트가 애플리케이션 서버를 통해 데이터를 조회하거나 저장하면 애플리케이션은 그림과 같이 데이터베이스를 사용한다.커넥션 연결 : 주로 TCP/IP를 사용해서 커넥션을 연결SQL 전달 : 애플리케이션 서버는 DB가 이해할 수 있는 SQL을 연결된 커넥션을 통해 DB에 전달한다.결과 응답 : DB는 받은 SQL을 실행하고 결과 값을 애플리케이션 서버에 전달한다.여기서 문제점이 있다. 예시로는 MySQL DB를 보여줬지만 데이터베이스 종류는 정말 많고 데이터베이스마다 커넥션 연결 방법, SQL 전달 방법, 결과 응답 방법이 모두 다르다. 이론적으로 DB를 바꾸게 되면 관련 데이터베이스 사용 코드도 바꿔야..