zonkyio / embedded-database-spring-test

A library for creating isolated embedded databases for Spring-powered integration tests.
Apache License 2.0
399 stars 37 forks source link

Unable to release database #192

Closed sandra-thieme closed 2 years ago

sandra-thieme commented 2 years ago

When executing tests that use the zonky postgres database it leads to a lot of messages like these:

2022-01-25 09:05:55.797  WARN 45822 --- [onPool-worker-5] .z.t.d.p.p.ZonkyPostgresDatabaseProvider : Unable to release 'srbnszldyljp' database
2022-01-25 09:05:55.801  WARN 45822 --- [onPool-worker-5] .z.t.d.p.p.ZonkyPostgresDatabaseProvider : Unable to release 'aqakjpwwxrie' database
2022-01-25 09:05:55.803  WARN 45822 --- [onPool-worker-5] .z.t.d.p.p.ZonkyPostgresDatabaseProvider : Unable to release 'reyhdyfelqvp' database
2022-01-25 09:05:55.819  WARN 45822 --- [onPool-worker-5] .z.t.d.p.p.ZonkyPostgresDatabaseProvider : Unable to release 'afwuxvvgtatv' database
2022-01-25 09:05:55.825  WARN 45822 --- [onPool-worker-5] .z.t.d.p.p.ZonkyPostgresDatabaseProvider : Unable to release 'zbcdlqbjtsya' database
2022-01-25 09:05:55.827  WARN 45822 --- [onPool-worker-5] .z.t.d.p.p.ZonkyPostgresDatabaseProvider : Unable to release 'vzplchluqymh' database
2022-01-25 09:05:55.837  WARN 45822 --- [onPool-worker-5] .z.t.d.p.p.ZonkyPostgresDatabaseProvider : Unable to release 'lmunlfgctomn' database

Exception that is happening (with trace logging enabled):

2022-01-26 12:19:35.870  WARN 47296 --- [onPool-worker-7] .z.t.d.p.p.ZonkyPostgresDatabaseProvider : Unable to release 'ollcnvrdjqij' database

org.postgresql.util.PSQLException: Connection to localhost:38451 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:301)
        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:466)
        at org.postgresql.Driver.connect(Driver.java:265)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
        at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:103)
        at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:87)
        at io.zonky.test.db.provider.support.AbstractEmbeddedDatabase.getConnection(AbstractEmbeddedDatabase.java:24)
        at io.zonky.test.db.provider.support.BlockingDatabaseWrapper.getConnection(BlockingDatabaseWrapper.java:61)
        at io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider$DatabaseInstance.executeStatement(ZonkyPostgresDatabaseProvider.java:184)
        at io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider$DatabaseInstance.lambda$dropDatabase$0(ZonkyPostgresDatabaseProvider.java:171)
        at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1736)
        at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1728)
        at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
        at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
        at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
        at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
        at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Caused by: java.net.ConnectException: Connection refused (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:238)
        at org.postgresql.core.PGStream.<init>(PGStream.java:98)
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:100)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:215)
        ... 19 common frames omitted

We're using io.zonky.test:embedded-postgres version 1.3.1 and io.zonky.test:embedded-database-spring-test version 2.1.1 and the @AutoConfigureEmbeddedDatabase annotation. Do you have any idea what could be going wrong?

tomix26 commented 2 years ago

This error means that you probably acquire a connection at the beginning of application or test start and hold it during the execution of multiple or all tests. The reason, why this is a problem, is that the library uses template databases for fast and cheap refreshing database content during tests. So databases are continuously created and released in a specific cycle. And the error above says that the current database cannot be released because there is still some active connection.

So please check your application or test logic to verify if there is some place that could hold a connection between execution of multiple tests.

tomix26 commented 2 years ago

Note that the function of the tests should not be affected. But it may cause the build machine to be slowed down or the memory to be run out.

tomix26 commented 2 years ago

I'm closing the issue due to inactivity, feel free to reopen it if the problem persists.