Search in sources :

Example 1 with JdbcSQLSyntaxErrorException

use of org.h2.jdbc.JdbcSQLSyntaxErrorException in project opentracing-toolbox by zalando.

the class DataSourceTracerTest method shouldTraceErrors.

@Test
void shouldTraceErrors() {
    final DataSource dataSource = unit.trace(original);
    assertThrows(SQLException.class, () -> {
        try (final Connection connection = dataSource.getConnection();
            final Statement statement = connection.createStatement()) {
            statement.execute("SELECT * FROM MATRIX(4)");
        }
    });
    final List<MockSpan> spans = tracer.finishedSpans();
    final MockSpan span = getOnlyElement(spans);
    assertThat(span.operationName(), is("execute"));
    assertThat(span.tags(), hasEntry("component", "JDBC"));
    assertThat(span.tags(), hasEntry("db.statement", "SELECT * FROM MATRIX(4)"));
    assertThat(span.tags(), hasEntry("db.type", "sql"));
    assertThat(span.tags(), hasEntry("span.kind", "client"));
    assertThat(span.tags(), hasEntry("flow_id", "REcCvlqMSReeo7adheiYFA"));
    assertThat(span.tags(), hasEntry("error", true));
    final List<MockSpan.LogEntry> entries = span.logEntries();
    assertThat(entries, hasSize(3));
    assertThat(entries.get(0).fields().get("message").toString(), containsString("Function \"MATRIX\" not found"));
    assertThat(entries.get(1).fields(), hasEntry("error.kind", "JdbcSQLSyntaxErrorException"));
    assertThat(entries.get(1).fields(), hasEntry(equalTo("error.object"), instanceOf(SQLException.class)));
    assertThat(entries.get(2).fields().get("stack").toString(), containsString("at org.h2.jdbc"));
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) MockSpan(io.opentracing.mock.MockSpan) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test)

Example 2 with JdbcSQLSyntaxErrorException

use of org.h2.jdbc.JdbcSQLSyntaxErrorException in project rxlib by RockyLOMO.

the class EntityDatabase method save.

@SneakyThrows
public <T> void save(T entity, boolean doInsert) {
    SqlMeta meta = getMeta(entity.getClass());
    try {
        List<Object> params = new ArrayList<>();
        if (doInsert) {
            for (Map.Entry<String, Tuple<Field, DbColumn>> col : meta.insertView) {
                params.add(col.getValue().left.get(entity));
            }
            executeUpdate(meta.insertSql, params);
            return;
        }
        StringBuilder cols = new StringBuilder(128);
        for (Map.Entry<String, Tuple<Field, DbColumn>> col : meta.secondaryView) {
            Object val = col.getValue().left.get(entity);
            if (val == null) {
                continue;
            }
            cols.append("%s=?,", col.getKey());
            params.add(val);
        }
        cols.setLength(cols.length() - 1);
        Object id = meta.primaryKey.getValue().left.get(entity);
        params.add(id);
        executeUpdate(new StringBuilder(meta.updateSql).replace($UPDATE_COLUMNS, cols.toString()).toString(), params);
    } catch (Exception e) {
        if (e instanceof JdbcSQLSyntaxErrorException && (Strings.startsWith(e.getMessage(), "Column count does not match") || Strings.containsAll(e.getMessage(), "Column", "not found"))) {
            dropMapping(entity.getClass());
            log.info("recreate {} -> {}", entity.getClass(), e.getMessage());
            createMapping(entity.getClass());
            save(entity, doInsert);
            return;
        }
        throw e;
    }
}
Also used : StringBuilder(org.rx.core.StringBuilder) JdbcSQLSyntaxErrorException(org.h2.jdbc.JdbcSQLSyntaxErrorException) App.toJsonString(org.rx.core.App.toJsonString) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractMap(java.util.AbstractMap) InvalidException(org.rx.exception.InvalidException) JdbcSQLSyntaxErrorException(org.h2.jdbc.JdbcSQLSyntaxErrorException) SneakyThrows(lombok.SneakyThrows)

Example 3 with JdbcSQLSyntaxErrorException

use of org.h2.jdbc.JdbcSQLSyntaxErrorException in project ma-core-public by infiniteautomation.

the class H2DatabaseTest method test2ExploitLobStorageMap.

// This test may break database
// Needs to be executed as last test for the suite
// https://github.com/h2database/h2database/issues/1808
@Test(timeout = 10 * 1000)
public void test2ExploitLobStorageMap() throws SQLException {
    Exception expected = null;
    try {
        Connection conn1 = Common.getBean(DatabaseProxy.class).getDataSource().getConnection();
        String createTable = "CREATE TABLE t1 (id int AUTO_INCREMENT, ver bigint, data text, PRIMARY KEY (id))";
        conn1.prepareStatement(createTable).executeUpdate();
        String insert = "INSERT INTO t1 (id, ver, data) values (1, 0, ?)";
        PreparedStatement insertStmt = conn1.prepareStatement(insert);
        String largeData = org.h2.util.StringUtils.pad("", 512, "x", false);
        insertStmt.setString(1, largeData);
        insertStmt.executeUpdate();
        new Thread(() -> {
            try {
                Connection conn2 = Common.getBean(DatabaseProxy.class).getDataSource().getConnection();
                String update = "UPDATE t1 SET ver = ver + 1 WHERE id = 1";
                while (!Thread.currentThread().isInterrupted()) {
                    conn2.prepareStatement(update).executeUpdate();
                }
            } catch (JdbcSQLSyntaxErrorException ex) {
                assertEquals("42S02", ex.getSQLState());
                assertEquals("UPDATE t1 SET ver = ver + 1 WHERE id = 1", ex.getSQL());
                assertEquals("Table \"T1\" not found", ex.getOriginalMessage());
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        }).start();
        while (true) {
            conn1.prepareStatement("SELECT * FROM t1").executeQuery();
        }
    } catch (JdbcSQLNonTransientException ex) {
        expected = ex;
        assertEquals("HY000", ex.getSQLState());
        assertEquals("SELECT * FROM t1", ex.getSQL());
        assertEquals("General error: \"java.lang.NullPointerException\"", ex.getOriginalMessage());
    }
    assertNotNull(expected);
}
Also used : JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) SQLException(java.sql.SQLException) JdbcSQLSyntaxErrorException(org.h2.jdbc.JdbcSQLSyntaxErrorException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JdbcSQLSyntaxErrorException(org.h2.jdbc.JdbcSQLSyntaxErrorException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 JdbcSQLSyntaxErrorException (org.h2.jdbc.JdbcSQLSyntaxErrorException)2 MockSpan (io.opentracing.mock.MockSpan)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 AbstractMap (java.util.AbstractMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DataSource (javax.sql.DataSource)1 SneakyThrows (lombok.SneakyThrows)1 JdbcSQLNonTransientException (org.h2.jdbc.JdbcSQLNonTransientException)1 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 App.toJsonString (org.rx.core.App.toJsonString)1 StringBuilder (org.rx.core.StringBuilder)1 InvalidException (org.rx.exception.InvalidException)1