Search in sources :

Example 26 with Task

use of org.h2.util.Task in project h2database by h2database.

the class JdbcBlob method setBinaryStream.

/**
 * Get a writer to update the Blob. This is only supported for new, empty
 * Blob objects that were created with Connection.createBlob(). The Blob is
 * created in a separate thread, and the object is only updated when
 * OutputStream.close() is called. The position must be 1, meaning the whole
 * Blob data is set.
 *
 * @param pos where to start writing (the first byte is at position 1)
 * @return an output stream
 */
@Override
public OutputStream setBinaryStream(long pos) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("setBinaryStream(" + pos + ");");
        }
        checkClosed();
        if (pos != 1) {
            throw DbException.getInvalidValueException("pos", pos);
        }
        if (value.getPrecision() != 0) {
            throw DbException.getInvalidValueException("length", value.getPrecision());
        }
        // local variable avoids generating synthetic accessor method
        final JdbcConnection c = conn;
        final PipedInputStream in = new PipedInputStream();
        final Task task = new Task() {

            @Override
            public void call() {
                value = c.createBlob(in, -1);
            }
        };
        PipedOutputStream out = new PipedOutputStream(in) {

            @Override
            public void close() throws IOException {
                super.close();
                try {
                    task.get();
                } catch (Exception e) {
                    throw DbException.convertToIOException(e);
                }
            }
        };
        task.execute();
        return new BufferedOutputStream(out);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Task(org.h2.util.Task) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 27 with Task

use of org.h2.util.Task in project h2database by h2database.

the class JdbcClob method setCharacterStream.

/**
 * Get a writer to update the Clob. This is only supported for new, empty
 * Clob objects that were created with Connection.createClob() or
 * createNClob(). The Clob is created in a separate thread, and the object
 * is only updated when Writer.close() is called. The position must be 1,
 * meaning the whole Clob data is set.
 *
 * @param pos where to start writing (the first character is at position 1)
 * @return a writer
 */
@Override
public Writer setCharacterStream(long pos) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCodeCall("setCharacterStream(" + pos + ");");
        }
        checkClosed();
        if (pos != 1) {
            throw DbException.getInvalidValueException("pos", pos);
        }
        if (value.getPrecision() != 0) {
            throw DbException.getInvalidValueException("length", value.getPrecision());
        }
        // required to avoid synthetic method creation
        final JdbcConnection c = conn;
        // PipedReader / PipedWriter are a lot slower
        // than PipedInputStream / PipedOutputStream
        // (Sun/Oracle Java 1.6.0_20)
        final PipedInputStream in = new PipedInputStream();
        final Task task = new Task() {

            @Override
            public void call() {
                value = c.createClob(IOUtils.getReader(in), -1);
            }
        };
        PipedOutputStream out = new PipedOutputStream(in) {

            @Override
            public void close() throws IOException {
                super.close();
                try {
                    task.get();
                } catch (Exception e) {
                    throw DbException.convertToIOException(e);
                }
            }
        };
        task.execute();
        return IOUtils.getBufferedWriter(out);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Task(org.h2.util.Task) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 28 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestPreparedStatement method testCancelReuse.

private void testCancelReuse(Connection conn) throws Exception {
    conn.createStatement().execute("CREATE ALIAS SLEEP FOR \"java.lang.Thread.sleep\"");
    // sleep for 10 seconds
    final PreparedStatement prep = conn.prepareStatement("SELECT SLEEP(?) FROM SYSTEM_RANGE(1, 10000) LIMIT ?");
    prep.setInt(1, 1);
    prep.setInt(2, 10000);
    Task t = new Task() {

        @Override
        public void call() throws SQLException {
            TestPreparedStatement.this.execute(prep);
        }
    };
    t.execute();
    Thread.sleep(100);
    prep.cancel();
    SQLException e = (SQLException) t.getException();
    assertNotNull(e);
    assertEquals(ErrorCode.STATEMENT_WAS_CANCELED, e.getErrorCode());
    prep.setInt(1, 1);
    prep.setInt(2, 1);
    ResultSet rs = prep.executeQuery();
    assertTrue(rs.next());
    assertEquals(0, rs.getInt(1));
    assertFalse(rs.next());
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 29 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestConcurrentConnectionUsage method testAutoCommit.

private void testAutoCommit() throws SQLException {
    deleteDb(getTestName());
    final Connection conn = getConnection(getTestName());
    final PreparedStatement p1 = conn.prepareStatement("select 1 from dual");
    Task t = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                p1.executeQuery();
                conn.setAutoCommit(true);
                conn.setAutoCommit(false);
            }
        }
    }.execute();
    PreparedStatement prep = conn.prepareStatement("select ? from dual");
    for (int i = 0; i < 10; i++) {
        prep.setBinaryStream(1, new ByteArrayInputStream(new byte[1024]));
        prep.executeQuery();
    }
    t.get();
    conn.close();
}
Also used : Task(org.h2.util.Task) ByteArrayInputStream(java.io.ByteArrayInputStream) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 30 with Task

use of org.h2.util.Task in project h2database by h2database.

the class TestMvccMultiThreaded method testConcurrentSelectForUpdate.

private void testConcurrentSelectForUpdate() throws Exception {
    deleteDb(getTestName());
    Connection conn = getConnection(getTestName() + ";MULTI_THREADED=TRUE");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int not null primary key, updated int not null)");
    stat.execute("insert into test(id, updated) values(1, 100)");
    ArrayList<Task> tasks = new ArrayList<>();
    int count = 3;
    for (int i = 0; i < count; i++) {
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                Connection conn = getConnection(getTestName());
                Statement stat = conn.createStatement();
                try {
                    while (!stop) {
                        try {
                            stat.execute("select * from test where id=1 for update");
                        } catch (SQLException e) {
                            int errorCode = e.getErrorCode();
                            assertTrue(e.getMessage(), errorCode == ErrorCode.DEADLOCK_1 || errorCode == ErrorCode.LOCK_TIMEOUT_1);
                        }
                    }
                } finally {
                    conn.close();
                }
            }
        }.execute();
        tasks.add(task);
    }
    for (int i = 0; i < 10; i++) {
        Thread.sleep(100);
        ResultSet rs = stat.executeQuery("select * from test");
        assertTrue(rs.next());
    }
    for (Task t : tasks) {
        t.get();
    }
    conn.close();
    deleteDb(getTestName());
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException)

Aggregations

Task (org.h2.util.Task)71 Connection (java.sql.Connection)33 Statement (java.sql.Statement)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 PreparedStatement (java.sql.PreparedStatement)22 SQLException (java.sql.SQLException)22 MVStore (org.h2.mvstore.MVStore)20 Random (java.util.Random)18 ResultSet (java.sql.ResultSet)14 JdbcConnection (org.h2.jdbc.JdbcConnection)7 IOException (java.io.IOException)5 ServerSocket (java.net.ServerSocket)5 Socket (java.net.Socket)5 ConcurrentModificationException (java.util.ConcurrentModificationException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 SSLServerSocket (javax.net.ssl.SSLServerSocket)4 SSLSocket (javax.net.ssl.SSLSocket)4 OutputStream (java.io.OutputStream)3 PipedInputStream (java.io.PipedInputStream)3 PipedOutputStream (java.io.PipedOutputStream)3