简体   繁体   English

使用MySQL连接器的Java Servlet中的XML响应缓慢

[英]Slow XML response in Java Servlet with MySQL connector

I created a Java Servlet that get query result from mySQL database and print it in XML format. 我创建了一个Java Servlet,该Java Servlet从mySQL数据库获取查询结果并将其以XML格式打印。 the problem is that it takes a very long time, something about three minutes to print the xml result while in my PHP script it takes 5 seconds. 问题是,这需要很长时间,打印xml结果大约需要三分钟,而在我的PHP脚本中则需要5秒钟。

My Servlet relevant function is: ( run a query and return the xml in a String variable, then, print it to the page ) 我的Servlet相关功能是:(运行查询并在String变量中返回xml,然后将其打印到页面上)

public String QueryResult(String query)
{
    String retStr;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection ("jdbc:mysql://"+this.host+":"+this.port+"/"+this.db, this.user, this.pass);
        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery(query);
        ResultSetMetaData rsMetaData = rset.getMetaData();

        retStr = "<Result>\n";
        while (rset.next())
        {
            retStr += "\t<Item>\n";
            for (int i=1;i<=rsMetaData.getColumnCount();i++)
            {
                retStr += "\t\t<"+rsMetaData.getColumnName(i)+">"+ rset.getString(i) + "</"+rsMetaData.getColumnName(i)+">\n";
            }
            retStr += "\t</Item>\n";
        }
        retStr += "</Result>\n";
        stmt.close();
        conn.close();
    }
    catch(Exception e) 
    {
        return "<Result><Error>"+e.toString()+"</Error></Result>";
    }
    return retStr;
}

String Concatenation: 字符串串联:

First of all, when speed matters, you do not want to concatenate strings the way you do. 首先,当速度很重要时,您不想以这种方式连接字符串。 Each time you concatenate a String you are creating a new one. 每次连接一个String您都会创建一个新的String

Better use StringBuilder with a well planned capacity, as the default one won't probably suit your need judging from the code snippet you show. 最好以计划好的容量使用StringBuilder ,因为从显示的代码片段来看,默认的版本可能不适合您的需求。

Also See: 另请参阅:

String, StringBuffer, and StringBuilder String,StringBuffer和StringBuilder

Why to use StringBuffer in Java instead of the string concatenation operator 为什么在Java中使用StringBuffer代替字符串连接运算符

XML: XML:

XStream is a simple library to serialize objects to XML and back again. XStream是一个简单的库,用于将对象序列化为XML并再次返回。

Might not be related to your performance issue but might come in handy: XStream 可能与您的性能问题无关,但可能会派上用场: XStream

"Performance" does figure in their features list. “性能”确实在其功能列表中显示。

You should try to use a StringBuilder to concatenate the XML data. 您应该尝试使用StringBuilder连接XML数据。 This avoids continously copying of data collected so far. 这样可以避免连续复制到目前为止收集的数据。 A minor improvement could also be to set the initial capacity (in StringBuilders constructor) to the expected size. 较小的改进还可以是将初始容量(在StringBuilders构造函数中)设置为预期的大小。

Strings in Java are immutable, this means that your code creates and destroys a lot of string objects. Java中的字符串是不可变的,这意味着您的代码会创建和销毁许多字符串对象。 An obvious optimisation would be to use a StringBuilder or StringBuffer to build your result. 一个明显的优化方法是使用StringBuilderStringBuffer生成结果。

I would not expect this to result in minutes difference between this and your other implementation, so something else might be the problem (a missing table index perhaps?) If you can add logging to your code, so that you get an idea where all this time is spent, we could give a more specific advice. 我不希望这会导致此实现与您的其他实现之间有几分钟的差异,所以可能还有其他问题(也许是缺少表索引吗?)如果您可以将日志记录添加到代码中,以便您了解所有这些代码花时间了,我们可以提供更具体的建议。

Creating/opening/closing connection takes lots time. 创建/打开/关闭连接需要很多时间。 It is not good for servlet to create own connection - better use connection pooling. servlet创建自己的连接不好-更好地使用连接池。

如果要串联的数据很多,也许您将使用StringBuffer而不是String。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM