开发者问题收集

org.hibernate.service.UnknownServiceException:请求未知服务 [org.hibernate.reactive.vertx.VertxInstance]

2021-05-18
345

我使用 Spring Boot - Webflux - Hibernate Reactive - Postgresql 作为我的 Web 应用的技术堆栈。所有端点检索端点均正常工作,但保存用户端点不工作。有人能帮我理解为什么会出现异常吗?

Mutiny.SessionFactory factory = CustomEntityManagerFactory.getInstance().getEntityManagerFactory(idCC).unwrap(Mutiny.SessionFactory.class); 之后产生异常。在其他端点中,CustomEntityManagerFactory 运行良好。

@Repository
public class UserRepository {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Transactional
    public void save(User user) throws Exception {
        try {
            Mutiny.SessionFactory factory = CustomEntityManagerFactory
                    .getInstance()
                    .getEntityManagerFactory(null)
                    .unwrap(Mutiny.SessionFactory.class); // Exception is produced here

            // Spring never pass here. Goes directly to the catch statement.

            Void transaction = factory.withTransaction(
                    (session, tx) -> session.persist(user)
            ).await().indefinitely();


            factory.close();
        } catch (Exception exception) {
            logger.error("ERROR: ", exception);
        }

    }
// Other methods here down
}

控制台

org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.reactive.vertx.VertxInstance]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
    at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.getService(SessionFactoryServiceRegistryImpl.java:98) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
    at org.hibernate.reactive.mutiny.impl.MutinySessionFactoryImpl.<init>(MutinySessionFactoryImpl.java:48) ~[hibernate-reactive-core-1.0.0.CR3.jar:1.0.0.CR3]
    at org.hibernate.reactive.session.impl.ReactiveSessionFactoryImpl.unwrap(ReactiveSessionFactoryImpl.java:51) ~[hibernate-reactive-core-1.0.0.CR3.jar:1.0.0.CR3]
    at cl.aurus.olimporeactivev3.client.infraestructure.repository.UserRepository.save(UserRepository.java:29) ~[classes/:na]
    at cl.aurus.olimporeactivev3.client.infraestructure.service.UserService.save(UserService.java:19) ~[classes/:na]
    at cl.aurus.olimporeactivev3.client.application.UserAuthRestController.authenticate(UserAuthRestController.java:92) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.web.reactive.result.method.InvocableHandlerMethod.lambda$invoke$0(InvocableHandlerMethod.java:146) ~[spring-webflux-5.3.6.jar:5.3.6]
    at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:125) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1815) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.MonoZip$ZipCoordinator.signal(MonoZip.java:251) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.MonoZip$ZipInner.onNext(MonoZip.java:336) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.onNext(MonoPeekTerminal.java:180) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onNext(FluxDefaultIfEmpty.java:100) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:73) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1815) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:151) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onNext(FluxContextWrite.java:107) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onNext(FluxMapFuseable.java:295) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxFilterFuseable$FilterFuseableConditionalSubscriber.onNext(FluxFilterFuseable.java:337) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1815) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.MonoCollect$CollectSubscriber.onComplete(MonoCollect.java:159) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:259) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.netty.channel.FluxReceive.terminateReceiver(FluxReceive.java:469) ~[reactor-netty-core-1.0.6.jar:1.0.6]
    at reactor.netty.channel.FluxReceive.drainReceiver(FluxReceive.java:261) ~[reactor-netty-core-1.0.6.jar:1.0.6]
    at reactor.netty.channel.FluxReceive.request(FluxReceive.java:130) ~[reactor-netty-core-1.0.6.jar:1.0.6]
    at reactor.core.publisher.FluxMap$MapSubscriber.request(FluxMap.java:162) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxPeek$PeekSubscriber.request(FluxPeek.java:137) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxMap$MapSubscriber.request(FluxMap.java:162) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.MonoCollect$CollectSubscriber.onSubscribe(MonoCollect.java:103) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxMap$MapSubscriber.onSubscribe(FluxMap.java:92) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxPeek$PeekSubscriber.onSubscribe(FluxPeek.java:170) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.core.publisher.FluxMap$MapSubscriber.onSubscribe(FluxMap.java:92) ~[reactor-core-3.4.5.jar:3.4.5]
    at reactor.netty.channel.FluxReceive.startReceiver(FluxReceive.java:168) ~[reactor-netty-core-1.0.6.jar:1.0.6]
    at reactor.netty.channel.FluxReceive.lambda$subscribe$2(FluxReceive.java:147) ~[reactor-netty-core-1.0.6.jar:1.0.6]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164) ~[netty-common-4.1.63.Final.jar:4.1.63.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472) ~[netty-common-4.1.63.Final.jar:4.1.63.Final]
    at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:384) ~[netty-transport-native-epoll-4.1.63.Final-linux-x86_64.jar:4.1.63.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.63.Final.jar:4.1.63.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.63.Final.jar:4.1.63.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.63.Final.jar:4.1.63.Final]
    at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]

Persistance.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="***">
        <provider>org.hibernate.reactive.provider.ReactivePersistenceProvider</provider>

        <!--DB conection-->
        <properties>

            <!-- PostgreSQL -->
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://***:***/***"/>

            <!-- Credentials -->
            <property name="javax.persistence.jdbc.user" value="****"/>
            <property name="javax.persistence.jdbc.password" value="*****"/>

            <!-- The Vert.x SQL Client connection pool size -->
            <property name="hibernate.connection.pool_size" value="100"/>

            <!-- Automatic schema export -->
            <property name="javax.persistence.schema-generation.database.action" value="none"/>

            <!--If load data needed-->
            <property name="javax.persistence.sql-load-script-source" value="data.sql" />

            <!-- SQL statement logging -->
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.highlight_sql" value="true"/>
        </properties>

    </persistence-unit>
</persistence>
2个回答

尝试像这样创建 SessionFactory:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("***");
Mutiny.SessionFactory factory = emf.unwrap(Mutiny.SessionFactory.class);
Issa Khodadadi
2021-08-08

我有一个工作示例来演示 Hibernate Reactive/Spring/Vertx,请在此处检查 完整源代码

只需声明一个 Mutiny.SessionFactory bean,然后将其作为普通 Spring bean 注入到您的存储库类中。

@Bean
    public Mutiny.SessionFactory sessionFactory() {
        return Persistence.createEntityManagerFactory("blogPU")
            .unwrap(Mutiny.SessionFactory.class);
    }
Hantsy
2021-08-26