在获取大数据量文本字段时,如果直接rs.getString(),当同时很多人访问量,会导致内存占用高,系统响应变慢,因此我们可以采用循环读取256字节,这样就大大提高了并发能力。以下是代码实现
public static String getLargeTextField(ResultSet rs, int columnIndex)
throws SQLException
{
Reader bodyReader = null;
String value = null;
if(isStreamTextRequired()){
bodyReader = rs.getCharacterStream(columnIndex);
if (bodyReader != null)
try {
char buf[] = new char[256];
StringWriter out = new StringWriter(256);
int len;
while ( (len = bodyReader.read(buf)) >= 0)
out.write(buf, 0, len);
value = out.toString();
out.close();
}
catch (Exception e) {
logger.error(e);
throw new SQLException("Failed to load text field");
}
finally {
try {
bodyReader.close();
}
catch (Exception e) {}
}
return value;
}
return rs.getString(columnIndex);
}
时间: 2024-10-29 07:56:32