性能超過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

使用實例

核心依賴

<!-- 數據庫驅動 -->
 <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.g.6</version>
        </dependency>

<!-- 鏈接池 -->
        <!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.4.7</version>
        </dependency>

核心配置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("獲取數據庫鏈接失敗!");
        }
    }
}
相關文章
相關標籤/搜索