HikariCP 动态创建数据源

邓尼茨我今天去赶集 2020年09月07日 1,182次浏览

基本功能,数据源对象会一直常驻内存,不会清理。

public enum DriverClassEnum {
    MYSQL("MYSQL", "com.mysql.cj.jdbc.Driver"),
    POSTGRE("POSTGRE", "org.postgresql.Driver");

    private String type;

    private String value;

    DriverClassEnum(String type, String value) {
        this.type = type;
        this.value = value;
    }
    public String getType() {
        return type;
    }
    public String getValue() {
        return value;
    }
}
import lombok.Data;

@Data
public class DataSourceProperty {
    private String dataSourceName;
    private String username;
    private String password;
    private String jdbcUrl;
    private String driverClassName;
    private Boolean autoCommit=true;
    private Integer maximumPoolSize=15;
    private Integer minimumIdle=2;
    private Integer idleTimeout=10000;
    private Integer validationTimeout=1000;
    private Integer connectionTimeout=50000;
}
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class MDIDataSource {

    public HikariDataSource create(DataSourceProperty dataSourceProperty) {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setUsername(dataSourceProperty.getUsername());
        hikariConfig.setPassword(dataSourceProperty.getPassword());
        hikariConfig.setJdbcUrl(dataSourceProperty.getJdbcUrl());
        hikariConfig.setMaximumPoolSize(dataSourceProperty.getMaximumPoolSize());
        hikariConfig.setMinimumIdle(dataSourceProperty.getMinimumIdle());
        hikariConfig.setPoolName(dataSourceProperty.getDataSourceName());
        hikariConfig.setAutoCommit(dataSourceProperty.getAutoCommit());
        hikariConfig.setIdleTimeout(dataSourceProperty.getIdleTimeout());
        hikariConfig.setValidationTimeout(dataSourceProperty.getValidationTimeout());
        hikariConfig.setConnectionTimeout(dataSourceProperty.getConnectionTimeout());
        hikariConfig.setDriverClassName(dataSourceProperty.getDriverClassName());
        return new HikariDataSource(hikariConfig);
    }
}
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MDIDataSourceProvider {

    private static Map<String, HikariDataSource> dataSourceMap = new ConcurrentHashMap<>(256);

    public static Connection getConnection(DataSourceProperty dataSourceProperty) throws SQLException {
        if (dataSourceMap.get(dataSourceProperty.getDataSourceName()) == null) {
            HikariDataSource dataSource = new MDIDataSource().create(dataSourceProperty);
            dataSourceMap.put(dataSourceProperty.getDataSourceName(), dataSource);
            return dataSource.getConnection();
        } else {
            return dataSourceMap.get(dataSourceProperty.getDataSourceName()).getConnection();
        }
    }

    public static boolean testConnection(DataSourceProperty dataSourceProperty) throws ApplicationException {
        try {
            Class.forName(dataSourceProperty.getDriverClassName());
            DriverManager.getConnection(dataSourceProperty.getJdbcUrl(),dataSourceProperty.getUsername(),dataSourceProperty.getPassword());
            return true;
        }catch(Exception e){
            throw new ApplicationException(e.getMessage());
        }
    }

    private static DataSourceProperty getMysqlProperty() {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        dataSourceProperty.setDataSourceName("MYSQL_001");
        dataSourceProperty.setJdbcUrl("jdbc:mysql://hbloud:3306/hbervice");
        dataSourceProperty.setPassword("Haaa");
        dataSourceProperty.setUsername("ddddin");
        dataSourceProperty.setDriverClassName(DriverClassEnum.MYSQL.getValue());
        return dataSourceProperty;
    }

    private static DataSourceProperty getMysqlProperty2() {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        dataSourceProperty.setDataSourceName("MYSQL_001");
        dataSourceProperty.setJdbcUrl("jdbc:mysql://h1ysqloud:3306/hbrvice");
        dataSourceProperty.setPassword("1swei123#_");
        dataSourceProperty.setUsername("dbsn");
        dataSourceProperty.setDriverClassName(DriverClassEnum.MYSQL.getValue());
        return dataSourceProperty;
    }

    private static DataSourceProperty getPostgreProperty() {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        dataSourceProperty.setDataSourceName("PG_001");
        dataSourceProperty.setJdbcUrl("jdbc:postgresql://11111:5432/1111");
        dataSourceProperty.setPassword("111");
        dataSourceProperty.setUsername("111");
        dataSourceProperty.setDriverClassName(DriverClassEnum.POSTGRE.getValue());
        return dataSourceProperty;
    }


    public static void main(String[] args) throws SQLException {

        for (int i = 0; i < 10; i++) {

            Connection conn = MDIDataSourceProvider.getConnection(getMysqlProperty());
            ResultSet resultSet = conn.createStatement().executeQuery("select now()");
            while (resultSet.next()) {
                System.out.println(resultSet.getTimestamp(1));
            }
            conn.close();

            Connection connPG = MDIDataSourceProvider.getConnection(getPostgreProperty());
            ResultSet resultSetPG = connPG.createStatement().executeQuery("select now()");
            while (resultSetPG.next()) {
                System.out.println(resultSetPG.getTimestamp(1));
            }
            connPG.close();
        }

        for (Map.Entry<String, HikariDataSource> entry : MDIDataSourceProvider.dataSourceMap.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue().getHikariConfigMXBean().toString());
        }

        try {
            if (testConnection(getMysqlProperty2())) {
                System.out.println("Connection Passed.");
            }
        } catch (ApplicationException e) {
            System.out.println("Connection Failed. Cause:"+e.getMessage());
        }
    }

}