packagecom.panda.core.db.impl;importcom.panda.core.log.log.util.Tools;importcom.zaxxer.hikari.HikariConfig;importcom.zaxxer.hikari.HikariDataSource;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.sql.Connection;importjava.sql.SQLException;importjava.util.Properties;/***数据库服务* Created by Lovell on 16/6/18.*/public classDBService {
private staticLogger logger= LoggerFactory.getLogger(DBService.class);private staticString DB_CONFIG_FILE= "/mysql.properties";private staticString HK_CONFIG_FILE= "/hikari.properties";//数据库服务器addrprivateString dbUrl= null;//数据库连接端口private shortdbPort= 0;//数据库名称privateString dbName= null;//数据库登录用户名privateString dbUsername= null;//数据库登录密码privateString dbPassword= null;private volatile longhkConnectionTimeout;private volatile longhkValidationTimeout;private volatile longhkIdleTimeout;//数据库连接池最小闲置连接private volatile shorthkMinIdle= 0;//最大生命周期private volatile longhkMaxLifetime= 0;//数据库连接池最大连接数private volatile shorthkMaxPoolSize= 0;//数据库连接池的名称privateString hkPoolName= null;//数据库预编译private booleanhkCachePrepStmts= false;//数据库预编译缓冲池的大小private shorthkPrepStmtCacheSize= 0;//数据库预编译数据库查询语句的最大长度private shorthkPrepStmtCacheSqlLimit= 0;//数据预编译功能是否开启private booleanhkUseServerPrepStmts= false;//是否自动提交private booleanhkIsAutoCommit= false;//数据库缓冲池privateHikariDataSource dataSource;private staticDBService dBService;public staticDBService getInstance() {
if(dBService== null) {
dBService= newDBService();}
returndBService;}
public voidsetConfigFilePathName(finalString dbPath, finalString hkPath) {
setDBConfigFilePathName(dbPath);setHKConfigFilePathName(hkPath);}
public voidsetDBConfigFilePathName(finalString path) {
if(path.charAt(0) == '/') {
DB_CONFIG_FILE= path;} else{
DB_CONFIG_FILE= "/"+ path;}
}
public voidsetHKConfigFilePathName(finalString path) {
if(path.charAt(0) == '/') {
HK_CONFIG_FILE= path;} else{
HK_CONFIG_FILE= "/"+ path;}
}
public voidstart() throwsIOException,SQLException {
Properties dbProp = newProperties();//编译运行时获取项目根路径下build/classes/main路径;jar运行时获取.jar路径String strPath = Tools.getPath();File dbFile = newFile(strPath + DB_CONFIG_FILE);//获取Resource路径的配置文件// InputStream dbIn = DBService.class.getClass().getResourceAsStream(DB_CONFIG_FILE);InputStream dbIn = newFileInputStream(dbFile);dbProp.load(dbIn);Properties hkProp = newProperties();File hkFile = newFile(strPath + HK_CONFIG_FILE);//获取Resource路径的配置文件// InputStream hkIn = DBService.class.getClass().getResourceAsStream(HK_CONFIG_FILE);InputStream hkIn = newFileInputStream(hkFile);hkProp.load(hkIn);dbUrl= String.valueOf(dbProp.getProperty("mysql.url"));dbPort= Short.valueOf(dbProp.getProperty("mysql.port"));dbName= String.valueOf(dbProp.getProperty("mysql.name"));dbUsername= String.valueOf(dbProp.getProperty("mysql.username"));dbPassword= String.valueOf(dbProp.getProperty("mysql.password"));hkPoolName= String.valueOf(hkProp.getProperty("hikari.poolName"));hkMinIdle= Short.valueOf(hkProp.getProperty("hikari.minIdle"));hkMaxPoolSize= Short.valueOf(hkProp.getProperty("hikari.maxPoolSize"));hkCachePrepStmts= Boolean.valueOf(hkProp.getProperty("hikari.cachePrepStmts"));hkPrepStmtCacheSize= Short.valueOf(hkProp.getProperty("hikari.prepStmtCacheSize"));hkPrepStmtCacheSqlLimit= Short.valueOf(hkProp.getProperty("hikari.prepStmtCacheSqlLimit"));hkUseServerPrepStmts= Boolean.valueOf(hkProp.getProperty("hikari.useServerPrepStmts"));hkIsAutoCommit= Boolean.valueOf(hkProp.getProperty("hikari.isAutoCommit"));hkMaxLifetime= Long.valueOf(hkProp.getProperty("hikari.maxLifetime"));if(dbUrl== null|| dbUrl.length() == 0) {
System.out.println("DB ip address error!");System.exit(0);}
HikariConfig config = newHikariConfig();config.setPoolName(hkPoolName);config.setMinimumIdle(hkMinIdle);config.setMaximumPoolSize(hkMaxPoolSize);config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");config.addDataSourceProperty("serverName",dbUrl);config.addDataSourceProperty("port",dbPort);config.addDataSourceProperty("databaseName",dbName);config.addDataSourceProperty("user",dbUsername);config.addDataSourceProperty("password",dbPassword);// MySQL为了性能的情况下使用设定config.addDataSourceProperty("cachePrepStmts",hkCachePrepStmts);config.addDataSourceProperty("prepStmtCacheSize",hkPrepStmtCacheSize);config.addDataSourceProperty("prepStmtCacheSqlLimit",hkPrepStmtCacheSqlLimit);config.addDataSourceProperty("useServerPrepStmts",hkUseServerPrepStmts);config.setMaxLifetime(hkMaxLifetime);config.setAutoCommit(hkIsAutoCommit);try{
dataSource= newHikariDataSource(config);}catch(Exception e) {
System.out.println("DB server is not running!");e.printStackTrace();}
}
/***从连接池中获取连接*@return*@throwsSQLException*/publicConnection getConnection() throwsSQLException {
try{
returndataSource.getConnection();} catch(SQLException e) {
System.out.println("connect to DB server error!");e.printStackTrace();dataSource.resumePool();}
return null;}
public voidevictConnection(Connection connection) throwsSQLException {
dataSource.evictConnection(connection);}
/***销毁连接池*/public booleanstop() throwsSQLException {
dataSource.close();return true;}
}