AppEngine上的HikariCP

时间:2018-12-17 09:46:35

标签: java spring-boot google-app-engine hikaricp

当使用HikariCP作为Appengine上的连接池部署Spring Boot应用程序时,执行一些请求时会出现一些与数据库(线程)有关的错误:

Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1008)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
    at com.zaxxer.hikari.pool.HikariPool.access$100(HikariPool.java:71)
    at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:706)
    at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:692)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    ... 1 more
Caused by: com.google.apphosting.api.ApiProxy$CallNotFoundException: Can't make API call memcache.Get in a thread that is neither the original request thread nor a thread created by ThreadManager

然后我发现AppEngine仅允许应用程序使用其ThreadFactory创建线程。因此,我确保将Hikari配置为使用AppEngine的Thread工厂,如下所示:

DataSource ds = new HikariDataSource();
try {
    final HikariConfig dataSourceConfig = new HikariConfig();
    dataSourceConfig.setDriverClassName(applicationProperties.getDatasource()
                                                            .getDriverClassName());
    dataSourceConfig.setJdbcUrl(applicationProperties.getDatasource()
                                                    .getUrl());
    dataSourceConfig.setUsername(applicationProperties.getDatasource()
                                                    .getUsername());
    dataSourceConfig.setPassword(applicationProperties.getDatasource()
                                                    .getPassword());

    dataSourceConfig.setRegisterMbeans(false);
    if (Objects.equal(ProfileResolver.getActiveCloudPlatform(env), ProfileConstants.SPRING_PROFILE_GCP)) {
        log.info("[GCP] Set 'com.google.appengine.api.ThreadManager.backgroundThreadFactory()' "
                    + "as the instance of the java.util.concurrent.ThreadFactory");
        dataSourceConfig.setThreadFactory(ThreadManager.backgroundThreadFactory());
    }
    ds = new HikariDataSource(dataSourceConfig);
} catch (final Exception e) {
    throw new IllegalStateException(e);
}
return ds;

它可以在我的本地appengine(DevServer)上运行,但是在部署时,由于Appengine的自动缩放模块不允许使用后台线程,因此在数据源初始化时会出现异常。

在AppEngine上使用HikariCP时是否可以保持“自动缩放”功能?

1 个答案:

答案 0 :(得分:0)

Java 8运行时在诸如App Engine先前版本之类的线程方面没有相同的限制。例如,此sample app使用HikariCP连接到Cloud SQL,并且无需自定义线程管理器即可工作。