gpt4 book ai didi

java - HikariCP 连接池未关闭

转载 作者:行者123 更新时间:2023-11-29 12:17:48 27 4
gpt4 key购买 nike

问题描述

我正在 Play Framework 应用程序中运行单元测试。经过几次测试后,它们开始失败,原因是达到了 100 个打开连接的 PostgreSQL 限制。每个单元测试都有一个 10 的连接池,因此在 10 个单元测试之后,失败开始。请记住,测试运行得非常快,并且在不到一秒的时间内多次创建和关闭池。使用调试日志记录,我没有看到任何意外行为(或者我没有启用所有调试信息)。

管理连接池

我自己不管理连接池(它由 Play Framework 管理),我可以在日志中看到连接池在每次单元测试后关闭。

[info] application - Creating Pool for datasource 'default'
[info] p.a.d.HikariCPConnectionPool - datasource [default] bound to JNDI as DefaultDS
[info] application - ApplicationTimer demo: Starting application at 2017-04-20T08:50:46.096Z
[info] application - ApplicationTimer demo: Stopping application at 2017-04-20T08:50:46.129Z after 1s.
[info] application - Shutting down connection pool.

在数据库的图表中,您可以看到池如何在每次单元测试后启动连接,并且在失败启动后突然,打开的连接立即关闭。 我没有看到任何连接在测试之间关闭。

PSQL Stats

示例单元测试

因为我找不到我正在做的单元测试的任何好的例子,我不能确认我的单元测试是完美无缺的(在这个意义上:我正确地使用了 Play Framework 提供的工具)。也许您会发现框架本身的一些误用会阻止连接池关闭。

public class UserPersistenceTesting {
@Inject
private JPAApi jpaApi;

@Before
public void setup() {
GuiceApplicationBuilder builder = new GuiceApplicationLoader()
.builder(new ApplicationLoader.Context(Environment.simple()));
Guice.createInjector(builder.applicationModule()).injectMembers(this);
}

@After
public void teardown() {
jpaApi.shutdown();
}

@Test
public void testRegisterNewUser() {
this.jpaApi.withTransaction(() -> {
UsersRepository repository = new UsersRepositoryJPA(jpaApi);
Date creationDate = new Date();
User expectedUser = new User(null, "max.muster@hsr.ch", "Muster", "Max", creationDate, Gender.MALE);
repository.registerUser(expectedUser);

User actualUser = repository.getUserById(expectedUser.getId());

assertEquals(expectedUser, actualUser);
});
}
}

我觉得是什么问题

我感觉池的“关闭”和连接释放之间存在延迟。因为单元测试运行得如此之快,所以在数据库开始抛出异常之前无法释放池。

我排除了什么

以下是已排除的错误列表:

  • 同时运行超过 10 个并发测试并填满连接池。 -> 我已禁用并发测试,但错误仍然发生。

发生异常

