java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z

26,368

Solution 1

java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z

This means that the MySQL JDBC driver is outdated as such that it doesn't support Java 1.6's Connection#isValid() method.

Upgrade it. And make sure that you've only one MySQL JDBC driver JAR file in the runtime classpath.

See also:

Solution 2

For me, the solution was not to upgrade my driver (JT400). Even the latest version (9.1) appears to not have implemented isValid() (it's commented out in the code?).

What worked for me was to provide a validationQuery to my database connection pool. E.g.:

validationQuery=SELECT current date FROM sysibm.sysdummy1

Solution 3

net.sourceforge.jtds.jdbc.JtdsConnection doesn't implement isValid()

So you need to specify a connection-test-query to ensure that isValid() method isn't called Adding the following line to application.propertiesfile resolved the error for me.

spring.datasource.hikari.connection-test-query=SELECT 1

Solution 4

in my case, I upgraded ojdbc from verion 14 to 6. I had artifact ojdbc14, then I changed it to ojdbc6. I lost quite sometime here, as I thought 14 is a later version as 14>6. but 14 is meant for java 1.4. 6 means java6

Share:
26,368
Abdul Tayyeb
Author by

Abdul Tayyeb

Updated on May 26, 2021

Comments

  • Abdul Tayyeb
    Abdul Tayyeb almost 3 years

    I am trying to connect my Servlet to mysql database using data Source . But whenever I run my servlet I end up getting this exception :

    java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z
        org.apache.tomcat.dbcp.dbcp2.DelegatingConnection.isValid(DelegatingConnection.java:913)
        org.apache.tomcat.dbcp.dbcp2.PoolableConnection.validate(PoolableConnection.java:282)
        org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:356)
        org.apache.tomcat.dbcp.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2306)
        org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2289)
        org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2038)
        org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
        Servlet.AbdulTayyebs.processRequest(AbdulTayyebs.java:36)
        Servlet.AbdulTayyebs.doGet(AbdulTayyebs.java:57)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    

    Here is my Content.xml

    <Resource name="jdbc/abdultayyebs" auth="Container"
              type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://127.0.0.1:4000/abdultayyebs?zeroDateTimeBehavior=convertToNull"
              username="root" password="february1996" maxActive="5" maxIdle="2"
              maxWait="1000"/>
    

    Here is my web.xml

      <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/abdultayyebs</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth></resource-ref>
    

    And here is my servlet AbdulTayyebs

     import java.io.IOException;
     import java.io.PrintWriter; 
     import java.sql.Connection;
     import java.sql.SQLException;
     import javax.naming.Context;
     import javax.naming.InitialContext;
     import javax.servlet.ServletConfig;
     import javax.servlet.ServletException;
     import javax.servlet.http.HttpServlet;
     import javax.servlet.http.HttpServletRequest;
     import javax.servlet.http.HttpServletResponse;
     import javax.sql.DataSource;
    
     public class AbdulTayyebs extends HttpServlet {
    
      DataSource ds=null;  
    
     @Override
     public void init(ServletConfig config)throws ServletException{
        try {
         Context initContext = new InitialContext();
         Context envContext  = (Context)initContext.lookup("java:/comp/env");
         ds = (DataSource)envContext.lookup("jdbc/abdultayyebs");  
        } catch (Exception e) {
         throw new ServletException("Something went wrong while Initializing the Servlet",e);
        }
      }
    
        protected void processRequest(HttpServletRequest request,                   
           HttpServletResponse response)
           throws ServletException,IOException {
           PrintWriter write = response.getWriter();
           try {   
           Others.Action a = Others.ActionFactory.CreateAction(request);
         try(Connection c=ds.getConnection()){
         String page = a.Execute(c,request,request.getSession(false));
           request.getRequestDispatcher(page).forward(request, response); 
          }      
       }     
       catch (SQLException e) {
         write.println(e);
       }
       catch (ServletException e) {
         write.println(e);     
       }
       catch (Exception e) {       
        write.println(e);
       }   
     }
    
     @Override
     protected void doGet(HttpServletRequest request, HttpServletResponse  response)
            throws ServletException, IOException {
          processRequest(request, response);
       }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         processRequest(request, response);
         } 
       }
    

    I also added the mysql jdbc driver in the lib folder of tomcat but even this didnt helped ? It would be highly appreciable if anybody can help me out

  • Abdul Tayyeb
    Abdul Tayyeb over 8 years
    Thanks it was really helpful
  • Florian F
    Florian F over 6 years
    I had an error similar to this but with an Oracle DB. Upgrading the driver fixed it.
  • Gary Eberhart
    Gary Eberhart over 6 years
    Would you be so kind as to share your code for doing this? I tried doing it like... config.addDataSourceProperty("validationQuery", "SELECT current date FROM sysibm.sysdummy1");
  • KC Baltz
    KC Baltz over 6 years
    validationQuery was a property of the connection pool I was using. It went alongside other properties like URL, username, and password in my config files.
  • Gary Eberhart
    Gary Eberhart over 6 years
    How to do it with Java code... config.setConnectionTestQuery("SELECT current date FROM sysibm.sysdummy1");
  • KC Baltz
    KC Baltz over 6 years
    Are you using a connection pool? Your code looks correct, so I'm guessing you might not be using a pool or that the pool you're using doesn't support that property. If you are and it does, you'll have to check the docs for that pool. That said, it's unusual to configure a connection pool in code. Consider doing it through a config file.
  • Gary Eberhart
    Gary Eberhart over 6 years
    You're correct. I'm using a Hikari connection pool. Sorry. I probably shouldn't have posted that here.
  • KC Baltz
    KC Baltz over 6 years
    The HikariCP docs mention a property called "connectionTestQuery" that sounds like the equivalent of the "validationQuery" I"m using. They say you should use if your driver doesn't support JDBC 4, which was the case in my instance.
  • JRSofty
    JRSofty over 4 years
    Any other possibilities for this? I'm using the newest version of MySQL Connector (8.0.18 at time of writing) but I'm getting the same exception.
  • BalusC
    BalusC over 4 years
    @JRSofty: then you don't have only one MySQL JDBC driver JAR file in the runtime classpath as indicated by the answer.