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,88 +27,84 @@ 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;
public class Application extends Controller { 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 DB_WHEREHOWS_USERNAME = private static final String WHZ_DB_DSCLASSNAME =
Play.application().configuration().getString("db.wherehows.username"); Play.application().configuration().getString("hikaricp.dataSourceClassName");
private static final String DB_WHEREHOWS_PASSWORD = private static final String DB_WHEREHOWS_USERNAME =
Play.application().configuration().getString("db.wherehows.password"); Play.application().configuration().getString("db.wherehows.username");
private static final String 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"); public static final DaoFactory daoFactory = createDaoFactory();
properties.put("hibernate.show_sql", "false");
properties.put("hibernate.dialect", "MySQL5"); private static DaoFactory createDaoFactory() {
properties.put("hibernate.jdbc.batch_size", "100"); try {
properties.put("hibernate.order_inserts", "true"); String className = Play.application()
properties.put("hibernate.order_updates", "true"); .configuration()
entityManagerFactory = Persistence.createEntityManagerFactory("default", properties); .getString("dao.entityManagerFactory.class", DaoFactory.class.getCanonicalName());
Class factoryClass = Class.forName(className);
Constructor<? extends DaoFactory> ctor = factoryClass.getConstructor(EntityManagerFactory.class);
return ctor.newInstance(entityManagerFactory);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Result index() {
return ok("TEST");
}
public static Result healthcheck() {
return ok("GOOD");
}
public static Result printDeps() {
String libPath = WHZ_APP_ENV + "/lib";
String commitFile = WHZ_APP_ENV + "/commit";
String libraries = "";
String commit = "";
if (WHZ_APP_ENV == null) {
return ok("WHZ_APP_HOME environmental variable not defined");
} }
public static final DaoFactory daoFactory = createDaoFactory(); try {
BufferedReader br = new BufferedReader(new FileReader(commitFile));
private static DaoFactory createDaoFactory() { commit = br.readLine();
try { } catch (IOException ioe) {
String className = Play.application().configuration().getString("dao.entityManagerFactory.class", DaoFactory.class.getCanonicalName()); Logger.error("Error while reading commit file. Error message: " + ioe.getMessage());
Class factoryClass = Class.forName(className);
Constructor<? extends DaoFactory> ctor = factoryClass.getConstructor(EntityManagerFactory.class);
return ctor.newInstance(entityManagerFactory);
} catch (Exception e) {
throw new RuntimeException(e);
}
} }
public static Result index() { //get all the files from a directory
return ok("TEST"); File directory = new File(libPath);
for (File file : directory.listFiles()) {
if (file.isFile()) {
libraries += file.getName() + "\n";
}
} }
public static Result healthcheck() { return ok("commit: " + commit + "\n" + libraries);
return ok("GOOD"); }
}
public static Result printDeps() {
String libPath = WHZ_APP_ENV + "/lib";
String commitFile = WHZ_APP_ENV + "/commit";
String libraries = "";
String commit = "";
if (WHZ_APP_ENV == null) {
return ok("WHZ_APP_HOME environmental variable not defined");
}
try {
BufferedReader br = new BufferedReader(new FileReader(commitFile));
commit = br.readLine();
} catch (IOException ioe) {
Logger.error("Error while reading commit file. Error message: " +
ioe.getMessage());
}
//get all the files from a directory
File directory = new File(libPath);
for (File file : directory.listFiles()) {
if (file.isFile()) {
libraries += file.getName() + "\n";
}
}
return ok("commit: " + commit + "\n" + libraries);
}
} }

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

@ -13,7 +13,7 @@ play.crypto.secret = ${?WHZ_SECRET}
# The application languages # The application languages
# ~~~~~ # ~~~~~
play.i18n.langs = [ "en" ] play.i18n.langs = ["en"]
# Play http parser settings # Play http parser settings
# #
@ -24,7 +24,7 @@ play.http.parser.maxMemoryBuffer = 10MB
# ~~~~~ # ~~~~~
# Define the Global object class for this application. # Define the Global object class for this application.
# Default to Global in the root package. # Default to Global in the root package.
application.global=shared.Global application.global = shared.Global
# Router # Router
# ~~~~~ # ~~~~~
@ -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
@ -63,7 +73,7 @@ db.wherehows.password = ${WHZ_DB_PASSWORD}
# if does not have this variable, every job will run # if does not have this variable, every job will run
# if have this varialbe, only the id in this list will be scheduled # if have this varialbe, only the id in this list will be scheduled
scheduler.check.interval = 5 scheduler.check.interval = 5
scheduler.check.interval=${?WHZ_SCHEDULER_CHECK_INTERVAL} scheduler.check.interval = ${?WHZ_SCHEDULER_CHECK_INTERVAL}
etl.jobs.dir = ${WHZ_ETL_JOBS_DIR} etl.jobs.dir = ${WHZ_ETL_JOBS_DIR}
etl.temp.dir = ${WHZ_ETL_TEMP_DIR} etl.temp.dir = ${WHZ_ETL_TEMP_DIR}

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);
}
}