Querydsl을 멀티쓰레드 환경에서 사용할 때 발생하는 문제에 대해
안녕하세요. 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
aws 관련 질문드립니다.
1
12
3
6강 17:50
0
13
1
6강 11:37
0
16
1
08:30 이중포인터 질문
0
12
0
6강 5:15
0
24
1
5강 5:45
0
17
2
전체적으로 답을 먼저 알려주지 마세요..
0
21
2
실습 권한 부탁드립니다.
0
19
1
강사님 질문 있어요.
0
22
2
reference to myMethod is ambiguous 오류
0
24
1
어플리케이션 실행 후 에러에 관하여 질문 드립니다.
2
42
2
업캐스팅 문제 예시 5
0
43
2
(기출) 2025년 2회 이론 파트
0
42
2
RestTemplate과 webClient, RestClient에 대해서 질문드립니다.
0
25
2
회원 도메인 인터페이스 개발
0
31
1
26년 1회실기
0
47
2
강의 자료
0
40
2
setter 사용하지 않기
0
36
1
27:15 break 출력
0
40
2
세션을 제대로 이해한게 맞는지 궁금합니다
0
23
1
jsp의 상대경로와 Controller에서 반환하는 이름이 달라도 괜찮나요?
0
26
1
55강 파이썬에만있는 연산자들
0
39
2
55강의 파이썬에서만 있는 연산자들
0
30
2
안녕하세요 토비님 혹시 완성된 코드의 repository 주소는 없을까요?
0
45
2





