Tomcat6无法连接到MySql(驱动程序尚未从服务器接收任何数据包) [英] Tomcat6 can't connect to MySql (The driver has not received any packets from the server)

查看:1982
本文介绍了Tomcat6无法连接到MySql(驱动程序尚未从服务器接收任何数据包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的本地计算机上使用Ubuntu 9.10作为操作系统运行Apache Tomcat 6.0.20 / MySQL 5.1.37-lubuntu / sun-java6-jdk / sun-java6-jre / sun-java6-
我试图获得一个简单的DB查询示例,现在运行2天,但我仍然得到这个异常:

  org.apache.jasper.JasperException:javax.servlet.ServletException:javax.servlet.jsp.JspException:无法获取连接,DataSource无效:org.apache.commons.dbcp.SQLNestedException:无法创建PoolableConnectionFactory(通信链接失败

成功发送到服务器的最后一个数据包是0毫秒前。驱动程序没有从服务器收到任何数据包。)
org.apache.jasper.servlet.JspServletWrapper.handleJspException (JspServletWrapper.java:522)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342 )
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

原因

javax.servlet.ServletException:javax.servlet.jsp.JspException:无法获取连接,DataSource无效:org.apache.commons.dbcp.SQLNestedException:无法创建PoolableConnectionFactory(通信链接failure

成功发送到服务器的最后一个数据包为0毫秒前。驱动程序没有收到来自服务器的任何数据包。)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException (PageContextImpl.java:791)
org.apache.jsp.index_jsp._jspService(index_jsp.java:104)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper .servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

根本原因

javax.servlet.jsp.JspException:无法获取连接,DataSource无效:org.apache.commons.dbcp.SQLNestedException:无法创建PoolableConnectionFactory(通信链接失败

成功发送到服务器的最后一个数据包为0毫秒前。驱动程序没有收到来自服务器的任何数据包。)
org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.getConnection(QueryTagSupport.java:285)
org.apache.taglibs .standard.tag.common.sql.QueryTagSupport.doStartTag(QueryTagSupport.java:168)
org.apache.jsp.index_jsp._jspx_meth_sql_005fquery_005f0(index_jsp.java:274)
org.apache.jsp.index_jsp ._jspx_meth_c_005fotherwise_005f0(index_jsp.java:216)
org.apache.jsp.index_jsp._jspx_meth_c_005fchoose_005f0(index_jsp.java:130)
org.apache.jsp.index_jsp._jspService(index_jsp.java:93)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper .servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service (JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

my web.xml如下所示:

 <?xml version =1.0encoding = utf-8?> 
< web-app xmlns =http://java.sun.com/xml/ns/javaee
xmlns:xsi =http://www.w3.org/2001/XMLSchema -instance
xsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
version =2.5>
< resource-ref>
< description> DB连接< / description>
< res-ref-name> jdbc / testDB< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< / resource-ref>
< / web-app>

context.xml如下所示:

 <?xml version =1.0encoding =UTF-8?> 
< Context path =/ my1stAppdocBase =/ var / www / jsp / my1stAppdebug =5oadable =truecrossContext =true>
< Resource name =jdbc / testDBauth =Containertype =javax.sql.DataSource
maxActive =5maxIdle =5maxWait =10000
username =userpassword =passworddriverClassName =com.mysql.jdbc.Driver
url =jdbc:mysql:// localhost:3306 / some/>
< / Context>

并且jsp文件如下所示:

 <%@ page contentType =text / html%> 

<%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core%>

<%@ taglib prefix =fnuri =http://java.sun.com/jsp/jstl/functions%>

<%@ taglib prefix =fmturi =http://java.sun.com/jsp/jstl/fmt%>
<%@ taglib prefix =sqluri =http://java.sun.com/jsp/jstl/sql%>


< html>

< head>

< title> DroneLootTool< / title>

< / head>

< body bgcolor =white>



< sql:query var =resdataSource =jdbc / testDB>
select name,othername
from mytable
< / sql:query>

< h2>结果< / h2>

< c:forEach var =rowitems =$ {res.rows}>
名称$ {row.name}< br />
MoreName $ {row.othername}< br />< br />
< / c:forEach>


< / body>

< / html>




  • 阅读了许多论坛条目/尝试过许多不同的设置

  • 在/ etc / default / tomcat6中设置TOMCAT6_SECURITY = no,因为TOMCAT6_SECURITY = yes会导致麻烦。

  • 未为数据库设置skip-networking标志(设置BIND 127.0.0.1)

  • 防火墙已关闭(sudo ufw disable)

  • MySQL工作(用户在此skript中使用过多次测试)

  • telnet localhost 3306说



    Trying :: 1 ...
    尝试127.0.0.1 ...
    连接到localhost。
    转义字符是'^]'。
    外部主机关闭的连接。







TestConnection .java生成以下输出:



me @ my-laptop:〜/ Desktop $ java -classpath'/usr/share/java/mysql.jar:./' TestConnection com.mysql.jdbc.Driver jdbc:mysql:// localhost:3306 / testDB myuser mypassword

  com.mysql.jdbc .CommunicationsException:通信链路故障
发送到服务器的最后一个数据包是0毫秒前。
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
at com.mysql .jdbc.ConnectionImpl。< init>(ConnectionImpl.java:718)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
at com.mysql.jdbc.NonRegisteringDriver。 connect(NonRegisteringDriver.java:282)
在java.sql.DriverManager.getConnection(DriverManager.java:582)
在java.sql.DriverManager.getConnection(DriverManager.java:185)
在TestConnection.checkConnection(TestConnection.java:40)
在TestConnection.main(TestConnection.java:21)
导致:com.mysql.jdbc.CommunicationsException:通信链路失败

发送到服务器的最后一个数据包是0毫秒前。
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:666)
at com.mysql .jdbc.MysqlIO.doHandshake(MysqlIO.java:1069)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
... 7更多
由于: java.io.EOFException:无法从服务器读取响应。预期读取4个字节,在连接意外丢失之前读取0个字节。
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:590)
... 9更多
连接失败。






我不知道是否有差异在java驱动程序连接到DB和Perl DBI模块的方式之间,但是这个PERL skript工作

 #!/ usr / bin / perl -w 

使用CGI;
使用DBI;
use strict;

print CGI :: header();

my $ dbh = DBI-> connect(dbi:mysql:some:localhost,user,password);

my $ sSql =SELECT * from mytable;

my $ ppl = $ dbh-> selectall_arrayref($ sSql);

foreach my $ pl(@ $ ppl)
{
my @array = @ $ pl;
print @array;
}

$ dbh-> disconnect;






但我没有得到任何新的警告。
当我在日志中搜索警告时,我发现这个消息,当我重新启动tomcat,不知道是否有帮助找到问题:

  Feb 2 19:50:37 tobias-laptop jsvc.exec [3129]:02.02.2010 19:50:37 org.apache.catalina.startup.HostConfig checkResources#012INFO:取消部署上下文[/ myapp] 
Feb 2 19:50:37 tobias-laptop jsvc.exec [3129]:02.02.2010 19:50:37 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads#012SCHWERWIEGEND:一个Web应用程序似乎有启动了一个名为[MySQL Statement Cancellation Timer]的线程,但是没能阻止它。这很可能造成内存泄漏。
Feb 2 19:50:37 tobias-laptop jsvc.exec [3129]:02.02.2010 19:50:37 org.apache.catalina.startup.HostConfig deployDescriptor#012INFO:部署配置描述符myapp.xml


解决方案



事实证明,我通过运行三个mysqld实例来摧毁服务器,所以杀死不必要的解决我的问题:

  me @ ubuntu:〜$ ps aux | grep mysql 
mysql 8149 0.1 0.4 280484 26008? Ssl 13:24 0:00 / usr / sbin / mysqld
me 8256 0.0 0.0 4392 836 pts / 3 S + 13:24 0:00 grep --color = auto mysql
root 26569 0.0 0.0 5952 1856 ? S 10:44 0:00 sudo / usr / sbin / mysqld --skip-grant-tables --skip-networking
mysql 26570 0.1 0.6 323528 41936? Sl 10:44 0:15 / usr / sbin / mysqld --skip-grant-tables --skip-networking

me @ ubuntu:〜$ sudo kill 26570


i'm running an Apache Tomcat 6.0.20 / MySQL 5.1.37-lubuntu / sun-java6-jdk /sun-java6-jre / sun-java6-bin on my local machine using Ubuntu 9.10 as OS. I'm trying to get a simple DB-query example running for 2 days now, but i still get this Exception:

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)"
 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)"
 org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
 org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
 org.apache.jsp.index_jsp._jspService(index_jsp.java:104)
 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)"
 org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.getConnection(QueryTagSupport.java:285)
 org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.doStartTag(QueryTagSupport.java:168)
 org.apache.jsp.index_jsp._jspx_meth_sql_005fquery_005f0(index_jsp.java:274)
 org.apache.jsp.index_jsp._jspx_meth_c_005fotherwise_005f0(index_jsp.java:216)
 org.apache.jsp.index_jsp._jspx_meth_c_005fchoose_005f0(index_jsp.java:130)
 org.apache.jsp.index_jsp._jspService(index_jsp.java:93)
 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

