use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.
the class BLOBTest method testDerby1511.
/**
* Regression test case for DERBY-1511. Scans of tables with large objects
* used to fail if commits were issued during the scans.
*/
public void testDerby1511() throws Exception {
setAutoCommit(false);
Statement s = createStatement();
s.executeUpdate("create table derby1511(b blob)");
PreparedStatement insert = prepareStatement("insert into derby1511(b) values (?)");
int rows = 20;
// LOB size should be > 32 KB to expose the bug
int length = 40000;
for (int i = 0; i < rows; i++) {
insert.setBinaryStream(1, new LoopingAlphabetStream(length), length);
insert.executeUpdate();
}
commit();
ResultSet rs = s.executeQuery("select b from derby1511");
for (int i = 0; i < rows; i++) {
assertTrue("Too few rows", rs.next());
// Second time this was called we used to get an error saying
// container has been closed.
assertEquals(new LoopingAlphabetStream(length), rs.getBinaryStream(1));
commit();
}
assertFalse("Too many rows", rs.next());
rs.close();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.
the class BLOBTest method testBlobCastInValuesClause.
/**
* Tests that a stream value in a values clause can be cast to a BLOB.
* <p>
* See DERBY-4102 (test case resulted in a ClassCastException earlier).
*
* @throws IOException if something goes wrong
* @throws SQLException if something goes wrong
*/
public void testBlobCastInValuesClause() throws IOException, SQLException {
// The length must be at least 32 KB.
final int length = 38 * 1024;
PreparedStatement ps = prepareStatement("values cast(? as blob)");
ps.setBinaryStream(1, new LoopingAlphabetStream(length), length);
ResultSet rs = ps.executeQuery();
assertTrue(rs.next());
Blob b = rs.getBlob(1);
assertEquals(length, b.length());
// Select some parts of the Blob, moving backwards.
assertEquals(100, b.getBytes(32 * 1024 - 27, 100).length);
assertEquals(1029, b.getBytes(19 * 1024, 1029).length);
// Compare a fresh stream with the one from the Blob.
assertEquals(new LoopingAlphabetStream(length), b.getBinaryStream());
assertEquals(-1, b.position(new byte[] { (byte) 'a', (byte) 'A' }, 1));
assertEquals(length, b.length());
assertFalse(rs.next());
rs.close();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.
the class BLOBTest method testDerby2992_Repro.
/**
* Checks that Derby fails with an exception when a transaction using
* READ_UNCOMMITTED obtains a stream from a BLOB (reads one byte) and at
* the same time another connection deletes the BLOB.
* <p>
* Earlier only parts of the BLOB was returned, without errors. It was
* impossible to tell for the user that only parts of the value was
* retrieved.
* <p>
* See DERBY-2992.
*/
public void testDerby2992_Repro() throws IOException, SQLException {
// Autocommit doesn't seem to be enabled here in all cases.
setAutoCommit(true);
final String TBL = "D2992BLOB";
// Switch to READ UNCOMMITTED.
getConnection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
Statement stmt = createStatement();
dropTable(TBL);
stmt.executeUpdate("create table " + TBL + " (b blob)");
stmt.close();
PreparedStatement ps = prepareStatement("insert into " + TBL + " values (?)");
// 65K
int length = 65 * 1024 * 1024;
ps.setBinaryStream(1, new LoopingAlphabetStream(length), length);
ps.executeUpdate();
ps.close();
stmt = createStatement();
ResultSet rs = stmt.executeQuery("select B from " + TBL);
assertTrue(rs.next());
// Read one byte, keep the stream / rs open.
InputStream is = rs.getBinaryStream(1);
int i = is.read();
assertTrue(i != -1);
// Open a second connection and delete the BLOB.
Connection secondCon = openUserConnection("APP");
Statement secondStmt = secondCon.createStatement();
assertEquals(1, secondStmt.executeUpdate("delete from " + TBL));
secondCon.close();
// Continue reading the BLOB through the stream.
// The stream has now probably read one page of data, and as we progress
// it will have to fetch the next page. However, the next page has been
// deleted.
byte[] buf = new byte[4096];
try {
// Drain the stream.
while (is.read(buf) != -1) {
}
// Expect the read call above to fail at some point.
fail("The read should have failed, value has been deleted");
} catch (EOFException eofe) {
// As we expected, everything's fine.
}
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.
the class BlobTest method transferAlphabetData.
/**
* Transfers the specified number of bytes generated from the modern latin
* alphabet (lowercase) to the destination stream.
*
* @param writer the destination
* @param length number of bytes to write
* @throws IOException if writing to the destination stream fails
*/
public static void transferAlphabetData(OutputStream writer, long length) throws IOException {
byte[] buffer = new byte[8 * 1024];
int bytesRead = 0;
LoopingAlphabetStream contents = new LoopingAlphabetStream(length);
while ((bytesRead = contents.read(buffer)) > 0) {
writer.write(buffer, 0, bytesRead);
}
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.
the class BlobTest method initializeLongBlob.
/**
* Insert a row with a large blob into the test table. Read the row from
* the database and assign the blob value to <code>blob</code>.
* @return The id of the row that was inserted
* @throws java.sql.SQLException
*/
private int initializeLongBlob() throws SQLException {
// Blob needs to be larger than one page for locking to occur
final int lobLength = 40000;
// Insert a long Blob
PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, BLOBDATA) values(?,?)");
int id = BlobClobTestSetup.getID();
ps.setInt(1, id);
ps.setBinaryStream(2, new LoopingAlphabetStream(lobLength), lobLength);
ps.execute();
ps.close();
commit();
// Fetch the Blob object from the database
Statement st = createStatement();
ResultSet rs = st.executeQuery("select BLOBDATA from BLOBCLOB where ID=" + id);
rs.next();
blob = rs.getBlob(1);
rs.close();
st.close();
return id;
}
Aggregations