How to Use HikariCP with MySql JDBC

安稳与你 提交于 2021-01-28 04:29:42

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/35032676/how-to-use-hikaricp-with-mysql-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!