【问题标题】:Why Play framework 2.5 does not use all connections in connection pool为什么 Play framework 2.5 不使用连接池中的所有连接
【发布时间】:2017-01-04 07:12:56
【问题描述】:

我已经使用 Ebean 设置了一个具有大量数据库访问权限的 play 2.5 项目。我将 hikaricp 配置为 maximumPoolSize=100 和 minimumIdle=100 和 hikaricp 池 100 连接成功,我还像这样配置线程池:

akka {
  actor {
    default-dispatcher {
      executor = "thread-pool-executor"
      throughput = 1
      thread-pool-executor {
        fixed-pool-size = 109 # db conn pool (100) + number of cores (8) + housekeeping (1)
      }
    }
  }
}

我使用 CompletionStage 实现异步操作:

public CompletionStage<Result> asyncDb() {
    CompletableFuture<Integer> future = new CompletableFuture<>().supplyAsync(() -> {
        Ebean.createSqlQuery("select sleep(3)").findUnique();
        return 0;
    });

    return future.thenApplyAsync(integer -> ok("done"));
}

我使用 sbt start 运行系统并使用 apache benchmark 对其进行测试:

ab -n 10000 -c 100 -r -k http://localhost:9000/asyncDb

问题是play只使用池中的7或8个连接,其他mysql连接空闲同时有请求,为什么?

【问题讨论】:

    标签: java playframework threadpool connection-pooling playframework-2.5


    【解决方案1】:

    Greg Methvin 在play google group 中回答了我的问题: 我认为问题在于 CompletableFuture.supplyAsync 不使用默认调度程序。而是使用 ForkJoinPool.commonPool()。 (见this link)

    您需要使用接受执行程序的the version

    对于执行者,你实际上有两个选择:

    1. 将 ActorSystem 传递给您的控制器并调用 actorSystem.dispatcher(),然后像上面那样配置它,或者

    2. 专门为您的数据库创建一个具有 100 个线程的单独执行程序,并保留默认执行程序来处理请求。

    在此处查看the documentation,了解线程池的不同选项:

    【讨论】:

      猜你喜欢
      • 2016-08-28
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 2011-06-20
      相关资源
      最近更新 更多