GVKun编程网logo

Mysql JDBC-mysql-Driver queryTimeout分析(mysql.jdbc.driver报错)

11

对于MysqlJDBC-mysql-DriverqueryTimeout分析感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍mysql.jdbc.driver报错,并为您提供关于Causedby

对于Mysql JDBC-mysql-Driver queryTimeout分析感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍mysql.jdbc.driver报错,并为您提供关于Caused by: java.lang.RuntimeException: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jd、ClassNotFoundException:com.mysql.jdbc.DriverWeb应用程序的JDBC MySQL驱动程序、ClassNotFoundException:com.mysql.jdbc.Driver。Web应用程序的JDBC MySQL驱动程序、com.mysql.cj.jdbc.Driver的有用信息。

本文目录一览:

Mysql JDBC-mysql-Driver queryTimeout分析(mysql.jdbc.driver报错)

Mysql JDBC-mysql-Driver queryTimeout分析(mysql.jdbc.driver报错)

Mysql jdbc的queryTimeout分析

###Mysql的jdbc-driver com.mysql.jdbc.Driver

###设置queryTimeout方法 com.mysql.jdbc.StatementImpl.setQueryTimeout StatementImpl实例有一个field:timeoutInMillis

public void setQueryTimeout(int seconds) throws SQLException {
    synchronized(this.checkClosed().getConnectionMutex()) {
        if(seconds < 0) {
            throw SQLError.createSQLException(Messages.getString("Statement.21"), "S1009", this.getExceptionInterceptor());
        } else {
            this.timeoutInMillis = seconds * 1000;
        }
    }
}

###queryTimeout使用场景示例: com.mysql.jdbc.StatementImpl.executeQuery

ResultSet executeQuery(String sql) throws SQLException;

executeQuery有一个较复杂的逻辑:

  • 获取connection的互斥锁

  • 校验、初始化一些配置,是否为ping请求

  • sql转义,防sql注入

  • 判断timeout是否有效,有效时创建一个CancelTask

  • 将cancelTask放入Timer中延迟执行

           if (locallyScopedConn.getEnableQueryTimeouts() && this.timeoutInMillis != 0 && locallyScopedConn.versionMeetsMinimum(5, 0, 0)) {
                          timeoutTask = new CancelTask(this);
                           //每个连接会有一个CancelTimer,一个deamon线程
                          locallyScopedConn.getCancelTimer().schedule(timeoutTask, this.timeoutInMillis);
            }
    

    也就是在当前时间的timeoutInMillis后会执行这个Task

  • 执行sql语句,获取结果

  • 超时任务判断,如果有超时任务,分为两种情况:1 超时异常已经抛出,直接返回异常;1 超时任务未执行,cancel超时任务

     this.results = locallyScopedConn.execSQL(this, sql, this.maxRows, (Buffer)null, this.resultSetType, this.resultSetConcurrency, this.createStreamingResultSet(), this.currentCatalog, cachedFields);
    
     if(timeoutTask != null) {
         if(timeoutTask.caughtWhileCancelling != null) {
      	   throw timeoutTask.caughtWhileCancelling;
         }
    
         timeoutTask.cancel();
         locallyScopedConn.getCancelTimer().purge();
         timeoutTask = null;
     }
    
  • 获取lastInsertId

  • 返回results

StatementImpl.CancelTask

class CancelTask extends TimerTask {
    SQLException caughtWhileCancelling = null;
    StatementImpl toCancel;
    Properties origConnProps = null;
    String origConnURL = "";
    long origConnId = 0L;

    CancelTask(StatementImpl cancellee) throws SQLException {
        this.toCancel = cancellee;
        this.origConnProps = new Properties();
        Properties props = StatementImpl.this.connection.getProperties();
        Enumeration keys = props.propertyNames();

        while(keys.hasMoreElements()) {
            String key = keys.nextElement().toString();
            this.origConnProps.setProperty(key, props.getProperty(key));
        }

        this.origConnURL = StatementImpl.this.connection.getURL();
        this.origConnId = StatementImpl.this.connection.getId();
    }

