【发布时间】:2012-03-10 00:52:43
【问题描述】:
我有一个带有 CLOB 字段的表 (MySQL MediumText)。
我想向该 CLOB 返回一个输入流。 我的资源代码如下所示:
@GET
public StreamingOutput getAsStream(int id) {
try {
// prepare the statement object
ResultSet rs = stmt.executeQuery();
if (rs.next()) return new StreamingOutput() {
public void write(OutputStream outputStream) throws ... {
copy(rs.getBinaryStream(1), outputStream);
}
}
}
finally {
rs.close();
stmt.close();
connection.close();
}
}
看起来不太对劲。在 Jersey 有机会将流写入响应之前,代码会关闭数据库资源(结果集、语句和连接)。
我可以在StreamingOutput.write 方法中关闭数据库资源。但它也感觉不对 - 我让一些外部容器关闭我的资源。
我能想到的最后一个想法是将整个流读入内存然后发送。我当然不想这样做。
那么,有人有更好的主意吗?
我检查了Return a file using Java Jersey,但没有多大帮助。
谢谢, 多伦
【问题讨论】: