μλ νμΈμ,
μ§κΈκΉμ§ Vertxλ Hibernate Reactive API(1.0.0.CR4)λ₯Ό μ¬μ©νλ λμ λμκ² μ¨κ²¨μ Έ μμμ΅λλ€. κ·Έλ¬λ μ΄ APIλ₯Ό μ¬μ©νλ ν΄λΌμ΄μΈνΈκ° μΈμ
μ λ«κΈ°λ‘ κ²°μ ν λκΉμ§ μ΄λ € μλ μΈμ
μ λ°ννλ €λ©΄ APIλ₯Ό λ
ΈμΆν΄μΌ ν©λλ€.
Junit ν
μ€νΈλ₯Ό μ€ν μ€μ΄λ©° λ€μ APIκ° μμ΅λλ€.
public Stage.Session openSession() throws ExecutionException, InterruptedException {
if (session != null && session.isOpen()) {
session.close();
}
Stage.Session newSession = sessionFactory.openSession();
this.session = newSession;
return newSession ;
}
κ·Έλ¦¬κ³ λ΄ junit ν
μ€νΈμμ :
Stage.Session session = deviceRepository.openSession();
μ½λλ₯Ό μ€νν λ μ€λ₯κ° λ°μνμ΅λλ€.
This needs to be run on the Vert.x event loop
μ¬λ¬ κ°μ§ λ°©λ²μΌλ‘ μ½λλ₯Ό 리ν©ν λ§νμ§λ§ μ€νμ μ±κ³΅νμ§ λͺ»νμ΅λλ€.
μ¬λ°λ₯΄κ² μννλ λ°©λ²μ μΆμ²ν΄ μ£Όμκ² μ΅λκΉ?
κ°μ¬ ν΄μ.
μλ
νμΈμ,
μΈμ
μ λ¬Έμ λ μ€λ λλ‘λΆν° μμ νμ§ μλ€λ κ²μ
λλ€. λ€λ₯Έ μ€λ λμμ μΈμ
μ μ¬μ©νλ©΄ λλ²κ·ΈνκΈ° μ΄λ €μ΄ λ¬Έμ κ° λ°μν μ μμ΅λλ€. λν μ μΈμ
μ΄ μμ±λλ©΄ Vert.x 컨ν
μ€νΈμ λ°μΈλ©λλ©° μ΄ν λμΌν 컨ν
μ€νΈμμ μ¬μ©ν΄μΌ ν©λλ€.
μ΄κ²μ SessionFactory#withTransaction
λ° SessionFactory#withSession
μ¬μ©ν λ Hibernate Reactiveμ μν΄ μΌλ°μ μΌλ‘ μ²λ¦¬λ©λλ€.
ν
μ€νΈλ Vert.x μ€λ λκ° μλ JUnit μ€λ λμμ μ€νλκΈ° λλ¬Έμ μλνμ§ μμ΅λλ€.
JUnit 4μμ μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ ν κ°μ§ λ°©λ²μ io.vertx:vertx-unit:4.0.3
λΌμ΄λΈλ¬λ¦¬μ ν¬ν¨λ κ·μΉμ μ¬μ©νλ κ²μ
λλ€.
<strong i="13">@Rule</strong>
public RunTestOnContext vertxContextRule = new RunTestOnContext();
μ΄λ κ² νλ©΄ ν μ€νΈκ° Vert.x 컨ν μ€νΈμμ μ€νλ©λλ€ .
μ΄κ²μΌλ‘λ μΆ©λΆνμ§ μμ΅λλ€. λν μΈμ μ΄ κ·μΉμ μν΄ μ μλ λμΌν Vert.x 컨ν μ€νΈμ λ°μΈλ©λμ΄ μλμ§ νμΈν΄μΌ ν©λλ€.
μΌλ°μ μΌλ‘ μ΄λ₯Ό λ¬μ±νλ λ°©λ²μ μ¬μ©μ μ§μ VertxInstance μλΉμ€λ₯Ό λ§λλ κ²μ λλ€.
μ΄ μλΉμ€λ μΈμ
μ΄ μμ±λ λ μ¬μ©ν μ¬λ°λ₯Έ Vert.x μΈμ€ν΄μ€λ₯Ό μ»λ λ°©λ²μ Hibernate Reactiveμ μλ €μ€λλ€.
νλ‘κ·Έλλ° λ°©μμΌλ‘ SessionFactoryλ₯Ό μμ±νλ κ²½μ° μν λ°©λ²μ λν μλ λ€μκ³Ό κ°μ΅λλ€.
Configuration configuration = new Configuration();
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder()
.addService( VertxInstance.class, (VertxInstance) () -> getMyVertxInstance())
.applySettings( configuration.getProperties() );
StandardServiceRegistry registry = builder.build();
SessionFactory sessionFactory = configuration.buildSessionFactory( registry );
λλ JDK μ
org.hibernate.reactive.vertx.VertxInstance
λΌλ ν
μ€νΈ νμΌμ μΆκ°ν©λλ€.
/META-INF/services/
VertxInstance
μΈν°νμ΄μ€λ₯Ό ꡬννλ ν΄λμ€μ λν κ²½λ‘κ° ν¬ν¨λ©λλ€.
org.myproject.MyVertxProvider
μ΄μ Hibernate Reactiveλ Vert.x μΈμ€ν΄μ€κ° νμν λ MyVertxProvider
μ μΈμ€ν΄μ€λ₯Ό νΈμΆνκ³ μ½λκ° λμΌν Vert.x 컨ν
μ€νΈμμ μ€νλλ©΄ λͺ¨λ κ²μ΄ μλν΄μΌ ν©λλ€.
ν μ€νΈλ‘ λμκ°μ λ¬Έμ λ₯Ό μμ νλ ν κ°μ§ λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€.
<strong i="41">@Rule</strong>
public RunTestOnContext vertxContextRule = new RunTestOnContext();
...
Configuration configuration = new Configuration();
StandardServiceRegistryBuilder builder = new ReactiveServiceRegistryBuilder()
.addService( VertxInstance.class, (VertxInstance) () -> vertxContextRule.vertx() )
.applySettings( configuration.getProperties() );
SessionFactory sessionFactory = configuration.buildSessionFactory( builder.build() );
κ·μΉμ μ¬μ©νμ§ μλ κ²½μ° λ€μκ³Ό κ°μ λ°©λ²μΌλ‘ νΉμ Vert.x 컨ν μ€νΈμμ μ½λλ₯Ό μ€νν μ μμ΅λλ€.
Context currentContext = Vertx.currentContext();
currentContext.runOnContext( event -> {
// Here you should be able to use the session
});
λ°©κΈ μμ±ν λ΄μ©μ FAQλ‘ μΉμ¬μ΄νΈ λλ μ΄λμλ μΆκ°ν΄μΌ ν©λλ€.
λ€, νκ² μ΅λλ€
@DavidD κ·νμ μ€λͺ μ κ°μ¬λ립λλ€. κ°μ¬ ν΄μ.
λλ λ¬Έμ/μΉμ¬μ΄νΈμ λν μ΄λ¬ν λ³κ²½ μ¬νμ μΆμ νκΈ° μν λ¬Έμ λ₯Ό λ§λ€μμ΅λλ€: https://github.com/hibernate/hibernate-reactive/issues/751
μ΄ λ¬Έμ λ₯Ό μ’ λ£νκ² μ΅λλ€. λ€λ₯Έ μ§λ¬Έμ λͺ ννμ§ μμ΅λλ€.
κ°μ¬ν©λλ€.
@DavidD JUnit 5μμ μμ μΈκΈν λ€μ μ½λλ₯Ό μ¬μ©ν μ μμ΅λκΉ?
Context currentContext = Vertx.currentContext();
currentContext.runOnContext( event -> {
// Here you should be able to use the session
});
μ, κ·Έ μ½λλ JUnitμ΄ μλλΌ Vert.xμ λλ€.
κ·Έλ¬λ μ¬κΈ°μ t ν΄λΉνλ νμ₯μ΄ μλλλ€ .
<strong i="8">@Rule</strong>
public RunTestOnContext vertxContextRule = new RunTestOnContext();
κ°μ₯ μ μ©ν λκΈ
λ€, νκ² μ΅λλ€