Skip to content

Latest commit

 

History

History
73 lines (53 loc) · 1.53 KB

EmbeddedH2에서_SLEEP_구현.md

File metadata and controls

73 lines (53 loc) · 1.53 KB

종종 인위적으로 DB-Connection을 가지고 오랜 시간이 걸리는 작업을 구현해야 할 때가 있다.
H2는 sleep 함수를 제공하지 않기 때문에, CREATE ALIAS를 사용하여 sleep 함수를 구현해야 한다.


sleep Function 기능 구현

public class CustomFunctions {
    public static void sleep(int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

Function Provider 구현

@Service
public class FunctionProvider {
    private final EntityManager entityManager;

    public FunctionProvider(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Transactional
    public void addFunction() {
        entityManager.createNativeQuery("CREATE ALIAS SLEEP FOR \"jpa.practice.relationship.sqlcount_assert.config.CustomFunctions.sleep\"")
                .executeUpdate();
    }
}

실제 사용모습

@Repository
@Transactional(readOnly = true)
public interface AuthorRepository extends JpaRepository<Author, Long>{

    @Query(value = "SELECT * FROM  author WHERE sleep(5000)", nativeQuery = true)
    Author findByName();
}
Name:DATA_SOURCE_PROXY, Connection:8, Time:5006, Success:True
Type:Prepared, Batch:False, QuerySize:1, BatchSize:0
Query:["SELECT * FROM  author WHERE sleep(5000)"]
Params:[()]

Test시

@BeforeEach
void before() {
    functionProvider.addFunction();
}