How do I write a custom Long class to handle long values in Oracle, to avoid the following error?
Caused by: java.sql.SQLException: Stream has already bee
The following doesn't answer the original question 'how to write a custom Long class to handle long values in Oracle' but may be helpful to avoid the 'Stream has already been closed' error when querying Oracle long raw columns.
We faced this error using a legacy database with no chances of changing the column type. We use Spring with hibernate3 session factory and transaction manager. The problem occurred when more than one task were accessing the DAO concurrently. We are using ojdbc14.jar driver and tried a newer one with no luck.
Setting useFetchSizeWithLongColumn = true in the connection properties for the OJDBC driver solved the problem. See the OracleDriver API
THIS IS A THIN ONLY PROPERTY. IT SHOULD NOT BE USED WITH ANY OTHER DRIVERS. If set to "true", the performance when retrieving data in a 'SELECT' will be improved but the default behavior for handling LONG columns will be changed to fetch multiple rows (prefetch size). It means that enough memory will be allocated to read this data. So if you want to use this property, make sure that the LONG columns you are retrieving are not too big or you may run out of memory. This property can also be set as a java property : java -Doracle.jdbc.useFetchSizeWithLongColumn=true myApplication
Oracle recommends not using Long
and Long Raw
columns (since Oracle 8i). They are included in Oracle only for legacy reasons. If you really need to use them, the you should first handle these columns before attempting to touch any other columns in the ResultSet
:
Docs:
When a query selects one or more LONG or LONG RAW columns, the JDBC driver transfers these columns to the client in streaming mode. After a call to executeQuery or next, the data of the LONG column is waiting to be read.
Do not create tables with LONG columns. Use large object (LOB) columns, CLOB, NCLOB, and BLOB, instead. LONG columns are supported only for backward compatibility. Oracle recommends that you convert existing LONG columns to LOB columns. LOB columns are subject to far fewer restrictions than LONG columns.
As for hibernate - see this question.
I think you get this message when you try to get an Oracle LONG value from the result set multiple times.
I had code like:
rs.getString(i+1) ;
if (rs.wasNull()) continue ;
set(queryAttr[i], rs.getString(i+1)) ;
And I started getting the "Stream has already been closed." error. I stopped getting the error when I changed the code to:
String str = rs.getString(i+1) ;
if (rs.wasNull()) continue ;
set(queryAttr[i], str) ;
This happens in a query of system tables:
SELECT * FROM all_tab_columns
WHERE owner = 'D_OWNER' AND COLUMN_NAME LIKE 'XXX%';