Ubuntu上的Tomcat 6和MySQL 5.x连接错误

时间:2011-03-30 17:31:00

标签: mysql jsp tomcat

当尝试连接到Tomcat 6上运行的JSP Web应用程序中的MySQL 5.x数据库时,我遇到以下异常:

org.apache.jasper.JasperException: javax.servlet.ServletException: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: java.security.AccessControlException: access denied (java.net.SocketPermission [0:0:0:0:0:0:0:1]:3307 connect,resolve)

STACKTRACE:

java.net.SocketException: java.security.AccessControlException: access denied (java.net.SocketPermission [0:0:0:0:0:0:0:1]:3307 connect,resolve)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:151)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1699)
    at com.mysql.jdbc.Connection.<init>(Connection.java:405)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)
    at java.sql.DriverManager.getConnection(DriverManager.java:620)
    at java.sql.DriverManager.getConnection(DriverManager.java:200)
    at org.apache.jsp.doLogin_jsp._jspService(doLogin_jsp.java:70)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at sun.reflect.GeneratedMethodAccessor32.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAsPrivileged(Subject.java:537)
    at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
    at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:283)
    at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:56)
    at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:636)

可能是什么原因以及如何解决?

1 个答案:

答案 0 :(得分:1)

以下是跟踪的相关位:

  

java.net.SocketException MESSAGE:java.security.AccessControlException:access denied(java.net.SocketPermission [0:0:0:0:0:0:0:1]:3307 connect,resolve)

webapp没有连接/解析上述套接字的权限。您需要在Tomcat级别配置它。打开/conf/catalina.policy并添加以下代码块:

grant {
    permission java.net.SocketPermission "localhost:3307", "connect,resolve";
};

如果您想要更具限制性,例如仅授予/lib/filename.jar中存在的特定JDBC驱动程序的访问权限,然后添加此代码:

grant codeBase "jar:file:${catalina.home}/lib/filename.jar!/-" {
    permission java.net.SocketPermission "localhost:3307", "connect,resolve";
};

无关到具体问题,以下几行

at java.sql.DriverManager.getConnection(DriverManager.java:200)
at org.apache.jsp.doLogin_jsp._jspService(doLogin_jsp.java:70)

表示您正在连接JSP文件中的数据库。也许你刚开始学习,但我只想提到这不是最好的做法。数据库连接应该在它自己的类中完成,而这些类又在servlet类中使用,而servlet类又转发到JSP,后者又显示结果。通过URL调用servlet应该会产生相同的结果,但最终会得到更好的可重用和可维护的代码。