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"));
}
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;
}
}
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);
}
Aggregations