my web.xml looks like this :

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/testDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

the context.xml looks like this :

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/my1stApp" docBase="/var/www/jsp/my1stApp" debug="5" reloadable="true" crossContext="true">
    <Resource name="jdbc/testDB" auth="Container" type="javax.sql.DataSource"
    maxActive="5" maxIdle="5" maxWait="10000"
    username="user" password="password" driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/some"/>
</Context>

and the jsp file looks like this:

<%@ page contentType="text/html" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>


<html>

  <head>

    <title>DroneLootTool</title>

  </head>

  <body bgcolor="white">



 <sql:query var="res" dataSource="jdbc/testDB">
  select name, othername 
  from mytable
 </sql:query>

  <h2>Results</h2>

<c:forEach var="row" items="${res.rows}">
    Name ${row.name}<br/>
    MoreName ${row.othername}<br/><br/>
</c:forEach>


  </body>

</html>

  • read lots of forum entries / tried lots of different settings (always changed back to original settings when it didnt' work)
  • set TOMCAT6_SECURITY=no in /etc/default/tomcat6 because TOMCAT6_SECURITY=yes was causing trouble too
  • the skip-networking flag is not set for the DB (BIND 127.0.0.1 is set)
  • firewall is swiched off (sudo ufw disable)
  • MySQL works (tested several times with user used in this skript)
  • telnet localhost 3306 says

    Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Connection closed by foreign host.


The TestConnection.java produced the following output:

me@my-laptop:~/Desktop$ java -classpath '/usr/share/java/mysql.jar:./' TestConnection com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/testDB myuser mypassword

com.mysql.jdbc.CommunicationsException: Communications link failure
    Last packet sent to the server was 0 ms ago.
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at TestConnection.checkConnection(TestConnection.java:40)
        at TestConnection.main(TestConnection.java:21)
    Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure

    Last packet sent to the server was 0 ms ago.
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
        at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:666)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1069)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
        ... 7 more
    Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)
        at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:590)
        ... 9 more
    Connection failed.


i don't know if there is a difference between the way the java driver connects to the DB and the Perl DBI module does, but this PERL skript works

#!/usr/bin/perl -w 

use CGI;
use DBI;
use strict;

print CGI::header();

my $dbh = DBI->connect("dbi:mysql:some:localhost", "user", "password");

my $sSql = "SELECT * from mytable";

my $ppl = $dbh->selectall_arrayref( $sSql );

foreach my $pl (@$ppl)
{
  my @array = @$pl;
  print @array;
}

$dbh->disconnect;


enabled --log-warnings on the mysql, but i didn't get any new warnings. When i was searching the logs for warnings i found this messages when i restart the tomcat, don't know if it helps to find the problem :

Feb  2 19:50:37 tobias-laptop jsvc.exec[3129]: 02.02.2010 19:50:37 org.apache.catalina.startup.HostConfig checkResources#012INFO: Undeploying context [/myapp]
Feb  2 19:50:37 tobias-laptop jsvc.exec[3129]: 02.02.2010 19:50:37 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads#012SCHWERWIEGEND: A web application appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.
Feb  2 19:50:37 tobias-laptop jsvc.exec[3129]: 02.02.2010 19:50:37 org.apache.catalina.startup.HostConfig deployDescriptor#012INFO: Deploying configuration descriptor myapp.xml

解决方案

I had the same issue.

It turns out I messed up the server by running three mysqld instances, so kill the unwanted resolved my issue:

me@ubuntu:~$ ps aux | grep mysql
mysql     8149  0.1  0.4 280484 26008 ?        Ssl  13:24   0:00 /usr/sbin/mysqld
me     8256  0.0  0.0   4392   836 pts/3    S+   13:24   0:00 grep --color=auto mysql
root     26569  0.0  0.0   5952  1856 ?        S    10:44   0:00 sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking
mysql    26570  0.1  0.6 323528 41936 ?        Sl   10:44   0:15 /usr/sbin/mysqld --skip-grant-tables --skip-networking

me@ubuntu:~$ sudo kill 26570

这篇关于Tomcat6无法连接到MySql(驱动程序尚未从服务器接收任何数据包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