Additional tests
added tests which specifically test blob streams and behavior after the RS is closed.
@@ -414,6 +414,92 @@ public class lobsTest extends AbstractTest {
testUpdateLobs(types, Clob.class);
}
+ @Test
+ @DisplayName("readBlobStreamAfterClosingRS")
+ public void readBlobStreamAfterClosingRS() throws Exception {
+ String types[] = {"varbinary(max)"};
+ table = createTable(table, types, false); // create empty table
+ int size = 10000;
+
+ byte[] data = new byte[size];
+ ThreadLocalRandom.current().nextBytes(data);
+
+ Blob blob = null;
+ InputStream stream = null;
+ PreparedStatement ps = conn.prepareStatement("INSERT INTO " + table.getEscapedTableName() + " VALUES(?)");
+ blob = conn.createBlob();
+ blob.setBytes(1, data);
+ ps.setBlob(1, blob);
+ ps.executeUpdate();
+
+ byte[] chunk = new byte[size];
+ ResultSet rs = stmt.executeQuery("select * from " + table.getEscapedTableName());
+ rs.next();
+
+ blob = rs.getBlob(1);
+ stream = blob.getBinaryStream();
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ int read = 0;
+ while ((read = stream.read(chunk)) > 0)
+ buffer.write(chunk, 0, read);
+ assertEquals(chunk.length, size);
+ rs.close();
+ stream = blob.getBinaryStream();
+ buffer = new ByteArrayOutputStream();
+ read = 0;
+ while ((read = stream.read(chunk)) > 0)
+ buffer.write(chunk, 0, read);
+ assertEquals(chunk.length, size);
+
+ if (null != blob)
+ blob.free();
+ dropTables(table);
+ }
+
+ @Test
+ @DisplayName("readMultipleBlobStreamsThenCloseRS")
+ public void readMultipleBlobStreamsThenCloseRS() throws Exception {
+ String types[] = {"varbinary(max)"};
+ table = createTable(table, types, false);
+ int size = 10000;
+
+ byte[] data = new byte[size];
+ Blob[] blobs = {null, null, null};
+ InputStream stream = null;
+ for (int i = 0; i < 3; i++)
+ {
+ PreparedStatement ps = conn.prepareStatement("INSERT INTO " + table.getEscapedTableName() + " VALUES(?)");
+ blobs[i] = conn.createBlob();
+ ThreadLocalRandom.current().nextBytes(data);
+ blobs[i].setBytes(1, data);
+ ps.setBlob(1, blobs[i]);
+ ps.executeUpdate();
+ }
+ byte[] chunk = new byte[size];
+ ResultSet rs = stmt.executeQuery("select * from " + table.getEscapedTableName());
+ for (int i = 0; i < 3; i++)
+ {
+ rs.next();
+ blobs[i] = rs.getBlob(1);
+ stream = blobs[i].getBinaryStream();
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ int read = 0;
+ while ((read = stream.read(chunk)) > 0)
+ buffer.write(chunk, 0, read);
+ assertEquals(chunk.length, size);
+ }
+ rs.close();
+ for (int i = 0; i < 3; i++)
+ {
+ stream = blobs[i].getBinaryStream();
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ int read = 0;
+ while ((read = stream.read(chunk)) > 0)
+ buffer.write(chunk, 0, read);
+ assertEquals(chunk.length, size);
+ }
+ }
+
private void testUpdateLobs(String types[],
Class lobClass) throws Exception {
table = createTable(table, types, false); // create empty table
