在 spring-boot 集成测试中使用 H2 数据库的 JDBC 连接错误

Posted

技术标签:

【中文标题】在 spring-boot 集成测试中使用 H2 数据库的 JDBC 连接错误【英文标题】:JDBC connection error using H2 database in spring-boot integration tests 【发布时间】:2015-06-15 03:38:35 【问题描述】:

在我的集成测试中启动服务时,我收到 JDBC 连接错误 -

WARN 8784 --- [HelperThread-#2] c.m.v2.resourcepool.BasicResourcePool    : com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@57453b70 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 

org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost:59000" [90067-184]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:168)
    at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:450)
    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:327)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:108)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:92)
    at org.h2.Driver.connect(Driver.java:72)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1127)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1114)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1855)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)


    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at org.h2.util.NetUtils.createSocket(NetUtils.java:122)
    at org.h2.util.NetUtils.createSocket(NetUtils.java:102)
    at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:112)
    at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:446)
    ... 13 common frames omitted

这是应用程序属性文件 -

dataSource.driverClassName=org.h2.Driver
dataSource.url=jdbc:h2:tcp://localhost:59000/./target/servicedatabase-db;MODE=Oracle;IGNORECASE=TRUE;IFEXISTS=TRUE
dataSource.username=sa
dataSource.password=sa
dataSource.maxActive=2
dataSource.initialSize=1
dataSource.maxIdle=1

我无法理解导致问题的原因。任何指针表示赞赏。

【问题讨论】:

【参考方案1】:

在进行 maven clean 验证时,测试运行良好,但不是单独进行。暂时我们可以关闭这个话题。

【讨论】:

以上是关于在 spring-boot 集成测试中使用 H2 数据库的 JDBC 连接错误的主要内容,如果未能解决你的问题,请参考以下文章

Spring-Boot / H2 将数据库快照写入文件系统

为啥 H2 进行 spring-boot 测试,但它会将数据写入我的本地 mysql 数据库?

如何使用 Spring-Boot 播种 Spring-Security

在内存数据库中使用 H2 进行 Spring Boot 集成测试

使用 H2 DB 进行 Spring/Hibernate 集成测试

基于spring-boot的应用程序的单元+集成测试方案