[error] o.p.Driver - Connection error:
org.postgresql.util.PSQLException: FATAL: tut mir leid, schon zu viele Verbindungen
Ort: Datei: proc.c, Routine: InitProcess, Zeile: 340.
Server SQLState: 53300
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:438)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
at org.postgresql.Driver.makeConnection(Driver.java:431)
at org.postgresql.Driver.connect(Driver.java:247)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:95)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:101)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:341)
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:506)
[error] Test integration.database.EventPersictenceTesting.testRegisterNewEvent failed: play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]], took 0.094 sec
[error] at play.api.Configuration$.configError(Configuration.scala:155)
[error] at play.api.Configuration.reportError(Configuration.scala:808)
[error] at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:48)
[error] at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:42)
[error] at scala.collection.immutable.List.foreach(List.scala:381)
[error] at play.api.db.DefaultDBApi.connect(DefaultDBApi.scala:42)
[error] at play.api.db.DBApiProvider.get$lzycompute(DBModule.scala:72)
[error] at play.api.db.DBApiProvider.get(DBModule.scala:62)
[error] at play.api.db.DBApiProvider.get(DBModule.scala:58)
[error] at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
[error] at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:72)
[error] at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:61)
[error] at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:62)
[error] at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
[error] at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
[error] at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:104)
[error] at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)
[error] at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:267)
[error] at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
[error] at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103)
[error] at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
[error] at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:145)
[error] at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
[error] at com.google.inject.internal.FactoryProxy.get(FactoryProxy.java:56)
[error] at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:54)
[error] at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:132)
[error] at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:93)
[error] at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:80)
[error] at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103)
[error] at com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:80)
[error] at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:62)
[error] at com.google.inject.internal.InjectorImpl.injectMembers(InjectorImpl.java:984)
[error] at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
[error] at com.google.inject.util.Providers$GuicifiedProviderWithDependencies$$FastClassByGuice$$2a7177aa.invoke(<generated>)
[error] at com.google.inject.internal.cglib.reflect.$FastMethod.invoke(FastMethod.java:53)
[error] at com.google.inject.internal.SingleMethodInjector$1.invoke(SingleMethodInjector.java:57)
[error] at com.google.inject.internal.SingleMethodInjector.inject(SingleMethodInjector.java:91)
[error] at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:132)
[error] at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:93)
[error] at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:80)
[error] at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1092)
[error] at com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:80)
[error] at com.google.inject.internal.Initializer$InjectableReference.get(Initializer.java:174)
[error] at com.google.inject.internal.Initializer.injectAll(Initializer.java:108)
[error] at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:174)
[error] at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
[error] at com.google.inject.Guice.createInjector(Guice.java:96)
[error] at com.google.inject.Guice.createInjector(Guice.java:84)
[error] at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:181)
[error] at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:123)
[error] at play.api.test.FakeApplication.<init>(Fakes.scala:209)
[error] at play.test.FakeApplication.<init>(FakeApplication.java:51)
[error] at play.test.Helpers.fakeApplication(Helpers.java:124)
[error] at play.test.WithApplication.provideFakeApplication(WithApplication.java:46)
[error] at play.test.WithApplication.provideApplication(WithApplication.java:33)
[error] at play.test.WithApplication.startPlay(WithApplication.java:51)
[error] ...
[error] Caused by: Configuration error: Configuration error[Failed to initialize pool: FATAL: tut mir leid, schon zu viele Verbindungen
[error] Ort: Datei: proc.c, Routine: InitProcess, Zeile: 340.
[error] Server SQLState: 53300]
[error] at play.api.Configuration$.configError(Configuration.scala:155)
[error] at play.api.PlayConfig.reportError(Configuration.scala:999)
[error] at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:70)
[error] at play.api.db.PooledDatabase.createDataSource(Databases.scala:199)
[error] at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:123)
[error] at play.api.db.DefaultDatabase.dataSource(Databases.scala:121)
[error] at play.api.db.DefaultDatabase.getConnection(Databases.scala:142)
[error] at play.api.db.DefaultDatabase.getConnection(Databases.scala:138)
[error] at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:44)
[error] ... 88 more
[error] Caused by: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: FATAL: tut mir leid, schon zu viele Verbindungen
[error] Ort: Datei: proc.c, Routine: InitProcess, Zeile: 340.
[error] Server SQLState: 53300
[error] at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:512)
[error] at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:105)
[error] at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:71)
[error] at play.api.db.HikariCPConnectionPool$$anonfun$1.apply(HikariCPModule.scala:58)
[error] at play.api.db.HikariCPConnectionPool$$anonfun$1.apply(HikariCPModule.scala:54)
[error] at scala.util.Try$.apply(Try.scala:192)
[error] at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:54)
[error] ... 94 more
[error] Caused by: org.postgresql.util.PSQLException: FATAL: tut mir leid, schon zu viele Verbindungen
[error] Ort: Datei: proc.c, Routine: InitProcess, Zeile: 340.
[error] Server SQLState: 53300
[error] at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:438)
[error] at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)
[error] at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
[error] at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
[error] at org.postgresql.Driver.makeConnection(Driver.java:431)
[error] at org.postgresql.Driver.connect(Driver.java:247)
[error] at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:95)
[error] at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:101)
[error] at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:341)
[error] at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:506)
[error] ... 100 more
[error] Failed: Total 18, Failed 6, Errors 0, Passed 12
[error] Failed tests:
[error] integration.database.EventPersictenceTesting
[error] integration.rest.IntegrationTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 31 s, completed 20.04.2017 10:50:52

最佳答案

这是一个老问题,我想用我记得的解决方案(或者至少它现在是这样工作的)来回答它。

当我观看原始问题中的代码时,我没有正确使用 Play Test 框架。我的类现在扩展了 WithApplication。我不再将 JPAApi 注入(inject)到测试类中,而是从测试方法内部获取它。我不再需要 setup() 方法了。重构后的类看起来像这样:

public class UserPersistenceTesting extends WithApplication {

@Test
public void testRegisterNewUser() {
final JPAApi jpaApi = this.app.injector().instanceOf(JPAApi.class);
jpaApi.withTransaction(() -> {
final UsersRepositoryJPA repository = new UsersRepositoryJPA(jpaApi);
final Date creationDate = new Date();
final User expectedUser = new User(null, "max.muster@hsr.ch", "Muster", "Max", creationDate, Gender.MALE);
repository.registerUser(expectedUser);

final User actualUser = repository.getUserById(expectedUser.getId());

assertEquals(expectedUser, actualUser);
});
}

关于java - HikariCP 连接池未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515195/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com