简体   繁体   English

JDBC Blob(不是)在使用后是免费的()吗?

[英]Should JDBC Blob (not) be free()'d after use?

Just whacking together an export from an old DB that contains binary data, I stumbled over an exception in one of our utility methods: 只是从包含二进制数据的旧数据库中导出一个导出,我偶然发现了一个实用程序方法中的异常:

java.lang.AbstractMethodError: net.sourceforge.jtds.jdbc.BlobImpl.free() java.lang.AbstractMethodError:net.sourceforge.jtds.jdbc.BlobImpl.free()

After checking our codebase, I found that utility method was never used until now, bascially it looks like this: 在检查了我们的代码库之后,我发现实用程序方法直到现在才被使用,基本上它看起来像这样:

public BinaryHolder getBinary(final int columnIndex) throws SQLException {
    Blob blob = null;
    try {
        blob = resultSet.getBlob(columnIndex);
        final BinaryHolder binary = BinaryHolderUtil.create(blob);
        return binary;
    } finally {
        if (blob != null)
            blob.free();
    }
}

BinaryHolder is just a wrapper that holdes the binary data (and before you ask, the code executes fine until it reaches the finally clause - BinaryHolderUtil.create(blob) does not attempt to free the blob). BinaryHolder只是一个包装,holdes二进制数据(你问之前,代码执行罚款,直至到达最终条款- BinaryHolderUtil.create(BLOB) 尝试释放BLOB)。

Investigating further I found that everywhere else we access Blob's, the blob is just obtained using getBlob() and not free'd at all (The Javadoc says it will be automatically disposed of when the result set is closed). 进一步调查我发现在我们访问Blob的其他地方,blob只是使用getBlob()而不是完全免费获得(Javadoc说它会在结果集关闭时自动处理掉)。

Question now: Should the blob be free()'d manually (after all the ResultSet may be held for more than just accessing the blob), and if yes how can it be free()'d in a way that works even with a driver that does not implement it? 现在问题:blob 是否应该是free()'d'手动(在所有ResultSet之后可以保留不仅仅是访问blob),如果是怎么可以free()'d甚至可以使用没有实现它的驱动程序?

(We are using SQL-Server with JTDS1.25, if that wasn't already obvious from the exception) (我们正在使用带有JTDS1.25的SQL-Server,如果从例外中看不出来的话)

The Blob.free() was introduced in JDBC 4.0 / Java 6. So you are most likely using a JDBC 3.0 or earlier JDBC driver. Blob.free()是在JDBC 4.0 / Java 6中引入的。因此,您最有可能使用JDBC 3.0或更早版本的JDBC驱动程序。

As with most (JDBC) resources, closing them as soon as possible has its advantages (eg GC can collect it earlier, database resources are freed etc). 与大多数(JDBC)资源一样,尽快关闭它们有其优点(例如,GC可以更早地收集它,释放数据库资源等)。 That is also why you can close a ResultSet even though it is closed when you close the statement (or execute the statement again), just like you can close a Statement even though it is closed when the Connection is closed. 这就是为什么你可以关闭一个ResultSet即使它在你关闭语句时关闭(或者再次执行语句),就像你关闭一个Statement即使它在Connection关闭时关闭一样。

So a Blob does not need to be freed, but it is - in general - a good idea to free it when you are done with it. 所以Blob 不需要被释放,但是 - 一般来说 - 当你完成它时释放它是一个好主意。

BTW: JTDS is only JDBC 3.0, you would be better off using the Microsoft SQL Server JDBC driver of Microsoft itself. 顺便说一句:JTDS只是JDBC 3.0,你最好使用微软自己的Microsoft SQL Server JDBC驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM