2010-01-31 24 views
6

Estoy ejecutando un Apache Tomcat 6.0.20/MySQL 5.1.37-lubuntu/sun-java6-jdk/sun-java6- jre/sun-java6-bin en mi máquina local usando Ubuntu 9.10 como sistema operativo. Estoy intentando conseguir un ejemplo sencillo DB-consulta que se ejecuta durante 2 días ahora, pero aún así obtener esta excepción:Tomcat6 no se puede conectar a MySql (El controlador no ha recibido ningún paquete del servidor)

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) 

mi web.xml se ve así:

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

el contexto .xml se ve así:

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

y el archivo JSP se ve así:

<%@ 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> 
  • leer un montón de entradas del foro/probado muchos entornos diferentes (siempre cambiado de nuevo a la configuración original cuando didnt' trabajo)
  • conjunto TOMCAT6_SECURITY = no en/etc/default/tomcat6 porque TOMCAT6_SECURITY = sí estaba causando problemas también
  • la bandera de aplazamiento de la creación de redes no está configurado para el DB (127.0.0.1 BIND está establecido)
  • cortafuegos está fuera swiched (UFW sudo desactivar)
  • funciona MySQL (probado varias veces con usuario utilizado en este skript)
  • telnet localhost 3306 dice

    Trying :: 1 ... Probando 127.0.0.1 ... Conectado a localhost. El carácter de escape es '^]'. Conexión cerrada por un host externo.


El TestConnection.java produce el siguiente resultado:

me

@ mi-portátil: /usr/share/java/mysql.jar:./ ~/Desktop $ java -classpath' 'TestConnection com.mysql.jdbc.Driver jdbc: mysql: // localhost: 3306/testDB miUsuario 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. 

no sé si hay una diferencia entre la forma en que el jav un conductor se conecta a la base de datos y el módulo Perl DBI hace, pero esto PERL skript funciona

#!/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; 

habilitados --log-advertencias en el MySQL, pero no he tenido ningún nuevas advertencias. cuando estaba buscando los registros de las advertencias que encontré este mensaje cuando reinicio el gato, no sé si le ayuda a encontrar el problema:

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 
+1

Podría probar el 'TestConnection.java' de http: //testdrive.mapinfo. com/techsupp/miprod.nsf/kbase_by_product/B309A6631406A514852569770060E795 y actualice la pregunta con el resultado? –

+0

¿Dónde ha colocado el contenedor de controladores mysql? Intente eliminar el atributo auth = "Container" también – nos

+0

el enlace simbólico mysql.jar en tomcat6/lib desde/usr/share/java. en/usr/share/java mysql.jar es un enlace simbólico al archivo real en el mismo directorio. versiones probadas mysql-connector-java-5.1.11 y 5.1.6. eliminar el atributo auth = "Container" tampoco ayuda. :( –

Respuesta

0

favor, intente con un "oficial" Connector/J 5.1 es decir, distribuida por MySQL , no un deb (tenga en cuenta que no tengo nada en contra de deb)

También habilite --log-warnings en el servidor mysql y vea si aparece algo en los registros de errores.

+0

uso el conector oficial versión 5.1.11, habilitado --log -advertencias. actualizó la pregunta –

3

He resuelto este problema. El motivo por el que se produce este error son las restricciones de seguridad de tomcat.

hay que añadir siguiente línea a su $TOMCAT-CONFIG/policy.d/04webapps.policy

permission java.net.SocketPermission "127.0.0.1:3306", "connect"; 

Obras para mí, espero que trabajará para usted también :)

+0

¿Alguien podría aprobar esto como una buena solución? –

+0

tengo el mismo problema. Esto no funcionó para mí. – jasonmclose

1
Class.forName("com.mysql.jdbc.Driver"); 
      con=DriverManager.getConnection("jdbc:mysql://localhost:3306/indiadb","root","root"); 

Comprobar:

  1. la número de puerto debe ser solo 3306

  2. Añadir la mysql.jar en lib si está utilizando Tomcat6

3

que tenía el mismo problema.

Resulta que metí la pata al servidor mediante la ejecución de tres casos mysqld, por lo matan el indeseado resuelto mi problema:

[email protected]:~$ 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 

[email protected]:~$ sudo kill 26570 
Cuestiones relacionadas