[英]Hibernate connection pool provider for Cloud SQL
我正在將Hibernate與GAE和Cloud SQL結合使用。
一切正常,但是當一個實例被喚醒時,有時需要很長時間才能連接到數據庫(最多15秒)
在日志中我看到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構建。
我的問題是,應該在此設置中使用哪個連接池管理器,以及還可以采取哪些措施來加快連接時間。
這種設置的最佳實踐是什么,因為除了一些簡單的例子我找不到任何東西。
我建議你使用HikariCP ,它可能是最快的開源CP 。
我有一個GitHub項目你可以克隆並檢查我的配置Hibernate本地事務配置。 雖然該示例使用DBCP,但您只需要使用HikariDataSource替換DataSource配置。
如果您只使用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()
);
ON_DEMAND SQL實例將在空閑時段后關閉。 當實例關閉時,任何新連接必須等待實例在連接完成之前啟動。
這可能是您遇到的延遲。 您可以嘗試將激活策略更改為ALWAYS,看看這是否會減少連接開銷。 如果您這樣做,您還應該將結算方案更改為“套餐”,因為考慮到您的實例將持續運行,它會更便宜。
Cloud SQL FAQ中對此進行了介紹。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.