Search in sources :

Example 41 with LoopingAlphabetStream

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();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 42 with LoopingAlphabetStream

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();
}
Also used : Blob(java.sql.Blob) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 43 with LoopingAlphabetStream

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.
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ReadOnceByteArrayInputStream(org.apache.derbyTesting.functionTests.util.streams.ReadOnceByteArrayInputStream) TestInputStream(org.apache.derbyTesting.functionTests.util.TestInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) EOFException(java.io.EOFException) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 44 with LoopingAlphabetStream

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);
    }
}
Also used : LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 45 with LoopingAlphabetStream

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;
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Aggregations

LoopingAlphabetStream (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)61 PreparedStatement (java.sql.PreparedStatement)33 InputStream (java.io.InputStream)31 ResultSet (java.sql.ResultSet)31 Statement (java.sql.Statement)20 Blob (java.sql.Blob)10 SQLException (java.sql.SQLException)9 LoopingAlphabetReader (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)9 PositionedStoreStream (org.apache.derby.impl.jdbc.PositionedStoreStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Connection (java.sql.Connection)7 OutputStream (java.io.OutputStream)5 Reader (java.io.Reader)5 Clob (java.sql.Clob)3 Timestamp (java.sql.Timestamp)3 DataInputStream (java.io.DataInputStream)2 EOFException (java.io.EOFException)2 CallableStatement (java.sql.CallableStatement)2 Date (java.sql.Date)2 Time (java.sql.Time)2