inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

해결된 질문

443

qwerty1434

작성한 질문수 6

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();
    }
}

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이 발생하는지, 그리고 어떻게하면 이러한 문제를 해결할 수 있는지 궁금합니다..!


 

 

 

 

java jpa spring querydsl

답변 0

강사님.. 하단 URL에 있었던 코드들 다운 받거나 공유 부탁 드립니다..

0

15

2

섹션3. 11 회원객체 다이어그램

0

18

1

OCP, DIP과 @Qualifier 어노테이션에 대해서 질문합니다.

0

19

1

강의 연장 요청

0

19

2

노션 접근권

0

20

1

설정 정보 없이 임베디드 데이터베이스 생성

0

15

1

@Version 어노테이션이 붙은 필드는 낙관적 락이든 비관적 락이든 업데이트가 됩니다.

0

19

1

형 이번에 낸 책이랑 강의 내용에 차이가 있어?

1

36

1

형 나 몰래 책내면 모를 줄 알고?

1

30

1

OrderServiceTest 상문주문 테스트 시 update 쿼리 문의

0

21

1

멀티스레드

1

22

2

N+1 관련해서 질문있습니다.

0

37

3

성능 오버헤드

1

29

2

다음 강의는 언제 올려주시는지요?

0

37

2

하단 파일이 열리지 않습니다

0

35

1

ssh-keygen -t rsa -P "" 등 ssh 암호화? 는 하둡에서 필수인가요?

0

28

2

강의 중복 확인 요청

0

26

1

volatile에 대해 질문 있습니다.

2

39

2

Mark and Sweep

1

35

1

20년 4,5회 13번

0

27

2

GC 알고리즘

1

32

2

용어 질문

1

26

1

호출횟수 질문입니다.

1

37

2

실행과정 질문입니다.

2

39

1