java 如何在 MySql JDBC 中使用 HikariCP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35032676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:39:54  来源:igfitidea点击:

How to Use HikariCP with MySql JDBC

javamysqljdbcconnection-poolinghikaricp

提问by Lucy

I'm trying to use HikariCP JDBC connection pool in my Java application. I'm not using any frameworks like Spring or Hibernate in my application. Currently I'm able to connect to MySQL DB using simple JDBC driver but when I try using Hiraki the code is not working. Can't understand where I'm going wrong even after initializing the data source .

我正在尝试在我的 Java 应用程序中使用 HikariCP JDBC 连接池。我没有在我的应用程序中使用任何框架,如 Spring 或 Hibernate。目前我可以使用简单的 JDBC 驱动程序连接到 MySQL DB,但是当我尝试使用 Hiraki 时,代码不起作用。即使在初始化数据源之后也无法理解我哪里出错了。

Initial JDBC Working Code..

初始 JDBC 工作代码..

public class Connect extends ErrorCat{

    protected Connection connection = null;

    //Database user name and password
    private String name = "root";
    private String pass = "";

    //Database URL and JDBC Driver
    private String url = "jdbc:mysql://127.0.0.1:3306/fls";
    private String driver = "com.mysql.jdbc.Driver";

    protected /*static Connection*/void getConnection(){

        if (connection == null){
            System.out.println("Registering driver....");

            try {
                //Driver Registration
                Class.forName(driver).newInstance();
                System.out.println("Driver Registered successfully!!.");

                //Initiate a connection
                System.out.println("Connecting to database...");
                connection = DriverManager.getConnection(url, name, pass);
                System.out.println("Connected to database!!!");

            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("Couldnt register driver...");
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("Couldnt connect to database...");
            }
        }

        //return connection;
    }
}

Updated Code(Not Working)..

更新代码(不工作)..

public class Connect extends ErrorCat{

    protected Connection connection = null;

    protected Connection connection = null;
    protected HikariDataSource ds = null; 
    protected static Connection instance = null; 

    protected /*static Connection*/void getConnection() {

        if (connection == null){
            System.out.println("Registering driver....");

            Connect ct = new Connect();
             ct.HikariGFXDPool();
        }

        //return connection;
    }

    protected void HikariGFXDPool(){

        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");
        config.setUsername("bart");
        config.setPassword("51mp50n");  
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

        HikariDataSource ds = new HikariDataSource(config);
    }
}

回答by Andrea Catania

I Think that the real problem is that in the method HikariGFXDPool you create a local variable and the class variable protected HikariDataSource ds = null; remain null. So you cannot get the connection.

我认为真正的问题是在 HikariGFXDPool 方法中创建了一个局部变量和类变量 protected HikariDataSource ds = null; 保持为空。所以你无法获得连接。

The best way is to use a separate class to make and get the connections

最好的方法是使用单独的类来建立和获取连接

something like this:

像这样:

public class DBHandler{
    private static HikariDataSource ds;
    static{

        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");
        config.setUsername("bart");
        config.setPassword("51mp50n");  
        config.setDriverClassName("com.mysql.jdbc.Driver");
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

        ds = new HikariDataSource(config);
    }

    public static Connection getConn() throws SQLException {
        return ds.getConnection();
    }

}

Then in yours other class you get the connection using:

然后在您的其他课程中,您使用以下方法获得连接:

Connection conn = DBHandler.getConn();
// query
conn.close();