阅读448 返回首页    go 阿里云 go 技术社区[云栖]


优化JDBC中读取大数据字段,提高并发能力

在获取大数据量文本字段时,如果直接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);
    }

最后更新:2017-04-02 05:21:04

  上一篇:go 当你学不进去的时候,不妨看看大脑是怎么想的?
  下一篇:go 大型网站的架构