我可以在Google App Engine上使用HikariCP吗? [英] Can i use HikariCP on Google App Engine

查看:107
本文介绍了我可以在Google App Engine上使用HikariCP吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在App Engine上寻找连接池解决方案,使用Cloud SQL,多个条目显示HikariCP应该可以工作,因为它允许外部ThreadFactory配置。例如;



配置会是这样的:

  import com.google.appengine.api.ThreadManager; 

...
HikariConfig lConfig = new HikariConfig();
config.setThreadFactory(ThreadManager.backgroundThreadFactory());
...

但有问题;

Google App Engine上的HikariCP



实际上,由于App Engine的受限制的沙箱环境,快速尝试暴露了多个问题。



问题依旧; 是否有人在Google App Engine上成功实现了HikariCP?

解决方案

是的,我在Google上实现了HikariCP App Engine,但有一些注意事项;

Google App Engine使用三种类型和实例类;


  • 自动缩放

  • 基本缩放

  • 手动缩放


  • $ b 类型基本和手动缩放允许后台线程,因此是HikariCP使用的唯一候选人。
    我已经使用Basic Sc​​aling,对于大致已知的用户群(例如每个部署实例有一个公司)没有问题。这种类型支持一些其他很好的功能,如会话支持和功能强大的B8实例(1024 MB / 4.8 GHz)。
    对于大多数我的应用程序,我更喜欢在多个实例之上的一个强大的初始化实例。


    $ b 属性 threadFactory 仅适用通过编程配置,所以我实现了自己的连接提供程序;

      public class MyConnectionProvider implements ConnectionProvider,可配置的,可停止的
    {

    ...

    public void configure(Map props)throws HibernateException
    {
    try
    {
    mHikariConfig = HikariConfigurationUtil.loadConfiguration(道具);

    if(SystemProperty.environment.value()== SystemProperty.Environment.Value.Production)
    {
    mHikariConfig.setDriverClassName(com.mysql.jdbc.GoogleDriver) ;
    mHikariConfig.setJdbcUrl(jdbc:google:mysql:// project-xxx:database / xxx);
    mHikariConfig.setThreadFactory(ThreadManager.backgroundThreadFactory());
    }
    else
    {
    mHikariConfig.setDriverClassName(com.mysql.jdbc.Driver);
    mHikariConfig.setJdbcUrl(jdbc:mysql:// localhost:3306 / xxx);
    }

    mHikariConfig.addDataSourceProperty(databaseName,xxx);
    mHikariConfig.setUsername(USERNAME);
    mHikariConfig.setPassword(PASSWD);
    mHikariConfig.setRegisterMbeans(false);

    mHikariConfig.setMaximumPoolSize(12);
    mHikariConfig.addDataSourceProperty(cachePrepStmts,true);
    mHikariConfig.addDataSourceProperty(useServerPrepStmts,true);
    mHikariConfig.addDataSourceProperty(prepStmtCacheSize,250);
    mHikariConfig.addDataSourceProperty(prepStmtCacheSqlLimit,2048);

    mHikariDataSource = new HikariDataSource(mHikariConfig);
    }
    catch(Exception e)
    {
    抛出新的HibernateException(e);
    }
    }

    ...

    }

    数据库名称已经在JdbcUrl中,但我必须再次指定它。
    另一个重要的配置设置是

      mHikariConfig.setRegisterMbeans(false); 

    这会禁用App Engine的受限制的Java管理扩展。


    Looking around for a connection pool solution on App Engine, with Cloud SQL, multiple entries show up with the suggestion that HikariCP should work because it allows for an external ThreadFactory configuration. For example;

    The configuration would be something like this:

    import com.google.appengine.api.ThreadManager;
    
    ...
    HikariConfig lConfig = new HikariConfig();
    config.setThreadFactory(ThreadManager.backgroundThreadFactory());
    ...
    

    But there are problems;

    HikariCP on Google App Engine

    And indeed, a quick attempt exposed multiple problems due to App Engine's restricted "sandbox" environment.

    So the question remains; did someone successfully implement HikariCP on Google App Engine?

    解决方案

    Yes i've implemented HikariCP on Google App Engine, but there are some considerations;

    Google App Engine uses three types and instance classes;

    • Automatic Scaling
    • Basic Scaling
    • Manual Scaling

    Only the types Basic and Manual Scaling allow background threads and are therefore the only candidates for use with HikariCP. I've used Basic Scaling which is no problem with a approximately known user base (for example one company for each deployed instance). This type enables some other nice features like session support and a powerful B8 instance (1024 MB / 4.8 GHz). For most of my applications i prefer one powerful initialized instance above multiple instances.

    Property threadFactory is only available via programmatic configuration, so i implemented my own connection provider;

    public class MyConnectionProvider implements ConnectionProvider, Configurable, Stoppable
    {
    
    ...
    
    public void configure(Map props) throws HibernateException
        {
            try
            {
                mHikariConfig = HikariConfigurationUtil.loadConfiguration(props);
    
                if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production)
                {
                    mHikariConfig.setDriverClassName("com.mysql.jdbc.GoogleDriver");
                    mHikariConfig.setJdbcUrl("jdbc:google:mysql://project-xxx:database/xxx");
                    mHikariConfig.setThreadFactory(ThreadManager.backgroundThreadFactory());
                }
                else
                {
                    mHikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
                    mHikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/xxx");
                }
    
                mHikariConfig.addDataSourceProperty("databaseName", "xxx");
                mHikariConfig.setUsername("USERNAME");
                mHikariConfig.setPassword("PASSWD");
                mHikariConfig.setRegisterMbeans(false);
    
                mHikariConfig.setMaximumPoolSize(12);
                mHikariConfig.addDataSourceProperty("cachePrepStmts", "true");
                mHikariConfig.addDataSourceProperty("useServerPrepStmts", "true");
                mHikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
                mHikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    
                mHikariDataSource = new HikariDataSource(mHikariConfig);
            }
            catch (Exception e)
            {
                throw new HibernateException(e);
            }
        }
    
    ...
    
    }
    

    The database name is already in the JdbcUrl but i had to specify it again. Another important configuration setting is

    mHikariConfig.setRegisterMbeans(false);
    

    this disables the on App Engine's restricted java management extensions.

    这篇关于我可以在Google App Engine上使用HikariCP吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