전체 글

github: https://github.com/belljun3395 email: [email protected]
· 스프링
save()는 즉시 INSERT하지 않는다SimpleJpaRepository.save()를 호출하면 SQL이 곧바로 실행될 것 같지만, 실제로는 그렇지 않다.@Override@Transactionalpublic S save(S entity) { if (entityInformation.isNew(entity)) { entityManager.persist(entity); return entity; } else { return entityManager.merge(entity); }}persist()가 하는 일은 두 가지뿐이다. PersistenceContext(1차 캐시)에 엔티티를 MANAGED 상태로 등록하고, ActionQueue에 EntityIns..
· 스프링
read-only 트랜잭션도 트랜잭션을 얻는다@Transactional(readOnly = true)를 보면 "트랜잭션 없이 그냥 읽기만 하겠다"는 의미로 오해하기 쉽다. 하지만 Spring 코드를 직접 보면 전혀 그렇지 않다. TransactionAspectSupport를 확인해 보면 어노테이션과 무관하게 TX를 획득한다.protected TransactionInfo createTransactionIfNecessary(...) { TransactionStatus status = null; if (txAttr != null) { if (tm != null) { status = tm.getTransaction(txAttr); // readOnly=true여도 이 ..
· 스프링
webClient.get() .uri("/external-api/data") .retrieve() .bodyToMono(Map.class) .subscribe(response -> { // 문제: subscribe 콜백 안에서 blocking 작업 수행 Thread.sleep(30000); // 30초 blocking processResponse(response); });WebClient를 사용하다가 위와 같은 코드를 작성했다고 가정해보자. HTTP 응답은 빠르게 반환되지만, 동시에 여러 요청을 보내면 일부 요청이 30초 이상 대기하는 현상이 발생한다. reactor-http-nio는 Event Loop다WebClient와 Reactor ..
· 자바
Mono와 Flux란?Mono// reactor-core/src/main/java/reactor/core/publisher/Mono.java/** * A Reactive Streams Publisher with basic rx operators that emits at most one item via the * onNext signal then terminates with an onComplete signal (successful Mono, * with or without value), or only emits a single onError signal (failed Mono). */public abstract class Mono implements CorePublisher특징0개 또는 1개의 요소만 방..
· 자바
@GetMapping("/users/{id}")public Mono getUser(@PathVariable String id) { return Mono.fromCallable(() -> { return userRepository.findById(id); // JDBC 블로킹 }) .subscribeOn(Schedulers.boundedElastic()); // ← 이 스레드는 뭐지?}WebFlux + Netty 환경에서 subscribeOn(Schedulers.boundedElastic())을 사용하면, 이 스레드는 Netty EventLoop와 완전히 별개다. Reactor Core가 자체적으로 관리하는 일반 Thread Pool이다. boundedElastic은 Rea..
· 스프링
webClient.get() .uri("/external-api/data") .retrieve() .bodyToMono(String.class) .flatMap(response -> Mono.fromCallable(() -> processResponse(response)) .subscribeOn(Schedulers.boundedElastic()) ) .subscribe();WebFlux에서 위 코드를 실행하면, HTTP 응답은 Netty EventLoop 스레드에서 받고, processResponse()는 boundedElastic 스레드에서 실행된다. 그런데 Reactor-Netty는 어떻게 Netty Even..
belljun
belljundev