    public void run() {
        Thread cancelThread = new Thread() {
            public void run() {
                Connection cancelConn = null;
                java.sql.Statement cancelStmt = null;

                try {
                    MySQLConnection npe = (MySQLConnection)StatementImpl.this.physicalConnection.get();
                    if(npe != null) {
                        if(npe.getQueryTimeoutKillsConnection()) {
                            CancelTask.this.toCancel.wasCancelled = true;
                            CancelTask.this.toCancel.wasCancelledByTimeout = true;
                            npe.realClose(false, false, true, new MySQLStatementCancelledException(Messages.getString("Statement.ConnectionKilledDueToTimeout")));
                        } else {
                            Object var4 = StatementImpl.this.cancelTimeoutMutex;
                            synchronized(StatementImpl.this.cancelTimeoutMutex) {
                                if(CancelTask.this.origConnURL.equals(npe.getURL())) {
                                    cancelConn = npe.duplicate();
                                    cancelStmt = cancelConn.createStatement();
                                    cancelStmt.execute("KILL QUERY " + npe.getId());
                                } else {
                                    try {
                                        cancelConn = (Connection)DriverManager.getConnection(CancelTask.this.origConnURL, CancelTask.this.origConnProps);
                                        cancelStmt = cancelConn.createStatement();
                                        cancelStmt.execute("KILL QUERY " + CancelTask.this.origConnId);
                                    } catch (NullPointerException var25) {
                                        ;
                                    }
                                }

                                CancelTask.this.toCancel.wasCancelled = true;
                                CancelTask.this.toCancel.wasCancelledByTimeout = true;
                            }
                        }
                    }
                } catch (SQLException var27) {
                    CancelTask.this.caughtWhileCancelling = var27;
                } catch (NullPointerException var28) {
                    ;
                } finally {
                    if(cancelStmt != null) {
                        try {
                            cancelStmt.close();
                        } catch (SQLException var24) {
                            throw new RuntimeException(var24.toString());
                        }
                    }

                    if(cancelConn != null) {
                        try {
                            cancelConn.close();
                        } catch (SQLException var23) {
                            throw new RuntimeException(var23.toString());
                        }
                    }

                    CancelTask.this.toCancel = null;
                    CancelTask.this.origConnProps = null;
                    CancelTask.this.origConnURL = null;
                }

            }
        };
        cancelThread.start();
    }
}

timeout后执行的操作主要为:

  • cancelConn = npe.duplicate(); //复制一个当前连接配置相同的连接
  • cancelStmt = cancelConn.createStatement(); //创建一个Statement对象,用来发送sql语句到数据库
  • cancelStmt.execute("KILL QUERY " + npe.getId()); //杀掉已经timeout的语句

可以看到,只要CancelTask执行,除了执行sql的连接压根没有成功生成外,都会执行KILL QUERY操作,里面不做任何请求是否已成功的判断。 原因也比较明显,凡是执行到CancelTask,说明确实超时了。

##connectTimeout=5000&socketTimeout=10000 其实,设置了queryTimeout也不一定生效,上述代码中无论是成功执行,还是CancelTask,都会涉及到对socket的操作,socket操作是底层的,它也有timeout选项,错误的配置或不配置,会采用操作系统的默认配置,这个时间可能是长达30分钟的。一旦网络出现问题,调用socket.read()时阻塞了,都到导致应用程序假死。

##解决办法 在jdbc.url中配置参数connectTimeout和socketTimeout参数,当然他们的值应该大于计划内的程序执行sql的最长耗时时间,否则可能中断正常的sql执行。

Caused by: java.lang.RuntimeException: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jd

Caused by: java.lang.RuntimeException: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jd

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.RuntimeException: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jdbc:mysql//localhost:3306/cache
### The error may exist in com/pshdhx/cache/mapper/Employeemapper.java (best guess)
### The error may involve com.pshdhx.cache.mapper.Employeemapper.getEmployee
### The error occurred while executing a query
### Cause: java.lang.RuntimeException: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jdbc:mysql//localhost:3306/cache




    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
    at com.sun.proxy.$Proxy73.selectOne(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:160)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:87)
    at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
    at com.sun.proxy.$Proxy79.getEmployee(Unknown Source)






springboot连接mysql查询,是字段没有对应上出的错,一开始我是以为连接mysql出了问题;

ClassNotFoundException:com.mysql.jdbc.DriverWeb应用程序的JDBC MySQL驱动程序

ClassNotFoundException:com.mysql.jdbc.DriverWeb应用程序的JDBC MySQL驱动程序

因此,我有一个用于MySQL JDBC驱动程序的.jar文件,该文件位于我的库源文件夹下,并且具有以下代码:

