如何在MySql JDBC中使用HikariCP [英] How to Use HikariCP with MySql JDBC

查看:128
本文介绍了如何在MySql JDBC中使用HikariCP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Java应用程序中使用HikariCP JDBC连接池.我在应用程序中未使用任何类似Spring或Hibernate的框架.目前,我可以使用简单的JDBC驱动程序连接到MySQL数据库,但是当我尝试使用Hiraki时,代码将无法正常工作.即使初始化了数据源,也无法理解我要去哪里.

初始JDBC工作代码.

 公共类Connect扩展ErrorCat {protected Connection连接= null;//数据库用户名和密码private String name ="root";private String pass =";//数据库URL和JDBC驱动程序私有字符串url ="jdbc:mysql://127.0.0.1:3306/fls";private String driver ="com.mysql.jdbc.Driver";受保护的/*静态连接*/void getConnection(){如果(连接==空){System.out.println(正在注册驱动程序....");尝试 {//驾驶员注册Class.forName(driver).newInstance();System.out.println(驱动程序已成功注册!.");//启动连接System.out.println(正在连接数据库...");连接= DriverManager.getConnection(URL,名称,通过);System.out.println(已连接到数据库!!");} catch(InstantiationException | IllegalAccessException | ClassNotFoundException e){e.printStackTrace();System.out.println(无法注册驱动程序...");} catch(SQLException e){e.printStackTrace();System.out.println(无法连接到数据库...");}}//返回连接;}} 

更新的代码(不起作用).

 公共类Connect扩展ErrorCat {protected Connection连接= null;protected Connection连接= null;受保护的HikariDataSource ds = null;受保护的静态Connection实例= null;受保护的/*静态连接*/void getConnection(){如果(连接==空){System.out.println(正在注册驱动程序....");连接ct = new Connect();ct.HikariGFXDPool();}//返回连接;}受保护的无效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 =新的HikariDataSource(config);}} 

我认为真正的问题是,在方法HikariGFXDPool中,您创建了一个局部变量,而该类变量保护了HikariDataSource ds = null;保持为空.因此您无法建立连接.

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

类似这样的东西:

 公共类DBHandler {私有静态HikariDataSource ds;静止的{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);}公共静态连接getConn()抛出SQLException {返回ds.getConnection();}} 

然后在其他班级,您将使用以下方法获得连接:

 连接conn = DBHandler.getConn();//询问conn.close(); 

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 .

Initial JDBC Working Code..

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);
    }
}

解决方案

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.

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();

这篇关于如何在MySql JDBC中使用HikariCP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