当前位置: 首页 > 知识库问答 >
问题:

如何使Quarkus服务器在数据源无法连接到数据库或即使数据库关闭时启动

申屠瀚海
2023-03-14

我想启动我的quarkus应用程序,如果数据库关闭,它不会影响我的启动。

我的代码:https://github.com/uenderley/quarkus-datasources

  <properties>
    <compiler-plugin.version>3.8.1</compiler-plugin.version>
    <maven.compiler.parameters>true</maven.compiler.parameters>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <quarkus-plugin.version>1.11.3.Final</quarkus-plugin.version>
    <quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
    <quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
    <quarkus.platform.version>1.11.3.Final</quarkus.platform.version>
    <surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
  </properties>

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-hibernate-orm</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-hibernate-orm-panache</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-jdbc-postgresql</artifactId>
    </dependency>

Application.Properties

quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=postgres
quarkus.datasource.password=postgres
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5433/users_db
quarkus.datasource.health.enabled=false
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.datasource.jdbc.min-size=0

当postgres关闭时,我试图启动应用程序时出错

WARN  [org.hib.eng.jdb.env.int.JdbcEnvironmentInitiator] (Quarkus Main Thread) HHH000342: Could not obtain connection to query metadata: org.postgresql.util.PSQLException: Connection to localhost:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:225)
        at org.postgresql.Driver.makeConnection(Driver.java:465)
        at org.postgresql.Driver.connect(Driver.java:264)
        at io.agroal.pool.ConnectionFactory.createConnection(ConnectionFactory.java:200)
        at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:452)
        at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:434)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at io.agroal.pool.util.PriorityScheduledExecutor.beforeExecute(PriorityScheduledExecutor.java:65)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1126)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.ConnectException: Conexão recusada (Connection refused)
        at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
        at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.base/java.net.Socket.connect(Socket.java:609)
        at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
        at org.postgresql.core.PGStream.<init>(PGStream.java:95)
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
        ... 12 more

2021-03-03 14:21:56,494 WARN  [io.agr.pool] (agroal-11) Datasource '<default>': Connection to localhost:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
2021-03-03 14:21:56,495 WARN  [org.hib.eng.jdb.spi.SqlExceptionHelper] (Quarkus Main Thread) SQL Error: 0, SQLState: 08001
2021-03-03 14:21:56,495 ERROR [org.hib.eng.jdb.spi.SqlExceptionHelper] (Quarkus Main Thread) Connection to localhost:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
2021-03-03 14:21:56,526 ERROR [io.qua.run.Application] (Quarkus Main Thread) Failed to start application (with profile dev): java.net.ConnectException: Conexão recusada (Connection refused)
        at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
        at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.base/java.net.Socket.connect(Socket.java:609)
        at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
        at org.postgresql.core.PGStream.<init>(PGStream.java:95)
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:225)
        at org.postgresql.Driver.makeConnection(Driver.java:465)
        at org.postgresql.Driver.connect(Driver.java:264)
        at io.agroal.pool.ConnectionFactory.createConnection(ConnectionFactory.java:200)
        at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:452)
        at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:434)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at io.agroal.pool.util.PriorityScheduledExecutor.beforeExecute(PriorityScheduledExecutor.java:65)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1126)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)

Quarkus application exited with code 1
Press Enter to restart or Ctrl + C to quit
2021-03-03 14:21:56,528 INFO  [io.qua.dep.dev.IsolatedDevModeMain] (main) Attempting to start hot replacement endpoint to recover from previous Quarkus startup failure

我知道spring Boot上的这些属性:

spring.datasource.continue-on-error=true

initialization-mode=never

共有1个答案

万俟光临
2023-03-14

如果数据库仅在调用服务时关闭,则可能尝试通过方法注入AgroalDataSource以失败。

删除@inject并将其添加到服务方法中:

AgroalDataSource datasource = CDI.current().select(AgroalDataSource.class).get();

导入类:

import javax.enterprise.inject.spi.CDI;
 类似资料:
  • 我想通过pymongo阅读集合,但我得到以下错误: 回溯(最近的调用为last):文件“C://users/gsingh/pycharmprojects/mc/quizQuestionAnswers.py”,第21行,pprint.pprint(posts.find_one())文件“C:\users\gsingh\appdata\local\continuum\anaconda2\lib\sit

  • 刚才发现每次调用完接口都有一堆连接没关掉( 如下图 ): 于是写了一段本地代码无限循环跑发现单独执行一次是可以立刻关闭连接的即便循环跑也一直是 10-30 来回跳我估计就是每次循环好就关闭了问题不大( 代码如下 ): 但是我的 flask 应用中每次请求完连接都在一直没搞懂什么问题( 代码如下 ): 请求指点分析什么问题

  • 我是docker的新手,我想用Docker-compose启动一个keycloak容器。我正在使用一个mariadb和数据库启动成功。但是当启动keycloak容器时,它会出现这个错误。 原因:java.sql.sqlnontransientconnectionException:无法连接到address=(Host=Project-DB)(Port=3308)(Type=master):套接字无

  • 我刚刚把pgAdmin更新到4.23版,PostgreSQL更新到12版。我还删除了所有以前的数据库和pgAdmin安装。所以应该是全新安装(我也清理了之前版本的所有残余)。 现在,当我尝试连接到本地数据库服务器时,我甚至在键入密码之前就得到了这个:

  • 在我的项目中,当我运行后端时,使用Quarkus、Angular和PostgreSQL DB 这是我放在后端文件夹(Quarkus)中的“docker-compose.yml”文件: 但是当我构建一个Quarkus泊坞窗映像并尝试在泊坞窗容器中运行它时,它失败了!知道角度泊坞站容器运行良好,数据库也是如此。以下是我在运行容器后获得的错误日志: 使用 /opt/jboss/容器/java/运行/运行