• 所用jar
HikariCP-2.5.1.jar
log4j-1.2.11.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
ojdbc7.jar
slf4j-api-1.8.0-alpha2.jar
  • jar下载


  • 配置文件 HikariConfig.properties
#-- Hikari Datasource -->  
 #driverClassName无需指定,除非系统无法自动识别 
driverClassName=oracle.jdbc.driver.OracleDriver
#database address
jdbcUrl=jdbc:oracle:thin:@************:ORCL
#useName 用户名
username=******
#password
password=***********
#连接只读数据库时配置为true, 保证安全 -->  
readOnly=false
#等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 -->  
connectionTimeout=30000
# 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->  
idleTimeout=600000
#一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) -->  
maxLifetime=1800000
# 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->  
maximumPoolSize=15

其中,很多配置都使用缺省值就行了,除了maxLifetime和maximumPoolSize要注意自己计算一下。
其他的配置(sqlSessionFactory、MyBatis
MapperScannerConfigurer、transactionManager等)统统不用变。

其他关于Datasource配置参数的建议: Configure your HikariCP idleTimeout and
maxLifeTime settings to be one minute less than the wait_timeout of
MySQL. 对于有Java连接池的系统,建议MySQL的wait_timeout使用缺省的8小时

  • 代码:
package com.oeina.database;

import java.sql.Connection;
import java.util.ResourceBundle;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.zaxxer.hikari.HikariDataSource;
/**
 * Hikari的 连接池
 * @author Michael
 * @date 2017年8月9日18:46:41
 * 
 */
public class HikariPoolManager {


    static Logger logger = LogManager.getLogger();

    //-- Hikari Datasource -->  
    //driverClassName无需指定,除非系统无法自动识别 
    private static String driverClassName="";
    //database address
    private static String  jdbcUrl="";
    //useName 用户名
    private static String username="";
    //password
    private static String  password="";
    //连接只读数据库时配置为true, 保证安全 -->  
    private static boolean readOnly=false;
    //等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 -->  
    private static int connectionTimeout;
    // 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->  
    private static int idleTimeout;
    //一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) -->  
    private static int maxLifetime; 
    // 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->  
    private static int maximumPoolSize;
    static  HikariDataSource hikariDataSource = new HikariDataSource();

    static {
        /**
         * 加载配置文件
         */
        readProperties("HikariConfig");
        /**
         * 设置数据源的参数
         */
        dataSourceConfig();

    }
    /**
     *
     * @see 读取数据库配置文件
     * @param     propertitieFileName 配置文件名
     * @return    
     * @Exception  FileNotFoundException,IOException
     */
    private static void readProperties(String propertitieFileName){

        try{


              ResourceBundle bundle = ResourceBundle.getBundle(propertitieFileName);

              driverClassName=bundle.getString("driverClassName");
              jdbcUrl=bundle.getString("jdbcUrl");
              username=bundle.getString("username");
              password=bundle.getString("password");
              readOnly=Boolean.parseBoolean(bundle.getString("readOnly"));
              connectionTimeout=Integer.parseInt(bundle.getString("connectionTimeout"));
              idleTimeout=Integer.parseInt(bundle.getString("idleTimeout"));
              maxLifetime=Integer.parseInt(bundle.getString("maxLifetime"));
              maximumPoolSize=Integer.parseInt(bundle.getString("maximumPoolSize"));



        } catch (Exception e){
            logger.error("读取数据库参数出现问题:"+e);
            throw e;

        }
    }



    /**
     *
     * @see 设置datasource各个属性值
     * @param 
     * @return Connection
     * @Exception  Exception
     */
    private static void dataSourceConfig(){
        try{

            //driverClassName无需指定,除非系统无法自动识别 
            //private static String driverClassName="";
            hikariDataSource.setDriverClassName(driverClassName);
            //database address
            hikariDataSource.setJdbcUrl(jdbcUrl);
            //useName 用户名
            hikariDataSource.setUsername(username);
            //password
            hikariDataSource.setPassword(password);
            //连接只读数据库时配置为true, 保证安全 -->  
            hikariDataSource.setReadOnly(readOnly);
            //等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 -->  
            hikariDataSource.setConnectionTimeout(connectionTimeout);
            // 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->  
            hikariDataSource.setIdleTimeout(idleTimeout);
            //一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) -->  
            hikariDataSource.setMaximumPoolSize(maxLifetime);
            // 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->  
            hikariDataSource.setMaximumPoolSize(maximumPoolSize);



        }catch(Exception e){
            logger.error("设置datasource各个属性值异常!" + e);
            throw e;
        }
    }


    /**
     * 取得数据库连接
     * @return
     * @throws Exception
     */
    public static Connection getConnection() throws Exception{

        Connection connection = null;
        try {
                connection = hikariDataSource.getConnection();
        } catch (Exception e) {
            logger.error("取得数据库连接时发生异常!"+ e);
            throw  e;
        }
        return connection;
    }

    /**
     * 释放数据库连接
     * @param connection
     * @throws Exception
     */
    public static void freeConnection(Connection connection) throws Exception{
        if (connection != null){
            try {
                connection.close();
            }catch(Exception e){
                logger.error("释放数据库连接时发生异常!"+ e.getMessage());
            }
        }
    }
}
  • 附录1:
    另一种创建连接池的方法
package com.ace.connectionpool;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * This clas shows how to use HikariConfig class
 */
public class CP_0200_HikariConfigTest
{
    public static void main(String[] args)
    {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/mydata");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("1111");
        hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
        hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
        hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

        HikariDataSource ds = new HikariDataSource(hikariConfig);
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        try
        {
            conn = ds.getConnection();
            statement = conn.createStatement();
            rs = statement.executeQuery("select * from dept");

            while (rs.next())
            {
                System.out.println(rs.getString("dname"));
                System.out.println(rs.getString("location"));
                System.out.println(rs.getInt("deptno"));
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

    }
}