无法创建到 Test Container for DB2 的 JDBC 连接
Posted
技术标签:
【中文标题】无法创建到 Test Container for DB2 的 JDBC 连接【英文标题】:Unable to create JDBC connection to Test Container for DB2 【发布时间】:2021-09-14 12:15:00 【问题描述】:我正在使用测试容器进行需要数据库的集成测试。我之前在 MSSQL 上做过这个并且工作正常,但这是我第一次尝试将它与 DB2 一起使用。
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>db2</artifactId>
<version>1.15.0</version>
<scope>test</scope>
</dependency>
我有这样的设置:
@BeforeClass
public static void setUpClass()
db2Container = new Db2Container("ibmcom/db2")
.acceptLicense()
.withDatabaseName("testDb2")
.withUsername("username")
.withPassword("password")
.withInitScript("initDB2.sql");
db2Container.start();
System.setProperty("general.datasource.jbhdb2p.jdbc-url", db2Container.getJdbcUrl());
System.setProperty("general.datasource.jbhdb2p.username", db2Container.getUsername());
System.setProperty("general.datasource.jbhdb2p.password", db2Container.getPassword());
我之前没有用 DB2 做过测试容器,所以我不确定所需的日志,但它似乎很好,直到它开始永远循环这 3 行:
10:10:18.569 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: 2331d2432c59900608324d8fe471d4e6fec3a6c337ebdf8bb0c532d361a07b66,false
10:10:18.570 [main] DEBUG org.testcontainers.shaded.com.github.dockerjava.core.exec.InspectContainerCmdExec - GET: DefaultWebTargetpath=[/containers/2331d2432c59900608324d8fe471d4e6fec3a6c337ebdf8bb0c532d361a07b66/json], queryParams=
10:10:18.578 [main] DEBUG ???? [ibmcom/db2:latest] - Trying to create JDBC connection using com.ibm.db2.jcc.DB2Driver to jdbc:db2://localhost:49193/testDb2 with properties: password=password, user=username
这是循环开始前的日志:
10:08:19.692 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Previous setup has not been detected. Creating the users...
10:08:19.692 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Creating users ...
10:08:19.837 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Creating instance ...
10:08:20.906 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:20.907 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DB2 installation is being initialized.
10:08:23.322 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:23.322 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Total estimated time for all tasks to be performed: 309 second(s)
10:08:23.322 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Total number of tasks to be performed: 4
10:08:23.337 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Estimated time 1 second(s)
10:08:23.337 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Description: Setting default global profile registry variables
10:08:23.337 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Task #1 start
10:08:23.359 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:23.359 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Task #1 end
10:08:23.383 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Estimated time 5 second(s)
10:08:23.383 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Description: Initializing instance list
10:08:23.383 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Task #2 start
10:08:23.965 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:23.965 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Task #2 end
10:08:23.979 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Estimated time 300 second(s)
10:08:23.979 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Description: Configuring DB2 instances
10:08:23.979 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Task #3 start
10:08:39.070 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Task #3 end
10:08:39.070 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:39.091 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:39.091 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: The execution completed successfully.
10:08:39.091 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:39.091 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Task #4 end
10:08:39.091 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Estimated time 3 second(s)
10:08:39.091 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Description: Updating global profile registry
10:08:39.091 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Task #4 start
10:08:40.739 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:40.739 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:40.739 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DBI1070I Program db2icrt completed successfully.
10:08:40.739 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:40.739 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:40.739 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DBI1446I The db2icrt command is running.
10:08:40.739 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: For more information see the DB2 installation log at "/tmp/db2icrt.log.71".
10:08:40.910 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: SQL1032N No start database manager command was issued. SQLSTATE=57019
10:08:40.910 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: 07/02/2021 14:08:41 0 0 SQL1032N No start database manager command was issued.
10:08:43.018 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Cataloging existing databases
10:08:43.040 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Applying Db2 license ...
10:08:43.040 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDERR: ls: cannot access /database/data/sa/NODE0000: No such file or directory
10:08:43.113 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:43.113 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: LIC1402I License added successfully.
10:08:43.113 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: LIC1426I This product is now licensed for use as outlined in your License Agreement. USE OF THE PRODUCT CONSTITUTES ACCEPTANCE OF THE TERMS OF THE IBM LICENSE AGREEMENT, LOCATED IN THE FOLLOWING DIRECTORY: "/opt/ibm/db2/V11.5/license/en_US.iso88591"
10:08:43.113 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:43.114 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:43.134 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Updating DBM CFG parameters ...
10:08:43.134 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Saving the checksum of the current nodelock file ...
10:08:44.528 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
10:08:44.530 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: successfully.
10:08:45.636 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: successfully.
10:08:45.636 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
10:08:46.731 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: successfully.
10:08:46.732 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
10:08:46.752 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Remounting /database with suid...
10:08:46.752 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: No Cgroup memory limit detected, instance memory will follow automatic tuning
10:08:51.381 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Code level is the same. No update/upgrade needed.
10:08:51.381 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Nothing appears in the Db2 directory. will skip update/upgrade.
10:08:51.440 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DB2 State : Operable
10:08:51.440 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:51.463 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT:
10:08:51.463 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: Starting DB2...
10:08:51.463 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DB2 has not been started
10:08:53.379 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: SQL1063N DB2START processing was successful.
10:08:53.379 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: 07/02/2021 14:08:53 0 0 SQL1063N DB2START processing was successful.
10:08:53.394 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Creating database testDb2 ...
10:08:53.394 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) User chose to create testDb2 database
10:10:15.143 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DB20000I The CREATE DATABASE command completed successfully.
10:10:16.771 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DB20000I The ACTIVATE DATABASE command completed successfully.
10:10:16.782 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Instance and database will not be auto configured. AUTOCONFIG has been set to false.
10:10:16.782 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Log archiving will not be configured as ARCHIVE_LOGS has been set to false.
10:10:18.307 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Skipping TEXT_SEARCH setup for database testDb2 because TEXT_SEARCH is not configured for the instance ...
10:10:18.519 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: ssh-keygen: generating new host keys: RSA2 RSA DSA ECDSA ED96529
10:10:18.534 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: 2
10:10:18.534 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DATA #10: unsigned integer, 8 bytes
10:10:18.534 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: 2
10:10:18.534 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DATA #9 : unsigned integer, 8 bytes
10:10:18.534 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: 4
10:10:18.534 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DATA #8 : signed integer, 4 bytes
10:10:18.535 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: 4
10:10:18.535 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DATA #7 : signed integer, 4 bytes
10:10:18.535 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: 8
10:10:18.535 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: DATA #6 : signed integer, 4 bytes
10:10:18.535 [main] DEBUG org.testcontainers.containers.output.WaitingConsumer - STDOUT: (*) Setup has completed.
10:10:18.537 [main] INFO ???? [ibmcom/db2:latest] - Container ibmcom/db2:latest started in PT2M2.3332179S
10:10:18.545 [main] INFO org.testcontainers.ext.ScriptUtils - Executing database script from initDB2.sql
我找到了一个存储库,其中已经编写了 DB2 集成测试,并尝试在我的机器上克隆和运行它。它提供的日志与我尝试创建的日志相同。这让我觉得我的问题可能是 docker/IDE 问题而不是代码问题。
谁能从这些日志中收集足够的信息来指导我一个方向?谢谢!
【问题讨论】:
让它运行一段时间后出现此错误:10:50:06.675 [main] ERROR org.testcontainers.jdbc.JdbcDatabaseDelegate - Could not obtain JDBC connection 10:50:06.686 [main] ERROR ???? [ibmcom/db2:latest] - Could not start container
是否为 TCPIP 配置了 Db2 实例 (db2inst1)?以用户 db2inst1、db2set -all
(应显示 DB2COMM=TCPIP...)和 db2 get dbm cfg | grep SVCENAME
的身份将 shell 放入容器中,应显示类似 50,000 或 60,000 的数字,或 /etc/services
中对应于端口号的单词条目。您的日志文件在连接 URL 中提到端口 49193,这不是默认值。
【参考方案1】:
您可以编写数据源 Bean 类集成测试,并根据来自 postgresqlContainer 或在您的情况下为 DB2 的参数创建数据源
@Configuration
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public class TestConfiguration
PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>(POSTGRES_TEST_IMAGE)
.withInitScript("somepath/init_postgresql.sql")
@Primary
@Bean
public DataSource dataSource()
HikariConfig hikariConfig=new HikariConfig();
hikariConfig.setJdbcUrl(postgres.getUrl());
hikariConfig.setUsername(postgres.getUsername());
hikariConfig.setPassword(postgres.getPassword());
hikariConfig.setDriverClassName(postgres.getDriverClassName());
HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig);
return hikariDataSource;
并将上面的配置类导入你的springboot测试,它会自动用TestContainer的Postgres数据源覆盖数据源。
你的测试应该是这样的
@ExtendWith(SpringExtension.clas)
@SpringBootTest(classes = DataSourceConfiguration.class, TestConfiguration.class)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public class RepoTest
@Autowired
TestRepository testRepository;
@Test
public void testRepo()
System.out.println("*****************" + testRepository.existsById(1L));
详情请参考此链接: https://medium.com/@arpitsharma14/testcontainer-springboot-tests-faa05b71a7dc
【讨论】:
以上是关于无法创建到 Test Container for DB2 的 JDBC 连接的主要内容,如果未能解决你的问题,请参考以下文章
ffmpeg:Codec for stream 0 does not use global headers but container format requires global headers
Docker macvlan host to container 互通