性能超过DRUID的最强数据库连接池——HikariCP相关配置及简单示例

  • 在配置application.yml时,对hikari的配置会有这样一个字段validationQuery。
  • validationQuery是用来验证数据库连接的查询语句,这个查询语句必须是至少返回一条数据的SELECT语句。每种数据库都有各自的验证语句。
DataBase validationQuery
hsqldb select 1 from INFORMATION_SCHEMA.SYSTEM_USERS
Oracle select 1 from dual
DB2 select 1 from sysibm.sysdummy1
MySql select 1
Microsoft SqlServer select1
postgresql select version()
ingres select 1
derby values 1
H2 gi select 1

使用实例

核心依赖


 
            com.oracle
            ojdbc6
            11.g.6
        


        
        
            com.zaxxer
            HikariCP
            2.4.7
        

核心配置hikari.properties

# 这里的配置仅适用于Orcale数据库,其他数据库参照官方说明
# 设置数据库驱动
dataSourceClassName=oracle.jdbc.pool.OracleDataSource
# 设置数据库用户名
dataSource.user=user_test
# 设置用户密码
dataSource.password=user_test
# 设置数据库名
dataSource.databaseName=test
# 设置数据库端口
dataSource.portNumber=1521
# 设置数据库地址
dataSource.serverName=192.168.1.11
# 设置驱动形式
dataSource.driverType=thin

常用数据库对应的dataSourceClassName

Database Driver DataSource class
Apache Derby Derby org.apache.derby.jdbc.ClientDataSource
Firebird Jaybird org.firebirdsql.ds.FBSimpleDataSource
H2 H2 org.h2.jdbcx.JdbcDataSource
HSQLDB HSQLDB org.hsqldb.jdbc.JDBCDataSource
IBM DB2 IBM JCC com.ibm.db2.jcc.DB2SimpleDataSource
IBM Informix IBM Informix com.informix.jdbcx.IfxDataSource
MS SQL Server Microsoft com.microsoft.sqlserver.jdbc.SQLServerDataSource
MySQL Connector/J com.mysql.jdbc.jdbc2.optional.MysqlDataSource
MariaDB MariaDB org.mariadb.jdbc.MariaDbDataSource
Oracle Oracle oracle.jdbc.pool.OracleDataSource
OrientDB OrientDB com.orientechnologies.orient.jdbc.OrientDataSource
PostgreSQL pgjdbc-ng com.impossibl.postgres.jdbc.PGDataSource
PostgreSQL PostgreSQL org.postgresql.ds.PGSimpleDataSource
SAP MaxDB SAP com.sap.dbtech.jdbc.DriverSapDB
SQLite xerial org.sqlite.SQLiteDataSource
SyBase jConnect com.sybase.jdbc4.jdbc.SybDataSource

获取数据库连接

package lss.medicare.ydjy.webservice.kswebservice.data_source;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.activation.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName HikariDataSourceConnection
 * @Description TODO
 * @Author syskey
 * @Date 2019/4/22 9:26
 * @Version 1.0
 **/
public class HikariConnection {
    private final static Logger log = LogManager.getLogger(HikariConnection.class);
    private static HikariDataSource dataSource;
    private static Connection connection;

    static {
        HikariConfig config = new HikariConfig("/hikari.properties");
        dataSource = new HikariDataSource(config);
    }

    public static Connection getInstance() {
        try {
            if (connection == null) {
                connection = dataSource.getConnection();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            log.error("获取数据源连接失败:" + e);
        }
        return connection;
    }


    public static void main(String[] args) {
        final String sql = "select * from test ";
        try {
            Connection connection = getInstance();
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                log.info(rs.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            log.error("获取数据库连接失败!");
        }
    }
}

转载于:https://www.cnblogs.com/caoleiCoding/p/11140701.html

你可能感兴趣的:(性能超过DRUID的最强数据库连接池——HikariCP相关配置及简单示例)