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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32783706/
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-08-11 13:05:29  来源:igfitidea点击:

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

javamysqlservletsjdbcdatasource

提问by Abdul Tayyeb

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 :

我正在尝试使用 data Source 将我的 Servlet 连接到 mysql 数据库。但是每当我运行我的 servlet 时,我最终都会得到这个异常:

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

这是我的 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

这是我的 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

这是我的 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

我还在 tomcat 的 lib 文件夹中添加了 mysql jdbc 驱动程序,但即使这样也没有帮助?如果有人可以帮助我,那将是非常可观的

采纳答案by BalusC

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

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.

这意味着 MySQL JDBC 驱动程序已经过时,因此它不支持 Java 1.6 的Connection#isValid()方法。

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

升级吧。并确保运行时类路径中只有一个 MySQL JDBC 驱动程序 JAR 文件。

See also:

也可以看看:

回答by KC Baltz

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?).

对我来说,解决方案不是升级我的驱动程序(JT400)。即使是最新版本 (9.1) 似乎也没有实现 isValid() (它在代码中被注释掉了?)。

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

对我有用的是为我的数据库连接池提供验证查询。例如:

validationQuery=SELECT current date FROM sysibm.sysdummy1

回答by Feng Zhang

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

就我而言,我将 ojdbc 从 verion 14 升级到 6。我有工件 ojdbc14,然后我将其更改为 ojdbc6。我在这里迷失了一段时间,因为我认为 14 是 14>6 的更高版本。但是 14 是为 java 1.4 设计的。6 表示 java6

回答by Rakesh Yadav

net.sourceforge.jtds.jdbc.JtdsConnectiondoesn't implement isValid()

net.sourceforge.jtds.jdbc.JtdsConnection不执行 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.

因此,您需要指定一个 connection-test-query 以确保isValid()不会调用该方法将以下行添加到application.properties文件中为我解决了错误。

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

回答by Bhavesh Chetwani

This is what I read about isValid method (source). The driver (here MySQL) tries to check if your connection is valid or not. Not sure how it works (as it doesn't allow to connect even when my MySQL instance/service is up and running). So, use the below string in your spring application.properties file

这是我读到的关于 isValid 方法(source)的内容。驱动程序(此处为 MySQL)尝试检查您的连接是否有效。不确定它是如何工作的(因为即使我的 MySQL 实例/服务启动并运行,它也不允许连接)。因此,在您的 spring application.properties 文件中使用以下字符串

spring.datasource.hikari.connection-test-query=select 1 from dual (or any test query, - select sysdate from dual)

spring.datasource.hikari.connection-test-query=select 1 from dual (或任何测试查询,-select sysdate from dual)