嵌入式 PostgreSQL 在 Quarkus 测试中无法连接。

huangapple go评论75阅读模式
英文:

Embedded postgres in Quarkus test does not connect

问题

Trying to write up a very simple db IT test using quarkus and an embedded postgres db. But when I run the test it seems to be trying to connect to the default `localhost:5432`. Debugging through the code, there is a different port assigned to the embedded db yet the code flips out saying it can't connect to `localhost:5432` and I have no idea why.
Heres what I have done so far

**PostgresDatabaseTestResource class**
```java
public class PostgresDatabaseTestResource implements QuarkusTestResourceLifecycleManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(PostgresDatabaseTestResource.class);
    private EmbeddedPostgres postgres;

    @Override
    public Map<String, String> start() {
        final String userName = System.getProperty("user.name");
        if ("root".equals(userName)) {
            throw new IllegalStateException("Cannot provision Ephemeral Postgres when running as user: " + userName);
        }
        try {
            postgres = EmbeddedPostgres.builder().start();
        } catch (IOException e) {
            throw new RuntimeException("Could not start Ephemeral Postgres", e);
        }
        Map<String, String> props = new HashMap<>();
        props.put("quarkus.datasource.url", postgres.getJdbcUrl("postgres", "postgres"));
        props.put("quarkus.datasource.username", "postgres");
        props.put("quarkus.datasource.password", "");
        props.put("quarkus.datasource.driver", Driver.class.getName());

        return props;
    }

    @Override
    public void stop() {
        if (postgres != null) {
            try {
                postgres.close();
            } catch (IOException e) {
                LOGGER.warn("Could not stop Ephemeral Postgres", e);
            }
            postgres = null;
        }
    }
}

DatabaseManagerIT class

@QuarkusTest
@QuarkusTestResource(PostgresDatabaseTestResource.class)
public class DatabaseManagerIT {

    @Inject
    JdbiProvider dbi;

    @Test
    public void testMigration() {
        Jdbi jdbi = dbi.get();
        assertNotNull(jdbi);
    }
}

Test logs and exception

Connected to the target VM, address: '127.0.0.1:47369', transport: 'socket'
2020-09-01 21:01:12,864 WARN  [io.qua.dep.QuarkusAugmentor] (main) Using Java versions older than 11 to build Quarkus applications is deprecated and will be disallowed in a future release!
...
Caused by: java.net.ConnectException: Connection refused (Connection refused)
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
...
	at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
...

I can see in the logs that the db is listening on port 45087. But then in the exception it's...


<details>
<summary>英文:</summary>

Trying to write up a very simple db IT test using quarkus and an embedded postgres db. But when I run the test it seems to be trying to connect to the default `localhost:5432`. Debugging through the code, there is a different port assigned to the embedded db yet the code flips out saying it can&#39;t connect to `localhost:5432` and I have no idea why. 
Heres what I have done so far

**PostgresDatabaseTestResource class**

public class PostgresDatabaseTestResource implements QuarkusTestResourceLifecycleManager {

private static final Logger LOGGER = LoggerFactory.getLogger(PostgresDatabaseTestResource.class);
private EmbeddedPostgres postgres;

@Override
public Map&lt;String, String&gt; start() {
    final String userName = System.getProperty(&quot;user.name&quot;);
    if (&quot;root&quot;.equals(userName)) {
        throw new IllegalStateException(&quot;Cannot provision Ephemeral Postgres when running as user: &quot; + userName);
    }
    try {
        postgres = EmbeddedPostgres.builder().start();
    } catch (IOException e) {
        throw new RuntimeException(&quot;Could not start Ephemeral Postgres&quot;, e);
    }
    Map&lt;String, String&gt; props = new HashMap&lt;&gt;();
    props.put(&quot;quarkus.datasource.url&quot;, postgres.getJdbcUrl(&quot;postgres&quot;, &quot;postgres&quot;));
    props.put(&quot;quarkus.datasource.username&quot;, &quot;postgres&quot;);
    props.put(&quot;quarkus.datasource.password&quot;, &quot;&quot;);
    props.put(&quot;quarkus.datasource.driver&quot;, Driver.class.getName());

    return props;
}

@Override
public void stop() {
    if (postgres != null) {
        try {
            postgres.close();
        } catch (IOException e) {
            LOGGER.warn(&quot;Could not stop Ephemeral Postgres&quot;, e);
        }
        postgres = null;
    }
}

}


**DatabaseManagerIT class**

@QuarkusTest
@QuarkusTestResource(PostgresDatabaseTestResource.class)
public class DatabaseManagerIT {

@Inject
JdbiProvider dbi;

@Test
public void testMigration() {
    Jdbi jdbi = dbi.get();
    assertNotNull(jdbi);
}

}


**Test logs and exception**

Connected to the target VM, address: '127.0.0.1:47369', transport: 'socket'
2020-09-01 21:01:12,864 WARN [io.qua.dep.QuarkusAugmentor] (main) Using Java versions older than 11 to build Quarkus applications is deprecated and will be disallowed in a future release!
2020-09-01 21:01:14,304 INFO [io.qua.fly.FlywayProcessor] (build-23) Adding application migrations in path '/home/ddold/Workspace/shopik/platform/target/classes/sql' using protocol 'file'
2020-09-01 21:01:15,000 INFO [io.qua.arc.pro.BeanProcessor] (build-4) Found unrecommended usage of private members (use package-private instead) in application beans:
- @Inject field io.shopik.FlywayMigrationReadinessHealthCheck#databaseManager
2020-09-01 21:01:15,311 INFO [com.ope.db.pos.emb.EmbeddedPostgres] (main) Detected a Linux x86_64 system
2020-09-01 21:01:15,446 INFO [com.ope.db.pos.emb.EmbeddedPostgres] (main) Postgres binaries at /tmp/embedded-pg/PG-06e3a92a2edb6ddd6dbdf5602d0252ca
2020-09-01 21:01:15,469 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) The files belonging to this database system will be owned by user "ddold".
2020-09-01 21:01:15,469 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) This user must also own the server process.
2020-09-01 21:01:15,469 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315))
2020-09-01 21:01:15,470 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) The database cluster will be initialized with locale "en_GB.UTF-8".
2020-09-01 21:01:15,470 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) The default text search configuration will be set to "english".
2020-09-01 21:01:15,470 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315))
2020-09-01 21:01:15,470 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) Data page checksums are disabled.
2020-09-01 21:01:15,470 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315))
2020-09-01 21:01:15,470 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) fixing permissions on existing directory /tmp/epg2137681061263626727 ... ok
2020-09-01 21:01:15,470 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) creating subdirectories ... ok
2020-09-01 21:01:15,477 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) selecting default max_connections ... 100
2020-09-01 21:01:15,489 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) selecting default shared_buffers ... 128MB
2020-09-01 21:01:15,489 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) selecting dynamic shared memory implementation ... posix
2020-09-01 21:01:15,862 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) creating configuration files ... ok
2020-09-01 21:01:16,008 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) running bootstrap script ... ok
2020-09-01 21:01:16,518 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) performing post-bootstrap initialization ... ok
2020-09-01 21:01:18,702 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) syncing data to disk ... ok
2020-09-01 21:01:18,702 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315))
2020-09-01 21:01:18,702 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) Success. You can now start the database server using:
2020-09-01 21:01:18,702 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315))
2020-09-01 21:01:18,702 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315)) /tmp/embedded-pg/PG-06e3a92a2edb6ddd6dbdf5602d0252ca/bin/pg_ctl -D /tmp/epg2137681061263626727 -l logfile start
2020-09-01 21:01:18,702 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:initdb] (log:pid(29315))
2020-09-01 21:01:18,712 INFO [com.ope.db.pos.emb.EmbeddedPostgres] (main) 620dfebb-f502-4036-bbb9-6cf921f86af4 initdb completed in 00:00:03.257
2020-09-01 21:01:18,716 INFO [com.ope.db.pos.emb.EmbeddedPostgres] (main) 620dfebb-f502-4036-bbb9-6cf921f86af4 postmaster started as java.lang.UNIXProcess@31e72cbc on port 45087. Waiting up to PT10S for server startup to finish.
2020-09-01 21:01:18,726 INFO [pg-620dfebb-f502-4036-bbb9-6cf921f86af4] (log:pid(29331)) waiting for server to start....2020-09-01 21:01:18.726 BST [29335] LOG: listening on IPv4 address "127.0.0.1", port 45087
2020-09-01 21:01:18,726 INFO [pg-620dfebb-f502-4036-bbb9-6cf921f86af4] (log:pid(29331)) 2020-09-01 21:01:18.726 BST [29335] LOG: listening on Unix socket "/tmp/.s.PGSQL.45087"
2020-09-01 21:01:18,737 INFO [pg-620dfebb-f502-4036-bbb9-6cf921f86af4] (log:pid(29331)) 2020-09-01 21:01:18.737 BST [29336] LOG: database system was shut down at 2020-09-01 21:01:16 BST
2020-09-01 21:01:18,739 INFO [pg-620dfebb-f502-4036-bbb9-6cf921f86af4] (log:pid(29331)) 2020-09-01 21:01:18.739 BST [29335] LOG: database system is ready to accept connections
2020-09-01 21:01:18,744 INFO [pg-620dfebb-f502-4036-bbb9-6cf921f86af4] (log:pid(29331)) 2020-09-01 21:01:18.744 BST [29343] LOG: incomplete startup packet
2020-09-01 21:01:18,815 INFO [com.ope.db.pos.emb.EmbeddedPostgres] (main) 620dfebb-f502-4036-bbb9-6cf921f86af4 postmaster startup finished in 00:00:00.101
2020-09-01 21:04:20,186 INFO [pg-620dfebb-f502-4036-bbb9-6cf921f86af4] (log:pid(29331)) done
2020-09-01 21:04:35,804 INFO [io.agr.pool] (main) Datasource '<default>': Initial size smaller than min. Connections will be created when necessary
2020-09-01 21:04:35,817 INFO [org.fly.cor.int.lic.VersionPrinter] (main) Flyway Community Edition 6.4.4 by Redgate
2020-09-01 21:04:35,843 WARN [io.agr.pool] (Agroal_6402948291) Datasource '<default>': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
2020-09-01 21:04:35,845 ERROR [io.qua.application] (main) Failed to start application: org.flywaydb.core.internal.exception.FlywaySqlException:
Unable to obtain connection from database: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

SQL State : 08001
Error Code : 0
Message : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:65)
at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.&lt;init&gt;(JdbcConnectionFactory.java:80)
at org.flywaydb.core.Flyway.execute(Flyway.java:456)
at org.flywaydb.core.Flyway.migrate(Flyway.java:159)
at io.quarkus.flyway.runtime.FlywayRecorder.doStartActions(FlywayRecorder.java:53)
at io.quarkus.deployment.steps.FlywayProcessor$createBeansAndStartActions-1520831253.deploy_0(FlywayProcessor$createBeansAndStartActions-1520831253.zig:76)
at io.quarkus.deployment.steps.FlywayProcessor$createBeansAndStartActions-1520831253.deploy(FlywayProcessor$createBeansAndStartActions-1520831253.zig:36)
at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:436)
at io.quarkus.runtime.Application.start(Application.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.quarkus.runner.bootstrap.StartupActionImpl.run(StartupActionImpl.java:223)
at io.quarkus.test.junit.QuarkusTestExtension.doJavaStart(QuarkusTestExtension.java:198)
at io.quarkus.test.junit.QuarkusTestExtension.ensureStarted(QuarkusTestExtension.java:406)
at io.quarkus.test.junit.QuarkusTestExtension.beforeAll(QuarkusTestExtension.java:439)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$7(ClassBasedTestDescriptor.java:359)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:359)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:189)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:78)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:132)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 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:280)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.Driver.connect(Driver.java:256)
at io.agroal.pool.ConnectionFactory.createConnection(ConnectionFactory.java:200)
at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:419)
at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:401)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at io.agroal.pool.util.PriorityScheduledExecutor.beforeExecute(PriorityScheduledExecutor.java:65)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.postgresql.core.PGStream.<init>(PGStream.java:70)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
... 13 more

2020-09-01 21:04:35,987 INFO [com.ope.db.pos.emb.EmbeddedPostgres] (main) 620dfebb-f502-4036-bbb9-6cf921f86af4 shut down postmaster in 00:00:00.133
2020-09-01 21:04:35,987 INFO [init-620dfebb-f502-4036-bbb9-6cf921f86af4:pg_ctl] (log:pid(29434)) waiting for server to shut down.... done

java.lang.RuntimeException: java.lang.RuntimeException: Failed to start quarkus

at io.quarkus.test.junit.QuarkusTestExtension.throwBootFailureException(QuarkusTestExtension.java:428)
at io.quarkus.test.junit.QuarkusTestExtension.beforeEach(QuarkusTestExtension.java:310)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachCallbacks$1(TestMethodTestDescriptor.java:161)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$5(TestMethodTestDescriptor.java:197)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:197)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachCallbacks(TestMethodTestDescriptor.java:160)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

Caused by: java.lang.RuntimeException: Failed to start quarkus
at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:585)
at io.quarkus.runtime.Application.start(Application.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.quarkus.runner.bootstrap.StartupActionImpl.run(StartupActionImpl.java:223)
at io.quarkus.test.junit.QuarkusTestExtension.doJavaStart(QuarkusTestExtension.java:198)
at io.quarkus.test.junit.QuarkusTestExtension.ensureStarted(QuarkusTestExtension.java:406)
at io.quarkus.test.junit.QuarkusTestExtension.beforeAll(QuarkusTestExtension.java:439)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$7(ClassBasedTestDescriptor.java:359)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:359)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:189)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:78)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:132)
... 29 more
Caused by: org.flywaydb.core.internal.exception.FlywaySqlException:
Unable to obtain connection from database: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

SQL State : 08001
Error Code : 0
Message : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:65)
at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.&lt;init&gt;(JdbcConnectionFactory.java:80)
at org.flywaydb.core.Flyway.execute(Flyway.java:456)
at org.flywaydb.core.Flyway.migrate(Flyway.java:159)
at io.quarkus.flyway.runtime.FlywayRecorder.doStartActions(FlywayRecorder.java:53)
at io.quarkus.deployment.steps.FlywayProcessor$createBeansAndStartActions-1520831253.deploy_0(FlywayProcessor$createBeansAndStartActions-1520831253.zig:76)
at io.quarkus.deployment.steps.FlywayProcessor$createBeansAndStartActions-1520831253.deploy(FlywayProcessor$createBeansAndStartActions-1520831253.zig:36)
at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:436)
... 44 more

Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 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:280)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.Driver.connect(Driver.java:256)
at io.agroal.pool.ConnectionFactory.createConnection(ConnectionFactory.java:200)
at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:419)
at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:401)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at io.agroal.pool.util.PriorityScheduledExecutor.beforeExecute(PriorityScheduledExecutor.java:65)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.postgresql.core.PGStream.<init>(PGStream.java:70)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
... 13 more


I can see in the logs that the db is listening on port `45087`. But then in the exception it&#39;s complaining about not being able to connect to port `5432`. Any ideas why I am seeing this?

**Edit**

**Quarkus Version**: 1.4.2.Final

**Application.properties**

quarkus.http.port=8082

DB

#quarkus.datasource.driver=com.mysql.cj.jdbc.Driver
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=<username>
quarkus.datasource.password=<password>
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/platform
quarkus.datasource.jdbc.acquisition-timeout=PT1M
quarkus.datasource.jdbc.min-size=1
quarkus.datasource.jdbc.max-size=16

Flyway

quarkus.flyway.migrate-at-start=true
quarkus.flyway.locations=sql

Logging

quarkus.log.console.enable=true
quarkus.log.console.level=ALL
quarkus.log.level=INFO
quarkus.log.category."io.shopik".level=DEBUG


</details>


# 答案1
**得分**: 3

你将新的数据源配置和旧的已弃用配置混合在一起,所以才导致它无法正常工作。

在你的 `application.properties` 文件中,你应该使用 `quarkus.datasource.jdbc.url`,而在你的 TestResource 中,你为 `quarkus.datasource.url` 设置了一个值。

在你的 TestResource 中,你应该使用:

* `quarkus.datasource.jdbc.url`
* `quarkus.datasource.jdbc.driver`(如果确实需要特定的驱动程序)

然后它应该能正常工作。

<details>
<summary>英文:</summary>

You&#39;re mixing the new datasource configuration and the old deprecated one thus why it&#39;s not working.

In your `application.properties`, you have `quarkus.datasource.jdbc.url` which is what you should use, whereas in your TestResource, you push a value for `quarkus.datasource.url`.

In your TestResource, you should use:

* `quarkus.datasource.jdbc.url`
* `quarkus.datasource.jdbc.driver` (if it really needs a specific driver)

Then it should work properly.

</details>



huangapple
  • 本文由 发表于 2020年9月2日 04:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63694884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定