public static Connection getConnection() throws SQLException {
        Connection conn = null;
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://50.56.81.42:3306/GUEST_BOOK";
            String user = "user";
            String password = "pass";

            conn = (Connection) DriverManager.getConnection(url,user,password);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(InstantiationException e){
            e.printStackTrace();
        }catch(IllegalAccessException e){
            e.printStackTrace();
        }
            return conn;

    }

但是,它总是给我这个错误:

INFO: Server startup in 645 ms
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at gbook.DbHelper.getConnection(DbHelper.java:14)
    at gbook.DbHelper.getGuestBook(DbHelper.java:51)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:83)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
Dec 1,2011 1:41:05 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/JDBC-MySQL] threw exception [javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Connection] with root cause
java.lang.ClassNotFoundException: com.mysql.jdbc.Connection
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    at gbook.DbHelper.getGuestBook(DbHelper.java:52)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:83)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这是我添加jar文件的方法。我创建了一个lib文件夹,并将jar放入其中,然后在eclipse中配置构建路径,并将lib文件夹中的jar添加到了项目中。

ClassNotFoundException:com.mysql.jdbc.Driver。Web应用程序的JDBC MySQL驱动程序

ClassNotFoundException:com.mysql.jdbc.Driver。Web应用程序的JDBC MySQL驱动程序

因此,我有一个用于MySQL JDBC驱动程序的.jar文件,该文件位于我的库源文件夹下,并且具有以下代码:

public static Connection getConnection() throws SQLException {        Connection conn = null;        try{            Class.forName("com.mysql.jdbc.Driver").newInstance();            String url = "jdbc:mysql://50.56.81.42:3306/GUEST_BOOK";            String user = "user";            String password = "pass";            conn = (Connection) DriverManager.getConnection(url, user, password);        }catch(ClassNotFoundException e){            e.printStackTrace();        }catch(InstantiationException e){            e.printStackTrace();        }catch(IllegalAccessException e){            e.printStackTrace();        }            return conn;    }

但是,它总是给我这个错误:

INFO: Server startup in 645 msjava.lang.ClassNotFoundException: com.mysql.jdbc.Driver    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)    at java.lang.Class.forName0(Native Method)    at java.lang.Class.forName(Class.java:169)    at gbook.DbHelper.getConnection(DbHelper.java:14)    at gbook.DbHelper.getGuestBook(DbHelper.java:51)    at org.apache.jsp.index_jsp._jspService(index_jsp.java:83)    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)    at java.lang.Thread.run(Thread.java:680)Dec 1, 2011 1:41:05 PM org.apache.catalina.core.StandardWrapperValve invokeSEVERE: Servlet.service() for servlet [jsp] in context with path [/JDBC-MySQL] threw exception [javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mysql/jdbc/Connection] with root causejava.lang.ClassNotFoundException: com.mysql.jdbc.Connection    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)    at gbook.DbHelper.getGuestBook(DbHelper.java:52)    at org.apache.jsp.index_jsp._jspService(index_jsp.java:83)    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这是我添加jar文件的方法。我创建了一个lib文件夹,并将jar放入其中,然后在eclipse中配置构建路径,并将lib文件夹中的jar添加到了项目中。

答案1

小编典典

您错过了类路径中的mysql-connector-j jar文件。使用“ java -cp。:mysql.jar …”。哦
这是一个webapp!然后把mysql jar文件放到WEB-INF / lib文件夹中。

com.mysql.cj.jdbc.Driver

com.mysql.cj.jdbc.Driver

# Loading class `com.mysql.jdbc.Driver''. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver''. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
  • MySQL 5.7.16
  • Spring Boot 2.1.0.RELEASE

用传统的 xml 文件配置,启动 Spring Boot 项目时,出现的提示,根据提示把配置文件中的 com.mysql.jdbc.Driver 换成 com.mysql.cj.jdbc.Driver 即可。

关于Mysql JDBC-mysql-Driver queryTimeout分析mysql.jdbc.driver报错的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Caused by: java.lang.RuntimeException: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jd、ClassNotFoundException:com.mysql.jdbc.DriverWeb应用程序的JDBC MySQL驱动程序、ClassNotFoundException:com.mysql.jdbc.Driver。Web应用程序的JDBC MySQL驱动程序、com.mysql.cj.jdbc.Driver等相关知识的信息别忘了在本站进行查找喔。

本文标签: