0

大きなテキストを読み書きするための統一されたJDBCコードを入手しました。列は、OracleではCLOB、MySQLではTEXTです。次のコード

java.sql.Clob aClob = resultSet.getClob(COLUMN_NAME);
java.io.InputStream aStream = aClob.getAsciiStream();
int av = aStream.available();

MySQL(Connector / J 5.0.4)では関連する値を示しますが、Oracle(Oracle JDBCドライバー11.2.0.2)ではゼロです。Clob.length()幸い、両方で正しい値が得られ、InputStream.read()最大-1の作品もあるため、統一された方法でデータを取得する方法は他にもあります。

Javadocはこの奇妙なメモを与えます:

クラスInputStreamで使用可能なメソッドは、常に0を返します。

では、どのドライバーが正しいのでしょうか?いいえ、ベンダー固有のパッケージをコードにドラッグしたくありません:-)この質問はJDBCニュートラルです。

4

1 に答える 1

2

どちらのドライバーも正しかったと言いたくなります。

The Javadoc for the available() method appears to suggest that the value returned is an estimate of how many bytes the InputStream currently has cached and can return to you without an I/O operation. How many bytes it has cached, and how it does any caching, would seem to me to be an implementation detail. The fact that these values are different merely suggests that the two drivers are implemented differently. Nothing in the Javadoc for the available() method suggests to me that either driver is doing anything wrong.

I'd guess that the Oracle driver doesn't cache any data from the CLOB immediately after executing the query, so that might be why the available() method returns 0. However, once data has been read from the stream, the available() method for the Oracle driver no longer returns 0, as it seems Oracle JDBC driver has been to the database and fetched some data out of the CLOB column. On the other hand, MySQL seems to be a bit more proactive in actually fetching data out of the TEXT column as soon as the query has finished executing.

Having read the Javadoc for the available() method I'm not sure why I'd use it. What are you using it for?

于 2012-09-26T14:28:17.243 に答える