带有 MySQL 的 Google Web Toolkit (GWT) Web 应用程序在 Eclipse 中运行良好,但在 openSUSE 服务器上无法运行

Posted

技术标签:

【中文标题】带有 MySQL 的 Google Web Toolkit (GWT) Web 应用程序在 Eclipse 中运行良好,但在 openSUSE 服务器上无法运行【英文标题】:Google Web Toolkit (GWT) web app with MySQL working fine in Eclipse but not working on openSUSE server 【发布时间】:2011-12-02 10:50:09 【问题描述】:

我编写了一个托管在外部 openSUSE 服务器上的 GWT Web 应用程序。我使用 Google 的 GWT RPC 与服务器通信并将数据从我的客户端发送到 mysql 数据库得到更新的服务器。在 Eclipse 中一切正常,但只要我将 /war 目录移动到服务器(包括 java-connector 所需的 mysql-connector-java-5.1.18-bin.jar 文件),我的本地 SQL 数据库服务器没有更新。为什么它不起作用?这是某种mysql配置问题吗?

我已经在服务器上建立了一个数据库,其名称和表与我的本地机器上的数据库完全相同(我在那里使用 Eclipse 进行调试)。我没有从 GWT 收到任何错误(因此服务器访问工作正常),我的 SQL 日志文件也没有显示错误。

我尝试重新启动 mysql,检查 mysql 的端口 3306 是否已打开并正在侦听,禁用防火墙,并为主机尝试了 localhost 和 127.0.0.1。

我无法弄清楚问题是什么,请有人帮助我!我在这里疯了!

这是我的服务器端代码,它在 Eclipse 中运行良好,但在服务器上却不行!

package com.mycompany.mywebapp.server;

@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
    GreetingService 

public String[] greetServer(String[] input) 

    //Data is entered into MySQL database on server side 
    Connection con = null; 
    Statement st = null; 

    String url = "jdbc:mysql://127.0.0.1:3306/testdb";
    String user = "username";
    String password = "pass";

    try
        con = DriverManager.getConnection(url,user,password); //establishes connection to database
        st = con.createStatement(); //object for sending SQL statements to database 

        for (int i=0;i<input.length;i++)
            String DataToSend = input[i];
            String part1 = DataToSend.substring(0,DataToSend.indexOf("["));
            String part2 = DataToSend.substring(DataToSend.indexOf("["),DataToSend.indexOf("]")+1);
            //store values into mysql database
            String query = "INSERT INTO boundingboxes(name, box) VALUES('"+part1+"','"+part2+"')";
            st.executeUpdate(query);
        

    catch (SQLException ex)
        Logger lgr = Logger.getLogger(Version.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);

    finally
        //make sure to avoid null pointerexception 
        try
            if (st !=null)
                st.close();
            
            if (con != null)
                con.close();
            
        catch (SQLException ex)
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.WARNING, ex.getMessage(), ex);
        
    

    return input; 





【问题讨论】:

您的catch (SQLException ex) 日志语句已被注释掉。为什么?另外,永远不要以这种方式使用 jdbc!阅读:en.wikipedia.org/wiki/SQL_injection 感谢您的帮助。我对 java/mysql 非常陌生,这是我的第一个网络应用程序。你能解释一下使用 jdbc 的最佳方式是什么吗?另外,我现在不确定要编写 catch 语句。特别是如何在我的 java 路径中包含 tools.js(由 Version.class 使用),这样我就不会得到 NoClassDefFoundError。 阅读教程,例如docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html 。为什么需要Version 类?阅读您的Logger.getLogger 的手册和教程。如果你不知道如何处理异常,最好的方法至少是throw new RuntimeException(ex) 【参考方案1】:

我找到了解决问题的方法: 要摆脱 NoClassDefFoundError,您的应用程序使用的外部 .jar 文件应直接复制到 /WEB-INF/lib/ 中。

Apache 需要一个额外的 jdbc-msql 配置步骤。首先,确保您为 SQL 数据库设置了正确的 GRANT 权限。

接下来,按照他的解决方案取自:http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-j2ee.html#connector-j-usagenotes-tomcat

