Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法连接到PostgreSQLContainer:java.io.EOFException_Java_Postgresql_Jdbc_Testcontainers_Postgresql 12 - Fatal编程技术网

无法连接到PostgreSQLContainer:java.io.EOFException

无法连接到PostgreSQLContainer:java.io.EOFException,java,postgresql,jdbc,testcontainers,postgresql-12,Java,Postgresql,Jdbc,Testcontainers,Postgresql 12,将我的容器创建为: public static PostgreSQLContainer<?> container = new PostgreSQLContainer<>("postgres:latest"); static { container.start(); System.setProperty("driver-class-name", container.getDriverClassName()); System.setProperty

将我的容器创建为:

public static PostgreSQLContainer<?> container = new PostgreSQLContainer<>("postgres:latest");

static {
    container.start();

    System.setProperty("driver-class-name", container.getDriverClassName());
    System.setProperty("spring.datasource.url", container.getJdbcUrl());
    System.setProperty("spring.datasource.username", container.getUsername());
    System.setProperty("spring.datasource.password", container.getPassword());
}
完整异常跟踪:

org.postgresql.util.PSQLException: The connection attempt failed.

    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
    at org.postgresql.Driver.makeConnection(Driver.java:458)
    at org.postgresql.Driver.connect(Driver.java:260)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.lenovo.edge.controller.UploadControllerTest.whenSelectQueryExecuted_thenResulstsReturned(UploadControllerTest.java:32)
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.io.EOFException
    at org.postgresql.core.PGStream.receiveChar(PGStream.java:337)
    at org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:411)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:135)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
    ... 29 more
org.postgresql.util.PSQLException:连接尝试失败。
位于org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292)
位于org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
位于org.postgresql.jdbc.PgConnection.(PgConnection.java:211)
位于org.postgresql.Driver.makeConnection(Driver.java:458)
位于org.postgresql.Driver.connect(Driver.java:260)
位于java.sql.DriverManager.getConnection(DriverManager.java:664)
位于java.sql.DriverManager.getConnection(DriverManager.java:247)
在com.lenovo.edge.controller.UploadControllerTest.whenSelectQueryExecuted\u然后返回结果(UploadControllerTest.java:32)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:50)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:47)
位于org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:363)
位于org.junit.runner.JUnitCore.run(JUnitCore.java:137)
位于com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
位于com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
位于com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
位于com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
原因:java.io.EOFException
位于org.postgresql.core.PGStream.receiveChar(PGStream.java:337)
位于org.postgresql.core.v3.ConnectionFactoryImpl.enableSSL(ConnectionFactoryImpl.java:411)
位于org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(connectionfactorympl.java:135)
位于org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
... 还有29个
所以很明显,我的应用程序也无法连接到它


有什么建议吗?

回答我自己的问题,问题是容器无法接收连接,我不确定实际原因,因为配置对我来说很好,但有效的解决方案是将容器初始化为

下面是我的工作解决方案示例,希望能对其他人有所帮助:

@ActiveProfiles("test")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
  "spring.datasource.url=jdbc:tc:postgresql:12.2:///test?TC_INITSCRIPT=init.sql",
  "spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver"
})
abstract class AbstractIntegrationTestCase {

  public static GenericContainer<?> container = new PostgreSQLContainer<>("postgres:12.2");

  static {
    container.start();
  }

  @Autowired
  protected TestRestTemplate restTemplate;
}
@ActiveProfiles(“测试”)
@SpringBootTest(webEnvironment=webEnvironment.RANDOM_端口,属性={
“spring.datasource.url=jdbc:tc:postgresql:12.2:///test?tc_INITSCRIPT=init.sql”,
“spring.datasource.driver类名=org.testcontainers.jdbc.ContainerDatabaseDriver”
})
抽象类AbstractIntegrationTestCase{
publicstaticgenericcontainer=newpostgresqlcontainer(“postgres:12.2”);
静止的{
container.start();
}
@自动连线
受保护的TestRestTemplate restTemplate;
}

您能告诉我们什么是
集装箱吗
info@YCF_L当然,你到底需要什么?所有的信息请url密码用户名driver@YCF_L驱动程序:org.postgresql.driver,url:jdbc:postgresql://localhost:32935/test?loggerLevel=OFF,用户:测试,密码:test@YCF_L我有你可以看到的默认设置
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
  "spring.datasource.url=jdbc:tc:postgresql:12.2:///test?TC_INITSCRIPT=init.sql",
  "spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver"
})
abstract class AbstractIntegrationTestCase {

  public static GenericContainer<?> container = new PostgreSQLContainer<>("postgres:12.2");

  static {
    container.start();
  }

  @Autowired
  protected TestRestTemplate restTemplate;
}