/** * Extract batching error from general exception. * @param e Exception * @param rowsAffected List containing the number of affected rows for every query in batch. * @param err Error tuple containing error code and error message. */ private static void extractBatchError(Exception e, List<Long> rowsAffected, IgniteBiTuple<Integer, String> err) { if (e instanceof IgniteSQLException) { BatchUpdateException batchCause = X.cause(e, BatchUpdateException.class); if (batchCause != null) { if (rowsAffected != null) { for (long cnt : batchCause.getLargeUpdateCounts()) rowsAffected.add(cnt); } err.set(batchCause.getErrorCode(), batchCause.getMessage()); } else err.set(((IgniteSQLException)e).statusCode(), OdbcUtils.tryRetrieveH2ErrorMessage(e)); } else err.set(IgniteQueryErrorCode.UNKNOWN, e.getMessage()); }
updCntsAcc.add(updCntsOnErr[i]); msg = batchCause.getMessage();
/** * Test error code for the case when error is caused on batch execution. * @throws SQLException if failed. */ @Test public void testBatchUpdateException() throws SQLException { try (final Connection conn = getConnection()) { try (Statement stmt = conn.createStatement()) { stmt.executeUpdate("CREATE TABLE test (id int primary key, val varchar)"); stmt.addBatch("insert into test (id, val) values (1, 'val1')"); stmt.addBatch("insert into test (id, val) values (2, 'val2')"); stmt.addBatch("insert into test (id1, val1) values (3, 'val3')"); stmt.executeBatch(); fail("BatchUpdateException is expected"); } catch (BatchUpdateException e) { assertEquals(3, e.getUpdateCounts().length); assertArrayEquals("", new int[] {1, 1, Statement.EXECUTE_FAILED}, e.getUpdateCounts()); assertEquals("42000", e.getSQLState()); assertTrue("Unexpected error message: " + e.getMessage(), e.getMessage() != null && e.getMessage().contains("Failed to parse query. Column \"ID1\" not found")); } } }
/** * @throws SQLException If failed. */ @Test public void testHeterogeneousBatchException() throws SQLException { stmt.addBatch("insert into Person (_key, id, firstName, lastName, age) values ('p0', 0, 'Name0', 'Lastname0', 10)"); stmt.addBatch("insert into Person (_key, id, firstName, lastName, age) values ('p1', 1, 'Name1', 'Lastname1', 20), ('p2', 2, 'Name2', 'Lastname2', 30)"); stmt.addBatch("merge into Person (_key, id, firstName, lastName, age) values ('p3', 3, 'Name3', 'Lastname3', 40)"); stmt.addBatch("update Person set id = 'FAIL' where age >= 30"); // Fail. stmt.addBatch("merge into Person (_key, id, firstName, lastName, age) values ('p0', 2, 'Name2', 'Lastname2', 50)"); stmt.addBatch("delete from Person where FAIL <= 40"); // Fail. try { stmt.executeBatch(); fail("BatchUpdateException must be thrown"); } catch (BatchUpdateException e) { int[] updCnts = e.getUpdateCounts(); if (!e.getMessage().contains("Value conversion failed")) { log.error("Invalid exception: ", e); fail(); } assertEquals("Invalid update counts size", 6, updCnts.length); assertArrayEquals("Invalid update count", new int[] {1, 2, 1, Statement.EXECUTE_FAILED, 1, Statement.EXECUTE_FAILED}, updCnts); } }
assertEquals("Invalid update count", i != FAILED_IDX ? 1 : Statement.EXECUTE_FAILED, updCnts[i]); if (!e.getMessage().contains("Value conversion failed")) { log.error("Invalid exception: ", e);
assertEquals("Invalid update count", i != FAILED_IDX ? 1 : Statement.EXECUTE_FAILED, updCnts[i]); if (!e.getMessage().contains("Value conversion failed")) { log.error("Invalid exception: ", e);
assertEquals("Invalid update count", i != FAILED_IDX ? 1 : Statement.EXECUTE_FAILED, updCnts[i]); if (!e.getMessage().contains("Data conversion error converting \"FAIL\"")) { log.error("Invalid exception: ", e);
assertEquals("Invalid update count", i != FAILED_IDX ? 1 : Statement.EXECUTE_FAILED, updCnts[i]); if (!e.getMessage().contains("Data conversion error converting \"FAIL\"")) { log.error("Invalid exception: ", e);
updCnts[i]); if (!e.getMessage().contains("Value conversion failed")) { log.error("Invalid exception: ", e);
updCnts[i]); if (!e.getMessage().contains("Failed to INSERT some keys because they are already in cache [keys=[p0]")) { log.error("Invalid exception: ", e);
updCnts[i]); if (!e.getMessage().contains("Given statement type does not match that declared by JDBC driver")) { log.error("Invalid exception: ", e);
updCnts[i]); if (!e.getMessage().contains("Value conversion failed")) { log.error("Invalid exception: ", e);
log.warn("Caught BatchUpdateException, one or more batch update have failed: " + be.getMessage(), be); throw be; } catch (SQLException sqle) {
@Override protected void processBatch() { logger.debug("start {} end {}", batchStartIdx, tuples.size()); try { for (int i = batchStartIdx; i < tuples.size(); i++) { String copyStmt = generateCopyStatement(tuples.get(i)); stmt.addBatch(copyStmt); } stmt.executeBatch(); stmt.clearBatch(); batchStartIdx += tuples.size() - batchStartIdx; } catch (BatchUpdateException bue) { logger.error(bue.getMessage()); processUpdateCounts(bue.getUpdateCounts(), tuples.size() - batchStartIdx); } catch (SQLException e) { throw new RuntimeException("processing batch", e); } }
protected int executeBatchAndGetCount(PreparedStatement statement) throws SQLException { int result = 0; try { int[] batchResult = statement.executeBatch(); result += sum(batchResult); } catch (BatchUpdateException e) { if (dieOnError) { throw e; } else { int[] batchResult = e.getUpdateCounts(); result += sum(batchResult); LOG.warn(e.getMessage()); } } int count = statement.getUpdateCount(); result = Math.max(result, count); return result; }
protected void processBatch() { logger.debug("start {} end {}", batchStartIdx, tuples.size()); try { for (int i = batchStartIdx; i < tuples.size(); i++) { setStatementParameters(updateCommand, tuples.get(i)); updateCommand.addBatch(); } updateCommand.executeBatch(); updateCommand.clearBatch(); batchStartIdx += tuples.size() - batchStartIdx; } catch (BatchUpdateException bue) { logger.error(bue.getMessage()); processUpdateCounts(bue.getUpdateCounts(), tuples.size() - batchStartIdx); } catch (SQLException e) { throw new RuntimeException("processing batch", e); } }
protected int executeBatchAndGetCount(PreparedStatement statement) throws SQLException { int result = 0; try { int[] batchResult = statement.executeBatch(); result += sum(batchResult); } catch (BatchUpdateException e) { if (dieOnError) { throw e; } else { int[] batchResult = e.getUpdateCounts(); result += sum(batchResult); LOG.warn(e.getMessage()); } } int count = statement.getUpdateCount(); result = Math.max(result, count); return result; }
private synchronized void addOntologyPatterns(URI physicalURI, OWLOntology ontology, Multiset<OWLAxiom> patterns){ int ontologyId = getOntologyID(physicalURI, ontology); for (OWLAxiom pattern : patterns.elementSet()) { try { int patternId = addPattern(pattern); int occurrences = patterns.count(pattern); insertOntologyPatternPs.setInt(1, ontologyId); insertOntologyPatternPs.setInt(2, patternId); insertOntologyPatternPs.setInt(3, occurrences); insertOntologyPatternPs.addBatch(); } catch (SQLException e) { LOGGER.error("Failed to insert pattern\n" + pattern + "\"", e); } } try { insertOntologyPatternPs.executeBatch(); } catch (BatchUpdateException e) { LOGGER.error("Failed to insert some pattern. Reason: {}", e.getMessage()); } catch (SQLException e) { LOGGER.error("Failed to insert patterns.", e); } }
/** * Extract batching error from general exception. * @param e Exception * @param rowsAffected List containing the number of affected rows for every query in batch. * @param err Error tuple containing error code and error message. */ private static void extractBatchError(Exception e, List<Long> rowsAffected, IgniteBiTuple<Integer, String> err) { if (e instanceof IgniteSQLException) { BatchUpdateException batchCause = X.cause(e, BatchUpdateException.class); if (batchCause != null) { if (rowsAffected != null) { for (long cnt : batchCause.getLargeUpdateCounts()) rowsAffected.add(cnt); } err.set(batchCause.getErrorCode(), batchCause.getMessage()); } else err.set(((IgniteSQLException)e).statusCode(), OdbcUtils.tryRetrieveH2ErrorMessage(e)); } else err.set(IgniteQueryErrorCode.UNKNOWN, e.getMessage()); }
throw new BatchUpdateException(e.getMessage(), e.getSQLState(), e.getErrorCode(), allUpdateCounts, e);