🤍 전 강의 25% 할인 중 🤍

2024년 상반기를 돌아보고 하반기에도 함께 성장해요!
인프런이 준비한 25% 할인 받으러 가기 >>

Querydsl을 멀티쓰레드 환경에서 사용할 때 발생하는 문제에 대해

23.03.10 19:54 작성 조회수 347

0

안녕하세요. Querydsl강의를 수강하고 혼자 개발을 진행하다가 다음과 같은 문제가 발생해서 질문을 남기게 되었습니다.
제 상황은 A엔티티와 B엔티티를 가지고 있고, 둘은 OneToOne관계로 B가 연관관계의 주인인 상황입니다. Repository에는 A와 B의 데이터를 호출하는 Querydsl코드가 각각 존재하며, 이를 Service Layer에서 그대로 Return값으로 받고 있습니다.
이들을 멀티쓰레드 환경에서 호출했을 때 B데이터를 호출하는 코드에 Connection leak이 존재한다는 에러가 발생하는 상황입니다. (.yml파일에서 hikari의 leak-detection-threshold을 설정한 상황입니다.)

Domain

@NoArgsConstructor
@AllArgsConstructor
@Entity
public class A {
    @Id
    private Long id;

    @OneToOne(mappedBy = "a")
    private B b;

    private int var1;
}
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class B {
    @Id
    private Long id;

    @OneToOne
    @JoinColumn(name = "b_id")
    private A a;

    private int var1;
}

Repository

public interface Repository extends JpaRepository<A,Long>, RepositoryCustom {

}
public interface RepositoryCustom {
    Dto queryOne();
    Dto queryTwo();
    Dto queryThree();
}
public class RepositoryCustomImpl implements RepositoryCustom {
    private final JPAQueryFactory queryFactory;

    public RepositoryCustomImpl(EntityManager em) {
        this.queryFactory = new JPAQueryFactory(em);
    }

    @Override
    public Dto queryOne() {
        return queryFactory
                .select(Projections.constructor(Dto.class,
                        a.id,
                        a.var1
                ))
                .from(a)
                .fetchFirst();
    }

    @Override
    public Dto queryTwo(){
        return queryFactory
                .select(Projections.constructor(Dto.class,
                        b.id,
                        b.var1
                ))
                .from(b)
                .fetchFirst();
    }

    @Override
    public Dto queryThree(){
        System.out.println("a.getClass() = " + a.getClass());
        System.out.println("b.getClass() = " + b.getClass());
        return queryFactory
                .select(Projections.constructor(Dto.class,
                        b.id,
                        b.var1
                ))
                .from(b)
                .fetchFirst();
    }
}
  • queryThree는 queryTwo와 Querydsl코드는 동일합니다. 다만 쿼리를 실행하기 전에 Qtype의 a와 b를 순서대로 한번 호출했다는 부분이 queryTwo와 다릅니다.

DTO

public class Dto {
    private Long id;
    private int var1;
}

Service

@org.springframework.stereotype.Service
@RequiredArgsConstructor
public class Service {

    private final Repository repo;

    @Transactional
    public Dto logicOne(){
        return repo.queryOne();
    }

    @Transactional
    public Dto logicTwo(){
        return repo.queryTwo();
    }

    @Transactional
    public Dto logicThree(){
        return repo.queryThree();
    }

}

테스트코드

@SpringBootTest
@Slf4j
public class SimpleTest {
    @Autowired
    Service service;

    @Test
    void connection_leak_detected(){
        Runnable userA = () -> {
            Dto dto = service.logicOne();
            log.info("[Thread A: {}]",dto);
        };
        Thread threadA = new Thread(userA);
        threadA.start();

        Runnable userB = () -> {
            Dto dto = service.logicTwo();
            log.info("[Thread B: {}]",dto);
        };
        Thread threadB = new Thread(userB);
        threadB.start();

        sleep(6000);

    }

    @Test
    void leak_not_detected(){
        Runnable userA = () -> {
            Dto dto = service.logicOne();
            log.info("[Thread A: {}]",dto);
        };
        Thread threadA = new Thread(userA);
        threadA.start();

        Runnable userB = () -> {
            Dto dto = service.logicThree();
            log.info("[Thread B: {}]",dto);
        };
        Thread threadB = new Thread(userB);
        threadB.start();

        sleep(6000);
    }

    @Test
    @Transactional
    void connection_leak_detected_otherCase(){
        Runnable userA = () -> {
            Dto dto = service.logicOne();
            log.info("[Thread A: {}]",dto);
        };
        Thread threadA = new Thread(userA);
        threadA.start();

        Runnable userB = () -> {
            Dto dto = service.logicThree();
            log.info("[Thread B: {}]",dto);
        };
        Thread threadB = new Thread(userB);
        threadB.start();

        sleep(6000);
    }




    private void sleep(int millis){
        try{
            Thread.sleep(millis);
        } catch (InterruptedException e){
            e.printStackTrace();
        }
    }

}
  • connection_leak_detected를 테스트하면 Connection leak이 발생했다는 에러가 발생합니다.

  • leak_not_detected를 테스트하면 Connection leak 로그가 출력되지 않습니다.

  • leak_not_detected와 동일한 테스트코드에 Transactional을 선언한 connection_leak_detected_otherCase은 Connection leak 로그가 출력됩니다.

어떤 이유로 이러한 Connection leak이 발생하는지, 그리고 어떻게하면 이러한 문제를 해결할 수 있는지 궁금합니다..!


 

 

 

 

답변 0

답변을 작성해보세요.

답변을 기다리고 있는 질문이에요.
첫번째 답변을 남겨보세요!

채널톡 아이콘