【问题标题】:Hibernate connection pool provider for Cloud SQLCloud SQL 的 Hibernate 连接池提供程序
【发布时间】:2014-10-25 15:59:07
【问题描述】:

我将 Hibernate 与 GAE 和 Cloud SQL 结合使用。

一切正常,但是当实例被唤醒时,有时连接数据库需要很长时间(最长15s)

在日志中我看到Hibernate建立连接的时间最多:

   I 17:46:35.936 org.hibernate.cfg.Configuration doConfigure: HHH000041: Configured SessionFactory: null
   W 17:46:36.209 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure: HHH000402: Using Hibernate built-in connection pool (not for production use!)
   I 17:46:36.243 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure: HHH000115: Hibernate connection pool size: 0
   I 17:46:36.244 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure: HHH000006: Autocommit mode: false
   I 17:46:36.244 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure: HHH000401: using driver [com.mysql.jdbc.GoogleDriver] at URL [jdbc:google:mysql://database?user=someUser]
   I 17:46:36.245 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure: HHH000046: Connection properties: {}
   I 17:46:45.356 org.hibernate.dialect.Dialect <init>: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect

这是产生这个的代码:

properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.GoogleDriver");
properties.put("hibernate.connection.url", "jdbc:google:mysql://" + SQL_INSTANCE + "/" + DATABASE + "?user=someUser");

// disable schema check
properties.put("hibernate.hbm2ddl.auto", ""); // disable
properties.put("hibernate.show_sql", false);            

// single session per request/thread
properties.put("hibernate.connection.pool_size", 0);
properties.put("hibernate.current_session_context_class", "thread");

Configuration cfg = new Configuration();
cfg.configure("/META-INF/hibernate.cfg.xml");
cfg.addProperties(properties);

sessionFactory = cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry());

以下方法提供会话

public Session getSession() {

        Session session;
        try {
            session = sessionFactory.getCurrentSession();
        }
        catch (org.hibernate.HibernateException he) {
            log.info("Opening new hibernate session.");
            session = sessionFactory.openSession();
        }

        return session;
    }

我将连接池大小设置为 0(按照建议),但显然使用了 Hibernates 内置的连接池管理器。

我的问题是,在此设置中应该使用哪个连接池管理器,以及可以做些什么来加快连接时间。

这种设置的最佳实践是什么,因为除了一些琐碎的例子我什么都找不到。

【问题讨论】:

    标签: java hibernate google-app-engine google-cloud-sql


    【解决方案1】:

    我推荐你使用HikariCP,也就是probably the fastest open-source CP

    我有一个GitHub 项目,您可以克隆并检查我的 Hibernate 本地事务配置的配置。虽然该示例使用 DBCP,但您只需将 DataSource 配置替换为 HikariDataSource one

    如果您只使用 Hibernate API 并且不想依赖 SPring,那么在您创建 HikariDataSource 对象后,您需要将其发送到 Hibernate 配置:

    @Override
    protected SessionFactory newSessionFactory() {
        Properties properties = new Properties();
        ...
        //data source settings
        properties.put("hibernate.connection.datasource", hikariDataSource);
        return new Configuration()
        .addProperties(properties)
        .addAnnotatedClass(SecurityId.class)
        .buildSessionFactory(
                new StandardServiceRegistryBuilder()
                .applySettings(properties)
        .build()
    );
    

    【讨论】:

    • 会试一试并比较启动时间......可能会成功。
    • 遗憾的是,我无法让它与 GAE 一起使用。它适用于本地主机,但不适用于生产。所以我使用了这个开源连接池提供程序:hibernate-gae.googlecode.com/svn/trunk/…
    • 它看起来像一个基本的连接池实现,但它可能使用其他外部池可能缺少的一些特定的 GAE 连接检索选项。
    【解决方案2】:

    ON_DEMAND SQL 实例将在一段空闲期后关闭。当实例关闭时,任何新连接都必须等待实例启动,然后才能完成连接。

    这可能是您遇到的延迟。您可以尝试将激活策略更改为始终,看看这是否会减少连接开销。如果您这样做,您还应该将计费计划更改为“包”,因为考虑到您的实例将连续运行,它会更便宜。

    Cloud SQL FAQ 对此进行了介绍。

    【讨论】:

    • 据我在日志中看到的.. 时间的主要部分用于建立连接。当新实例启动时,每个预热请求都会发生这种情况。这不是一个关键任务应用程序,因此启动并运行实例是多余的。我怀疑(但不能证明)内部的 hybernate 连接池管理器导致了延迟(因为它不是生产规模的实现,也不是用于 GAE 风格的)。我会尝试更改设置,看看会发生什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多