개발일상/TIL 5

[230916] MySQL InnoDB의 베타 락

✅ MySQL InnoDB의 베타 락 헷갈려서 스스로에게 내본 문제. (1) 인덱스가 걸려있지 않은 컬럼으로 베타 락을 거는 경우 Q. 위와 같은 test 테이블이 있다. id는 기본 키이고, name은 인덱스가 걸리지 않은 컬럼이다. 이 때 select * from test where name = 'a' for update 쿼리로 베타 락을 걸면 어떤 컬럼에 어떤 락이 걸릴까? A. *************************** 1. row *************************** OBJECT_NAME: test INDEX_NAME: NULL LOCK_TYPE: TABLE LOCK_MODE: IX LOCK_STATUS: GRANTED LOCK_DATA: NULL ***************..

개발일상/TIL 2023.09.17

[230914] Spring의 @Transactional 기본 설정, Spring의 AOP와 프록시, MySQL 아키텍처

✅ @Transactional 기본 설정 https://docs.spring.io/spring-framework/reference/data-access/transaction/declarative/annotations.html#transaction-declarative-attransactional-settings Using @Transactional :: Spring Framework The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics (for example, "start a brand new read-only transaction ..

개발일상/TIL 2023.09.14

[230911] DTO와 도메인의 변환, 중첩 트랜잭션

✅ DTO와 도메인의 변환 DTO를 사용하는 이유? Although the main reason for using a Data Transfer Object is to batch up what would be multiple remote calls into a single call, it's worth mentioning that another advantage is to encapsulate the serialization mechanism for transferring data over the wire. By encapsulating the serialization like this, the DTOs keep this logic out of the rest of the code and also prov..

개발일상/TIL 2023.09.14

[230910] JPA에서 부모가 자식을 제한해서 가지는 경우

✅ JPA에서 부모가 자식을 제한해서 가지는 경우 상황 설명 (제목 쓰는 것부터 헷갈리네...) 일반적으로 데이터베이스에서 fk로 부모와 자식의 관계를 맺는 경우, 자식이 해당 부모의 식별자를 가지고 있기만 하면 된다. 즉 부모가 가질 수 있는 자식의 수에는 제한이 없다. JPA에서도 마찬가지이다. 따라서 보통 @OneToMany로 부모 쪽에서 자식을 참조할 때는 List 등 크기 제한이 없는 컬렉션을 사용한다. @Entity public class Parent { @OneToMany private List childs = new ArrayList(); } 하지만 특정 기준에 따라 제한적으로 자식을 가지고 싶다면 어떻게 될까? 현재 내가 참여한 프로젝트에서는 '가계부 내역'과 '카테고리'가 존재한다. 가..

개발일상/TIL 2023.09.10

[230907] Spring Boot 테스트의 롤백

✅ Spring Boot 테스트의 롤백 문제 발생: 일대다 단방향 참조가 불러온 나비효과 JPA에서 일대다 단방향을 지양하라는 것은 널리 알려진 얘기이다. 대표적인 이유는 자식 엔티티를 데이터베이스에 save하는 과정에서 추가 쿼리가 날라가기 때문인데, 자식 엔티티에서 부모의 정보(id)를 가지고 있지 않아 insert 쿼리 이후 fk를 설정하는 update 쿼리가 발생한다. 이를 직접 확인하기 위해 테스트를 해보았다. @DataJpaTest public class CustomTest { @Autowired private OrderRepository orderRepository; @Test void test() { Order order = new Order(new OrderItems()); orderRe..

개발일상/TIL 2023.09.08