HikariCP - Unable to get driver for JDBC URL

1

I'm trying to implement HikariCP to care for connections in a web project.

Steps followed:

  • I created a Dynamic Web Project
  • I've added the required .jars into WEB-INF / lib ( HikariCP-2.3.5.jar / sfl4j-api.1.7.10.jar / sfl4j-jdk.1.7.10.jar / javassist.jar - mysql-connector-java-5.1.35-bin.jar )
  • I created a class that will serve the connections ( HikariPool )
  • Additional information:

  • Java version: 1.8.0_31
  • JDK: 1.7.0_45
  • Here are the codes used:

    HikariPool

    package br.com.conexao;
    
    import com.zaxxer.hikari.HikariConfig;
    import com.zaxxer.hikari.HikariDataSource;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    public class HikariPool {
    
        private static HikariPool instance = null;
        private HikariDataSource ds = null;
    
        static {
            try {
                instance = new HikariPool();
            } catch (Exception e) {
                System.out.println("Erro caiu aqui: " + e.getMessage());
                throw new RuntimeException(e.getMessage(), e);
    
            }
    
        }
    
        private HikariPool() {
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl("jdbc:mysql://localhost:3306/teste");
            config.setUsername("teste");
            config.setPassword("teste");
            //config.addDataSourceProperty("cachePrepStmts", "true");
            //config.addDataSourceProperty("prepStmtCacheSize", "250");
            //config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
            //config.addDataSourceProperty("useServerPrepStmts", "true");
    
            ds = new HikariDataSource(config);
        }
    
        public static HikariPool getInstance() {
            return instance;
        }
    
        public Connection getConnection() throws SQLException {
            return ds.getConnection();
        }
    
    }
    

    Then on the main page as a test, I'm using the following code:

    index.jsp

    try {
    
        Logger logger = Logger.getLogger(this.getClass().getSimpleName());
        Statement stmt = null;
        ResultSet rset = null;
        Connection conn = null;
    
        HikariPool pool = HikariPool.getInstance();
    
        try {
            conn = pool.getConnection();
            stmt = conn.createStatement();
            rset = stmt.executeQuery("select * from psw_relatorio_server");
            while (rset.next()) {
                System.out.println(String.format("[%s]",
                        rset.getString(1)));
            }
        } catch (SQLException se) {
            logger.log(Level.SEVERE, se.getMessage());
        } finally {
            if (stmt != null) {
                stmt.close();
            }
    
            if (rset != null) {
                rset.close();
            }
    
            if (conn != null) {
                conn.close();
            }
    
        }
    
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    Error:

    Erro caiu aqui: Unable to get driver for JDBC URL jdbc:mysql://localhost:3306/teste
    mar 24, 2015 3:44:44 PM org.apache.catalina.core.StandardWrapperValve invoke
    GRAVE: Servlet.service() for servlet [jsp] in context with path [/webviewer2] threw exception [javax.servlet.ServletException: java.lang.ExceptionInInitializerError] with root cause
    java.sql.SQLException: No suitable driver
        at java.sql.DriverManager.getDriver(Unknown Source)
        at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:52)
        at com.zaxxer.hikari.pool.PoolUtilities.initializeDataSource(PoolUtilities.java:107)
        at com.zaxxer.hikari.pool.BaseHikariPool.<init>(BaseHikariPool.java:153)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:61)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:49)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:80)
        at br.com.conexao.HikariPool.<init>(HikariPool.java:35)
        at br.com.conexao.HikariPool.<clinit>(HikariPool.java:16)
        at org.apache.jsp.index_jsp._jspService(index_jsp.java:236)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:431)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
        at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)
    

    Doubt, what would be the problem if I put the jar connection in the lib? NOTE: Connection data such as user, bank name and the like have been replaced by company security issues. But I'm sure they're correct.

        
    asked by anonymous 24.03.2015 / 19:54

    1 answer

    2

    Well, I changed the connection form of the HikariPool class and it worked:

    HikariPool

    package br.com.conexao;
    
    import com.zaxxer.hikari.HikariDataSource;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    public class HikariPool {
    
        private static HikariPool instance = null;
        private HikariDataSource ds = null;
    
        static {
            try {
                instance = new HikariPool();
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage(), e);
            }
    
        }
    
        //ALTERADO
        private HikariPool() {
            ds = new HikariDataSource();
    
            ds.setMaximumPoolSize(10);
            ds.setDriverClassName("com.mysql.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/teste");
            ds.setUsername("teste");
            ds.setPassword("teste");
            ds.addDataSourceProperty("cachePrepStmts", "true");
            ds.addDataSourceProperty("prepStmtCacheSize", "250");
            ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
            ds.addDataSourceProperty("useServerPrepStmts", "true");
        }
    
        public static HikariPool getInstance() {
            return instance;
        }
    
        public Connection getConnection() throws SQLException {
            return ds.getConnection();
        }
    
    }
    
        
    25.03.2015 / 13:42