处理Java EE应用程序中的大型记录 [英] Handling large records in a Java EE application
问题描述
有一个表 phonenumbers
,有两列: id
, number
。表中有大约 50万条
。数据库是 MySQL
。
There is a table phonenumbers
with two columns: id
, and number
. There are about half a million entries
in the table. Database is MySQL
.
要求是开发一个连接到该数据库的简单Java EE应用程序,它允许用户通过关注特定网址以逗号分隔样式
下载所有数字
值。
The requirement is to develop a simple Java EE application, connected to that database, that allows a user to download all number
values in comma separated style
by following a specific URL.
如果我们在一个巨大的 String数组
中获取所有值,然后在<$ c中连接它们(在所有值之间使用逗号) $ c> String 然后将其发送给用户,听起来是否合适?
If we get all the values in a huge String array
and then concatenate them (with comma in between all the values) in a String
and then send it down to the user, does it sound a proper solution?
该应用程序不公开,将会是限量使用。人们。
The application is not public and will be used by a limited no. of people.
推荐答案
你最好的选择是不以任何方式将数据存储在Java的内存中,但只需将获得的数据写入响应立即,因为数据进入。您还需要配置MySQL JDBC驱动程序逐行地通过 Statement#setFetchSize()
根据 MySQL JDBC驱动程序文档,否则它会将整个内容缓存在内存中。
Your best bet is to not store the data in Java's memory in any way, but just write the obtained data to the response immediately as the data comes in. You also need to configure the MySQL JDBC driver to serve the resultset row-by-row by Statement#setFetchSize()
as per the MySQL JDBC driver documentation, otherwise it will cache the whole thing in memory.
假设您熟悉Servlets,这里有一个启动示例这一切都考虑在内:
Assuming you're familiar with Servlets, here's a kickoff example which takes that all into account:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
response.setHeader("Content-Disposition", "attachment;filename=numbers.txt"); // Force download popup.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
Writer writer = response.getWriter();
try {
connection = database.getConnection();
statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(Integer.MIN_VALUE);
resultSet = statement.executeQuery("SELECT number FROM phonenumbers");
while (resultSet.next()) {
writer.write(resultSet.getString("number"));
if (!resultSet.isLast()) {
writer.write(",");
}
}
} catch (SQLException e) {
throw new ServletException("Query failed!", e);
} finally {
if (resultSet != null) try { resultSet.close; } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close; } catch (SQLException logOrIgnore) {}
if (connection != null) try { connection.close; } catch (SQLException logOrIgnore) {}
}
}
这篇关于处理Java EE应用程序中的大型记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!