首先,将 Connector/J 自带的 .jar 文件安装到 $CATALINA_HOME/common/lib 中,以便容器中安装的所有应用程序都可以使用它。

接下来,通过将声明资源添加到定义 Web 应用程序的上下文中的 $CATALINA_HOME/conf/server.xml 来配置 JNDI 数据源:

<Context ....>

...

<Resource name="jdbc/MySQLDB"
       auth="Container"
       type="javax.sql.DataSource"/>

<!-- The name you used above, must match _exactly_ here!

The connection pool will be bound into JNDI with the name
"java:/comp/env/jdbc/MySQLDB"
-->

<ResourceParams name="jdbc/MySQLDB">
<parameter>
 <name>factory</name>
 <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Don't set this any higher than max_connections on your
 MySQL server, usually this should be a 10 or a few 10's
 of connections, not hundreds or thousands -->

    <parameter>
      <name>maxActive</name>
      <value>10</value>
    </parameter>

 <!-- You don't want to many idle connections hanging around
   if you can avoid it, only enough to soak up a spike in
   the load -->

   <parameter>
    <name>maxIdle</name>
    <value>5</value>
   </parameter>

 <!-- Don't use autoReconnect=true, it's going away eventually
 and it's a crutch for older connection pools that couldn't
 test connections. You need to decide whether your application
 is supposed to deal with SQLExceptions (hint, it should), and
 how much of a performance penalty you're willing to pay
 to ensure 'freshness' of the connection -->

  <parameter>
   <name>validationQuery</name>
   <value>SELECT 1</value> <-- See discussion below for update to this option -->
  </parameter>

  <!-- The most conservative approach is to test connections
  before they're given to your application. For most applications
  this is okay, the query used above is very small and takes
  no real server resources to process, other than the time used
  to traverse the network.

  If you have a high-load application you'll need to rely on
  something else. -->

  <parameter>
   <name>testOnBorrow</name>
   <value>true</value>
  </parameter>

   <!-- Otherwise, or in addition to testOnBorrow, you can test
   while connections are sitting idle -->

   <parameter>
    <name>testWhileIdle</name>
    <value>true</value>
   </parameter>

  <!-- You have to set this value, otherwise even though
   you've asked connections to be tested while idle,
   the idle evicter thread will never run -->

 <parameter>
  <name>timeBetweenEvictionRunsMillis</name>
  <value>10000</value>
 </parameter>

 <!-- Don't allow connections to hang out idle too long,
 never longer than what wait_timeout is set to on the
 server...A few minutes or even fraction of a minute
 is sometimes okay here, it depends on your application
 and how much spikey load it will see -->

 <parameter>
  <name>minEvictableIdleTimeMillis</name>
  <value>60000</value>
 </parameter>

 <!-- Username and password used when connecting to MySQL -->

 <parameter>
  <name>username</name>
  <value>someuser</value>
 </parameter>

 <parameter>
  <name>password</name>
  <value>somepass</value>
 </parameter>

 <!-- Class name for the Connector/J driver -->

 <parameter>
  <name>driverClassName</name>
  <value>com.mysql.jdbc.Driver</value>
 </parameter>

 <!-- The JDBC connection url for connecting to MySQL, notice
 that if you want to pass any other MySQL-specific parameters
 you should pass them here in the URL, setting them using the
 parameter tags above will have no effect, you will also
 need to use &amp; to separate parameter values as the
 ampersand is a reserved character in XML -->

 <parameter>
  <name>url</name>
  <value>jdbc:mysql://localhost:3306/test</value>
 </parameter>

【讨论】:

以上是关于带有 MySQL 的 Google Web Toolkit (GWT) Web 应用程序在 Eclipse 中运行良好,但在 openSUSE 服务器上无法运行的主要内容,如果未能解决你的问题,请参考以下文章

带有 PHP 和 MySQl 的 Google 图表工具

如何避免带有非常大的 IN 表的 dbsql_stmnt_too_large 转储?

在 Anaconda 的 Windows 上安装 witwidget(google what-if too)时面临问题

带有自签名证书的 IIS 7.5 SSL 错误“ssl_error_rx_record_too_long”

Argument list too long......

带有 .NET JSON Web 令牌处理程序的 Google 身份工具包令牌验证异常