【发布时间】:2015-05-27 06:48:49
【问题描述】:
在我的 DB2 数据库中,我有一个带有 Blob 的表:
CREATE TABLE FILE_STORAGE (
FILE_STORAGE_ID integer,
DATA blob(2147483647),
CONSTRAINT PK_FILE_STORAGE PRIMARY KEY (FILE_STORAGE_ID));
使用db2jcc JDBC驱动(db2jcc4-9.7.jar),我可以毫无问题地读写这个表中的数据。
现在我需要能够追加数据到现有的行,但 DB2 给出了神秘的错误
Invalid operation: setBinaryStream is not allowed on a locator or a reference. ERRORCODE=-4474, SQLSTATE=null
我使用以下代码来附加我的数据:
String selectQuery = "SELECT DATA FROM FILE_STORAGE WHERE FILE_STORAGE_ID = ?";
try (PreparedStatement ps = conn.prepareStatement(selectQuery, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {
ps.setInt(1, fileStorageID);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
Blob existing = rs.getBlob(1);
try {
// The following line throws the exception:
try (OutputStream output = existing.setBinaryStream(existing.length() + 1)) {
// append the new data to the output:
writeData(output);
} catch (IOException e) {
throw new IllegalStateException("Error writing output stream to blob", e);
}
rs.updateBlob(1, existing);
rs.updateRow();
} finally {
existing.free();
}
} else {
throw new IllegalStateException("No row found for file storage ID: " + fileStorageID);
}
}
}
我的代码使用OutputStream to the BLOB column of a DB2 database table 中建议的方法。似乎还有其他人也有同样的问题:Update lob columns using lob locator。
作为一种解决方法,我目前将所有现有数据读入内存,将新数据追加到内存中,然后将完整数据写回 blob。这行得通,但速度很慢,如果 blob 中有更多数据,显然会花费更长的时间,每次更新都会变慢。
我确实需要使用 Java 来更新数据,但除了切换到 JVM 之外,我很乐意尝试任何可能的替代方案,我只需要以某种方式附加数据。
提前感谢您的任何想法!
【问题讨论】: