Friday, February 24, 2012

JDBC ResultSet writes to tmp file

The problem I got is when I am using the ResultSet to loop through the
result rows, the JDBC driver keeps writing each row's data to a tmp
file at /Tomcat/temp/ After the loop is finished, I do a
"ResultSet.close();". After that, the content in that tmp file is
gone.
Because the content written to the tmp file is really big during the
looping, it significantly slows done the performance. Following is my
code.
[code]
int size = 0;
int i = 0;
ObjectOutputStream outputToApplet = new
ObjectOutputStream(response.
getOutputStream());
ResultSet resultSet = statement.executeQuery(q);
if (page == 0) { //send back the number of rows.
resultSet.last();
size = resultSet.getRow();
resultSet.beforeFirst();
outputToApplet.writeObject(new Integer(size));
outputToApplet.flush();
}
else if (page > 0) { //send back 1000 as the number of rows,
and start from page*1000
outputToApplet.writeObject(new Integer(1000));
outputToApplet.flush();
resultSet.absolute(page * 1000);
}
while (resultSet.next()) {
String MailID = resultSet.getString("mail_Id");
String Name = resultSet.getString("mailFrom");
String To = resultSet.getString("mailToSorted");
String Subject = resultSet.getString("Subject");
String InsertDate = resultSet.getString("insertDate");
CustomerServiceReturn C = new CustomerServiceReturn(Name,
To, MailID, Subject, InsertDate);
outputToApplet.writeObject(C);
outputToApplet.flush();
i++;
if (i == 1000) {
outputToApplet.close();
resultSet.close();
return;
}
}
resultSet.close();
outputToApplet.close();
[/code]
The name of the file is "ddtbxxxxx.tmp". xxxxx is a number.
Thank you so much for any help.
Hong
Hong wrote:
> The problem I got is when I am using the ResultSet to loop through the
> result rows, the JDBC driver keeps writing each row's data to a tmp
> file at /Tomcat/temp/ After the loop is finished, I do a
> "ResultSet.close();". After that, the content in that tmp file is
> gone.
> Because the content written to the tmp file is really big during the
> looping, it significantly slows done the performance.
Did you actually verify this with some measurements that the tmp file is
the reason for slow performance?

> Following is my
> code.
> [code]
> int size = 0;
> int i = 0;
> ObjectOutputStream outputToApplet = new
> ObjectOutputStream(response.
> getOutputStream());
> ResultSet resultSet = statement.executeQuery(q);
> if (page == 0) { //send back the number of rows.
> resultSet.last();
> size = resultSet.getRow();
> resultSet.beforeFirst();
> outputToApplet.writeObject(new Integer(size));
> outputToApplet.flush();
> }
> else if (page > 0) { //send back 1000 as the number of
> rows, and start from page*1000
> outputToApplet.writeObject(new Integer(1000));
> outputToApplet.flush();
> resultSet.absolute(page * 1000);
> }
> while (resultSet.next()) {
> String MailID = resultSet.getString("mail_Id");
> String Name = resultSet.getString("mailFrom");
> String To = resultSet.getString("mailToSorted");
> String Subject = resultSet.getString("Subject");
> String InsertDate = resultSet.getString("insertDate");
> CustomerServiceReturn C = new
> CustomerServiceReturn(Name, To, MailID, Subject, InsertDate);
> outputToApplet.writeObject(C);
> outputToApplet.flush();
> i++;
> if (i == 1000) {
> outputToApplet.close();
> resultSet.close();
> return;
> }
> }
> resultSet.close();
> outputToApplet.close();
> [/code]
> The name of the file is "ddtbxxxxx.tmp". xxxxx is a number.
> Thank you so much for any help.
I'd remove the invocations of "outputToApplet.flush();". Closing does a
flush automatically and with the flushes in between you underutilize the
streams buffers which might be *one* reasons for bad performance.
It's also a good idea to put close() calls into finally blocks. That way
you make sure that even in case of an exception resources are properly
deallocated.
Kind regards
robert

No comments:

Post a Comment