使用后是否应将JDBC Blob(不是)释放()? [英] Should JDBC Blob (not) be free()'d after use?

查看:100
本文介绍了使用后是否应将JDBC Blob(不是)释放()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从一个包含二进制数据的旧数据库中导出文件,我偶然发现了一种实用程序方法中的异常:

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()

检查完我们的代码库后,我发现直到现在才使用过Utility方法,基本上看起来像这样:

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只是一个保存二进制数据的包装器(在您问之前,代码会执行良好,直到到达finally子句为止-BinaryHolderUtil.create(blob)不会尝试释放该blob ).

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).

进一步研究发现,在我们访问Blob的所有其他地方,仅使用getBlob()获得了blob,而根本没有释放它(Javadoc表示,当结果集关闭时,它将被自动处置).

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(毕竟所有的ResultSet都可以保存,而不仅仅是访问blob),如果是,则如何即使使用未实现该功能的驱动程序,它是否也可以通过free()使用?

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?

(如果不是很明显,我们将SQL Server与JTDS1.25一起使用)

(We are using SQL-Server with JTDS1.25, if that wasn't already obvious from the exception)

推荐答案

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.

与大多数(JDBC)资源一样,尽快关闭它们具有其优势(例如,GC可以更早地收集它,数据库资源被释放等).这就是为什么即使关闭ResultSet在关闭语句时将其关闭(或再次执行该语句),也可以关闭ResultSet的原因,就像您可以在Connection关闭时将其关闭的同时关闭Statement一样.

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). 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.

因此,Blob不需要 被释放,但是通常,在完成后将其释放是一个好主意.

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.

顺便说一句:JTDS只是JDBC 3.0,最好使用Microsoft本身的Microsoft SQL Server JDBC驱动程序.

BTW: JTDS is only JDBC 3.0, you would be better off using the Microsoft SQL Server JDBC driver of Microsoft itself.

这篇关于使用后是否应将JDBC Blob(不是)释放()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