Search in sources :

Example 11 with JdbcConnection

use of org.h2.jdbc.JdbcConnection 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 12 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class JdbcDataSource method getJdbcConnection.

private JdbcConnection getJdbcConnection(String user, char[] password) throws SQLException {
    if (isDebugEnabled()) {
        debugCode("getJdbcConnection(" + quote(user) + ", new char[0]);");
    }
    Properties info = new Properties();
    info.setProperty("user", user);
    info.put("password", password);
    Connection conn = Driver.load().connect(url, info);
    if (conn == null) {
        throw new SQLException("No suitable driver found for " + url, "08001", 8001);
    } else if (!(conn instanceof JdbcConnection)) {
        throw new SQLException("Connecting with old version is not supported: " + url, "08001", 8001);
    }
    return (JdbcConnection) conn;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) JdbcConnection(org.h2.jdbc.JdbcConnection) JdbcConnection(org.h2.jdbc.JdbcConnection) Properties(java.util.Properties)

Example 13 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class TestLob method testConcurrentCreate.

private void testConcurrentCreate() throws Exception {
    deleteDb("lob");
    final JdbcConnection conn1 = (JdbcConnection) getConnection("lob");
    final JdbcConnection conn2 = (JdbcConnection) getConnection("lob");
    conn1.setAutoCommit(false);
    conn2.setAutoCommit(false);
    final byte[] buffer = new byte[10000];
    Task task1 = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Blob b = conn1.createBlob();
                OutputStream out = b.setBinaryStream(1);
                out.write(buffer);
                out.close();
            }
        }
    };
    Task task2 = new Task() {

        @Override
        public void call() throws Exception {
            while (!stop) {
                Blob b = conn2.createBlob();
                OutputStream out = b.setBinaryStream(1);
                out.write(buffer);
                out.close();
            }
        }
    };
    task1.execute();
    task2.execute();
    Thread.sleep(1000);
    task1.get();
    task2.get();
    conn1.close();
    conn2.close();
}
Also used : Task(org.h2.util.Task) Blob(java.sql.Blob) OutputStream(java.io.OutputStream) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 14 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class TestLob method testJavaObject.

private void testJavaObject() throws SQLException {
    deleteDb("lob");
    JdbcConnection conn = (JdbcConnection) getConnection("lob");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, DATA OTHER)");
    PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST VALUES(1, ?)");
    prep.setObject(1, new TestLobObject("abc"));
    prep.execute();
    ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
    rs.next();
    Object oa = rs.getObject(2);
    assertEquals(TestLobObject.class.getName(), oa.getClass().getName());
    Object ob = rs.getObject("DATA");
    assertEquals(TestLobObject.class.getName(), ob.getClass().getName());
    assertEquals("TestLobObject: abc", oa.toString());
    assertEquals("TestLobObject: abc", ob.toString());
    assertFalse(rs.next());
    conn.createStatement().execute("drop table test");
    stat.execute("create table test(value other)");
    prep = conn.prepareStatement("insert into test values(?)");
    prep.setObject(1, JdbcUtils.serialize("", conn.getSession().getDataHandler()));
    prep.execute();
    rs = stat.executeQuery("select value from test");
    while (rs.next()) {
        assertEquals("", (String) rs.getObject("value"));
    }
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement)

Example 15 with JdbcConnection

use of org.h2.jdbc.JdbcConnection in project h2database by h2database.

the class TestLob method testBufferedInputStreamBug.

/**
 * Test a bug where the usage of BufferedInputStream in LobStorageMap was
 * causing a deadlock.
 */
private void testBufferedInputStreamBug() throws SQLException {
    deleteDb("lob");
    JdbcConnection conn = (JdbcConnection) getConnection("lob");
    conn.createStatement().execute("CREATE TABLE TEST(test BLOB)");
    PreparedStatement ps = conn.prepareStatement("INSERT INTO TEST(test) VALUES(?)");
    ps.setBlob(1, new ByteArrayInputStream(new byte[257]));
    ps.executeUpdate();
    conn.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

JdbcConnection (org.h2.jdbc.JdbcConnection)27 SQLException (java.sql.SQLException)15 Connection (java.sql.Connection)14 Statement (java.sql.Statement)13 PreparedStatement (java.sql.PreparedStatement)9 Session (org.h2.engine.Session)8 ResultSet (java.sql.ResultSet)6 IOException (java.io.IOException)5 DbException (org.h2.message.DbException)4 Parser (org.h2.command.Parser)3 Task (org.h2.util.Task)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 Savepoint (java.sql.Savepoint)2 Properties (java.util.Properties)2 IgniteH2Indexing (org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 Expression (org.h2.expression.Expression)2