Search in sources :

Example 51 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class LobStreamsTest method testGettingBlobTwice.

/**
 * Originally tested that the usage pattern {@code rs.getBlob().method()}
 * didn't cause the underlying source stream to be closed too early. This
 * behavior was forbidden, the test now checks that an exception is thrown.
 * <p>
 * Test description: Select from a BLOB column, access the BLOB using the
 * pattern rs.getBlob(1).blobMethod() (note that we do not keep a reference
 * to the Blob-object), provoke/invoke GC and finalization, and finally try
 * to access the same BLOB again (through a different/another call to
 * rs.getBlob(1)).
 * <p>
 * Note that the BLOB must be of a certain size (i.e. multiple pages), such
 * that it is stored/accessed as a stream in store.
 * <p>
 * See DERBY-3844 and DERBY-4440.
 *
 * @throws Exception if something goes wrong
 */
public void testGettingBlobTwice() throws Exception {
    setAutoCommit(false);
    // We need a Blob represented as a stream in store.
    int length = 71 * 1024 + 7;
    PreparedStatement ps = prepareStatement("insert into testBlobX1(a,b) values (?,?)");
    ps.setInt(1, 2);
    ps.setBinaryStream(2, new LoopingAlphabetStream(length), length);
    ps.executeUpdate();
    ps.close();
    // Get a result set with the Blob.
    ps = prepareStatement("select b from testBlobX1 where a = ?");
    ps.setInt(1, 2);
    ResultSet rs = ps.executeQuery();
    assertTrue(rs.next());
    Blob b = rs.getBlob(1);
    try {
        // Get the length, but don't keep a reference to the Blob.
        assertEquals(length, rs.getBlob(1).length());
        fail("Getting the Blob the second time should have failed");
    } catch (SQLException sqle) {
        assertSQLState("XCL18", sqle);
    }
    // Increase the likelyhood of getting the finalizer run.
    // Create some junk to fill up the heap, hopefully not JIT'ed away...
    // 10 K
    int size = 10 * 1024;
    byte[] bytes = null;
    for (int i = 0; i < 50; i++) {
        bytes = new byte[size * (i + 1)];
    }
    // For good measure...
    System.gc();
    System.runFinalization();
    try {
        Thread.sleep(100L);
    } catch (InterruptedException ie) {
    // No need to reset the interrupted flag here in the test.
    }
    // This will fail if the finalizer caused the source stream to be
    // closed and the source page to be unlatched.
    InputStream is = b.getBinaryStream();
    while (is.read() != -1) {
    // Keep on reading...
    }
    assertNotNull(bytes);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 52 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class LobStreamsTest method testClobAsciiWrite3Param.

/**
 * Tests the ClobOutputStream.write(byte  b[], int off, int len) method
 */
public void testClobAsciiWrite3Param() throws Exception {
    InputStream streamIn = new LoopingAlphabetStream(streamSize[0]);
    assertTrue("FAIL -- file not found", streamIn != null);
    PreparedStatement stmt3 = prepareStatement("SELECT c FROM testBlobX1 WHERE a = 1");
    ResultSet rs3 = stmt3.executeQuery();
    rs3.next();
    Clob clob = rs3.getClob(1);
    assertTrue("FAIL -- clob is NULL", clob != null);
    int count = 0;
    byte[] buffer = new byte[1024];
    OutputStream outstream = clob.setAsciiStream(1L);
    while ((count = streamIn.read(buffer)) != -1) {
        outstream.write(buffer, 0, count);
    }
    outstream.close();
    streamIn.close();
    PreparedStatement stmt4 = prepareStatement("UPDATE testBlobX1 SET c = ? WHERE a = 1");
    stmt4.setClob(1, clob);
    stmt4.executeUpdate();
    stmt4.close();
    rs3.close();
    rs3 = stmt3.executeQuery();
    assertTrue("FAIL -- clob not found", rs3.next());
    clob = rs3.getClob(1);
    long new_length = clob.length();
    assertEquals("FAIL -- wrong clob length", streamSize[0], new_length);
    // Check contents ...
    InputStream fStream = new LoopingAlphabetStream(streamSize[0]);
    InputStream lStream = clob.getAsciiStream();
    assertTrue("FAIL - Clob and file contents do not match", compareLob2File(fStream, lStream));
    fStream.close();
    lStream.close();
    rs3.close();
    stmt3.close();
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ResultSet(java.sql.ResultSet) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob)

Example 53 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class TriggerTests method runtest2UpdateTrigger.

/**
 * Following will update a row in table1 which will cause update
 * trigger to fire. The update involves the LOB column.
 *
 * @throws SQLException
 */
public void runtest2UpdateTrigger() throws SQLException {
    PreparedStatement ps = prepareStatement("update table1 set bl = ? where id = 1");
    ps.setBinaryStream(1, new LoopingAlphabetStream(lobsize), lobsize);
    ps.executeUpdate();
    commit();
}
Also used : LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 54 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class TriggerTests method test4UpdateAfterTriggerUpdatedLOB.

/**
 * The after update trigger on LOB column which then gets updated in the
 * trigger action. So this test updates the LOB in the trigger action
 * and is also the cause of the update trigger to fire.
 *
 * The UPDATE trigger access the large data in LOB column inside the
 * trigger action which will cause the test to run out of memory. For
 * this reason, this test is disabled.
 *
 * @throws SQLException
 */
public void test4UpdateAfterTriggerUpdatedLOB() throws SQLException {
    if (isDerby1482Fixed == false)
        return;
    if (testWithLargeDataInLOB)
        return;
    basicSetup();
    Statement s = createStatement();
    // The default table1 created by basicSetup does not match the
    // requirement of this test so dropping and recreating it.
    s.execute("drop table table1");
    s.execute("create table table1 (id int, status smallint, bl blob(2G), bl_null blob(2G))");
    s.execute("create trigger trigger1 after update of bl_null on table1 referencing " + "new as n_row for each row " + "update table1 set bl_null=n_row.bl where bl_null is null");
    PreparedStatement ps = prepareStatement("insert into table1 values (?, 0, ?, ?)");
    ps.setInt(1, 1);
    ps.setBinaryStream(2, new LoopingAlphabetStream(lobsize), lobsize);
    ps.setBinaryStream(3, new LoopingAlphabetStream(lobsize), lobsize);
    ps.executeUpdate();
    commit();
    runtest3UpdateTrigger();
}
Also used : LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 55 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class TriggerTests method test2InsertAfterTriggerUpdatedLOB.

/**
 * This test creates an AFTER INSERT trigger which in it's trigger action
 * updates a lob column from the row just inserted. So, this test does
 * update the LOB from the triggering table inside the trigger
 * action.
 *
 * INSERT trigger in this test is inserting large data in the LOB column
 * which will be used in the INSERT trigger and hence it will run out of
 * memory. For that reason, the test is disabled.
 *
 * @throws SQLException
 */
public void test2InsertAfterTriggerUpdatedLOB() throws SQLException {
    if (isDerby1482Fixed == false)
        return;
    if (testWithLargeDataInLOB)
        return;
    basicSetup();
    Statement s = createStatement();
    // The default table1 created by basicSetup does not match the
    // requirement of this test so dropping and recreating it.
    s.execute("drop table table1");
    s.execute("create table table1 (id int, status smallint, bl blob(2G), bl_null blob(2G))");
    PreparedStatement ps = prepareStatement("insert into table1 values (?, 0, ?, null)");
    ps.setInt(1, 1);
    ps.setBinaryStream(2, new LoopingAlphabetStream(lobsize), lobsize);
    ps.executeUpdate();
    s.execute("create trigger trigger1 after INSERT on table1 referencing " + "new as n_row for each row " + "update table1 set bl_null=n_row.bl where bl_null is null");
    commit();
    runtest2InsertTriggerTest();
}
Also used : 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