fix hibernate connection pool only support mysql issue (#641)

This commit is contained in:
hzhang2 2017-08-07 17:26:37 -07:00 committed by Mars Lan
parent aa28eba78a
commit 7e3d4c220b
4 changed files with 148 additions and 75 deletions

View File

@ -18,7 +18,6 @@ import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence; import javax.persistence.Persistence;
@ -28,6 +27,7 @@ import play.Logger;
import play.Play; import play.Play;
import play.mvc.Controller; import play.mvc.Controller;
import play.mvc.Result; import play.mvc.Result;
import wherehows.dao.ConnectionPoolProperties;
import wherehows.dao.DaoFactory; import wherehows.dao.DaoFactory;
@ -35,39 +35,35 @@ public class Application extends Controller {
private static final String WHZ_APP_ENV = System.getenv("WHZ_APP_HOME"); private static final String WHZ_APP_ENV = System.getenv("WHZ_APP_HOME");
private static final String DB_WHEREHOWS_URL = Play.application().configuration().getString("db.wherehows.url"); private static final String DB_WHEREHOWS_URL = Play.application().configuration().getString("db.wherehows.url");
private static final String WHZ_DB_DSCLASSNAME =
Play.application().configuration().getString("hikaricp.dataSourceClassName");
private static final String DB_WHEREHOWS_USERNAME = private static final String DB_WHEREHOWS_USERNAME =
Play.application().configuration().getString("db.wherehows.username"); Play.application().configuration().getString("db.wherehows.username");
private static final String DB_WHEREHOWS_PASSWORD = private static final String DB_WHEREHOWS_PASSWORD =
Play.application().configuration().getString("db.wherehows.password"); Play.application().configuration().getString("db.wherehows.password");
private static final String DB_WHEREHOWS_DIALECT = Play.application().configuration().getString("hikaricp.dialect");
public static final EntityManagerFactory entityManagerFactory; public static final EntityManagerFactory entityManagerFactory;
static { static {
Map<String, String> properties = new HashMap<>(); entityManagerFactory = ConnectionPoolProperties.builder()
properties.put("hibernate.connection.provider_class", HikariCPConnectionProvider.class.getName()); .providerClass(HikariCPConnectionProvider.class.getName())
properties.put("hibernate.hikari.dataSourceClassName", "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); .dataSourceClassName(WHZ_DB_DSCLASSNAME)
properties.put("hibernate.hikari.dataSource.url", DB_WHEREHOWS_URL); .dataSourceURL(DB_WHEREHOWS_URL)
properties.put("hibernate.hikari.dataSource.user", DB_WHEREHOWS_USERNAME); .dataSourceUser(DB_WHEREHOWS_USERNAME)
properties.put("hibernate.hikari.dataSource.password", DB_WHEREHOWS_PASSWORD); .dataSourcePassword(DB_WHEREHOWS_PASSWORD)
properties.put("hibernate.hikari.dataSource.cachePrepStmts", "true"); .dialect(DB_WHEREHOWS_DIALECT)
properties.put("hibernate.hikari.dataSource.prepStmtCacheSize", "250"); .build()
properties.put("hibernate.hikari.dataSource.prepStmtCacheSqlLimit", "2048"); .buildEntityManagerFactory();
properties.put("hibernate.hikari.minimumIdle", "5");
properties.put("hibernate.hikari.maximumPoolSize", "10");
properties.put("hibernate.hikari.idleTimeout", "30000");
properties.put("hibernate.show_sql", "false");
properties.put("hibernate.dialect", "MySQL5");
properties.put("hibernate.jdbc.batch_size", "100");
properties.put("hibernate.order_inserts", "true");
properties.put("hibernate.order_updates", "true");
entityManagerFactory = Persistence.createEntityManagerFactory("default", properties);
} }
public static final DaoFactory daoFactory = createDaoFactory(); public static final DaoFactory daoFactory = createDaoFactory();
private static DaoFactory createDaoFactory() { private static DaoFactory createDaoFactory() {
try { try {
String className = Play.application().configuration().getString("dao.entityManagerFactory.class", DaoFactory.class.getCanonicalName()); String className = Play.application()
.configuration()
.getString("dao.entityManagerFactory.class", DaoFactory.class.getCanonicalName());
Class factoryClass = Class.forName(className); Class factoryClass = Class.forName(className);
Constructor<? extends DaoFactory> ctor = factoryClass.getConstructor(EntityManagerFactory.class); Constructor<? extends DaoFactory> ctor = factoryClass.getConstructor(EntityManagerFactory.class);
return ctor.newInstance(entityManagerFactory); return ctor.newInstance(entityManagerFactory);
@ -98,8 +94,7 @@ public class Application extends Controller {
BufferedReader br = new BufferedReader(new FileReader(commitFile)); BufferedReader br = new BufferedReader(new FileReader(commitFile));
commit = br.readLine(); commit = br.readLine();
} catch (IOException ioe) { } catch (IOException ioe) {
Logger.error("Error while reading commit file. Error message: " + Logger.error("Error while reading commit file. Error message: " + ioe.getMessage());
ioe.getMessage());
} }
//get all the files from a directory //get all the files from a directory

View File

@ -11,6 +11,10 @@ WHZ_DB_PASSWORD="wherehows"
# Fully qualified jdbc url # Fully qualified jdbc url
WHZ_DB_URL="jdbc:mysql://localhost/wherehows" WHZ_DB_URL="jdbc:mysql://localhost/wherehows"
#mysql setup
WHZ_DB_DSCLASSNAME = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
WHZ_DB_DIALECT = "org.hibernate.dialect.MySQLInnoDBDialect"
# Directory containing ETL job files # Directory containing ETL job files
WHZ_ETL_JOBS_DIR="/var/tmp/jobs" WHZ_ETL_JOBS_DIR="/var/tmp/jobs"

View File

@ -49,6 +49,16 @@ db.wherehows.url = ${WHZ_DB_URL}
db.wherehows.username = ${WHZ_DB_USERNAME} db.wherehows.username = ${WHZ_DB_USERNAME}
db.wherehows.password = ${WHZ_DB_PASSWORD} db.wherehows.password = ${WHZ_DB_PASSWORD}
# Hibernate HIKARICP connection driver name
# HikariCP is an open source JDBC connection pooling library
hikaricp.dataSourceClassName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
hikaricp.dataSourceClassName = ${WHZ_DB_DSCLASSNAME}
# dialect for DB
# example values for different db: org.hibernate.dialect.MySQLInnoDBDialect, org.hibernate.dialect.H2Dialect
hikaricp.dialect = "org.hibernate.dialect.MySQLInnoDBDialect"
hikaricp.dialect = ${WHZ_DB_DIALECT}
# You can expose this datasource via JNDI if needed (Useful for JPA) # You can expose this datasource via JNDI if needed (Useful for JPA)
# db.default.jndiName=DefaultDS # db.default.jndiName=DefaultDS

View File

@ -0,0 +1,64 @@
/**
* Copyright 2015 LinkedIn Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package wherehows.dao;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import lombok.Builder;
@Builder
public class ConnectionPoolProperties {
private final String providerClass;
private final String dataSourceClassName;
private final String dataSourceURL;
private final String dataSourceUser;
private final String dataSourcePassword;
@Builder.Default
private final String minimumIdle = "10";
@Builder.Default
private final String maximumPoolSize = "20";
@Builder.Default
private final String idleTimeout = "30000";
@Builder.Default
private final String showSQL = "false";
private final String dialect;
@Builder.Default
private final String jdbcBatchSize = "100";
@Builder.Default
private final String orderInserts = "true";
@Builder.Default
private final String orderUpdates = "true";
public EntityManagerFactory buildEntityManagerFactory() {
Map<String, String> properties = new HashMap<>();
properties.put("hibernate.connection.provider_class", providerClass);
properties.put("hibernate.hikari.dataSourceClassName", dataSourceClassName);
properties.put("hibernate.hikari.dataSource.url", dataSourceURL);
properties.put("hibernate.hikari.dataSource.user", dataSourceUser);
properties.put("hibernate.hikari.dataSource.password", dataSourcePassword);
properties.put("hibernate.hikari.minimumIdle", minimumIdle);
properties.put("hibernate.hikari.maximumPoolSize", maximumPoolSize);
properties.put("hibernate.hikari.idleTimeout", idleTimeout);
properties.put("hibernate.show_sql", showSQL);
properties.put("hibernate.dialect", dialect);
properties.put("hibernate.jdbc.batch_size", jdbcBatchSize);
properties.put("hibernate.order_inserts", orderInserts);
properties.put("hibernate.order_updates", orderUpdates);
return Persistence.createEntityManagerFactory("default", properties);
}
}