public int[] executeBatch() throws SQLException { if (batch == null) { return new int[0]; } int[] ret = new int[batch.size()]; for (int i = 0; i < ret.length; i++) { ret[i] = EXECUTE_FAILED; } int errs = 0; for (int i = 0; i < ret.length; i++) { try { execute((String) batch.get(i)); ret[i] = updcnt; } catch (SQLException e) { ++errs; } } if (errs > 0) { throw new BatchUpdateException("batch failed", ret); } return ret; }
public int[] executeBatch() throws SQLException { if (batch == null) { return new int[0]; } int[] ret = new int[batch.size() / args.length]; for (int i = 0; i < ret.length; i++) { ret[i] = EXECUTE_FAILED; } int errs = 0; int index = 0; for (int i = 0; i < ret.length; i++) { for (int k = 0; k < args.length; k++) { BatchArg b = (BatchArg) batch.get(index++); args[k] = b.arg; blobs[k] = b.blob; } try { ret[i] = executeUpdate(); } catch (SQLException e) { ++errs; } } if (errs > 0) { throw new BatchUpdateException("batch failed", ret); } return ret; }
@Override protected boolean insertBatch(PreparedStatement pstmt) throws SQLException { for (JdbcEntryData pendingEntry : TeradataBufferedInserter.this.pendingInserts) { int i = 1; for (JdbcEntryDatum datum : pendingEntry) { Object value = datum.getVal(); if (value != null) { pstmt.setObject(i, value); } else { // Column type is needed for null value insertion pstmt.setNull(i, columnPosSqlTypes.get(i)); } i++; } pstmt.addBatch(); pstmt.clearParameters(); } if (LOG.isDebugEnabled()) { LOG.debug("Executing SQL " + pstmt); } int[] execStatus = pstmt.executeBatch(); // Check status explicitly if driver continues batch insertion upon failure for (int status : execStatus) { if (status == Statement.EXECUTE_FAILED) { throw new BatchUpdateException("Batch insert failed.", execStatus); } } return true; }
public void handleError(SQLException newError) { if (batchException == null) { int[] successCounts; if (resultIndex >= updateCounts.length) successCounts = updateCounts; else { successCounts = new int[resultIndex]; System.arraycopy(updateCounts, 0, successCounts, 0, resultIndex); } String queryString = "<unknown>"; if (resultIndex < queries.length) queryString = queries[resultIndex].toString(parameterLists[resultIndex]); batchException = new BatchUpdateException(GT.tr("Batch entry {0} {1} was aborted. Call getNextException to see the cause.", new Object[]{ new Integer(resultIndex), queryString}), newError.getSQLState(), successCounts); } batchException.setNextException(newError); }
public void handleError(SQLException newError) { if (batchException == null) { int[] successCounts; if (resultIndex >= updateCounts.length) successCounts = updateCounts; else { successCounts = new int[resultIndex]; System.arraycopy(updateCounts, 0, successCounts, 0, resultIndex); } String queryString = "<unknown>"; if (resultIndex < queries.length) queryString = queries[resultIndex].toString(parameterLists[resultIndex]); batchException = new BatchUpdateException(GT.tr("Batch entry {0} {1} was aborted. Call getNextException to see the cause.", new Object[]{ new Integer(resultIndex), queryString}), newError.getSQLState(), successCounts); } batchException.setNextException(newError); }
@Override public void handleError(SQLException newError) { if (getException() == null) { Arrays.fill(updateCounts, committedRows, updateCounts.length, Statement.EXECUTE_FAILED); if (allGeneratedRows != null) { allGeneratedRows.clear(); } String queryString = "<unknown>"; if (resultIndex < queries.length) { queryString = queries[resultIndex].toString(parameterLists[resultIndex]); } BatchUpdateException batchException = new BatchUpdateException( GT.tr("Batch entry {0} {1} was aborted: {2} Call getNextException to see other errors in the batch.", resultIndex, queryString, newError.getMessage()), newError.getSQLState(), uncompressUpdateCount()); batchException.initCause(newError); super.handleError(batchException); } resultIndex++; super.handleError(newError); }
public void handleCompletion() throws SQLException { updateGeneratedKeys(); SQLException batchException = getException(); if (batchException != null) { if (isAutoCommit()) { // Re-create batch exception since rows after exception might indeed succeed. BatchUpdateException newException = new BatchUpdateException( batchException.getMessage(), batchException.getSQLState(), uncompressUpdateCount() ); newException.initCause(batchException.getCause()); SQLException next = batchException.getNextException(); if (next != null) { newException.setNextException(next); } batchException = newException; } throw batchException; } }
@Test public void testBatchUpdateWithBatchFailure() throws Exception { final String[] sql = {"A", "B", "C", "D"}; given(this.statement.executeBatch()).willThrow( new BatchUpdateException(new int[] {1, Statement.EXECUTE_FAILED, 1, Statement.EXECUTE_FAILED})); mockDatabaseMetaData(true); given(this.connection.createStatement()).willReturn(this.statement); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); try { template.batchUpdate(sql); } catch (UncategorizedSQLException ex) { assertThat(ex.getSql(), equalTo("B; D")); } }
throw new BatchUpdateException(Arrays.copyOf(updCntrs, idx), ex);
@Test public void batchExceptionTranslation() { SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); SQLException badSqlEx = new SQLException("", "", 1); BatchUpdateException batchUpdateEx = new BatchUpdateException(); batchUpdateEx.setNextException(badSqlEx); BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", batchUpdateEx); assertEquals("SQL", bsgex.getSql()); assertEquals(badSqlEx, bsgex.getSQLException()); }
BatchUpdateException e = new BatchUpdateException(resEx.getMessage(), resEx.getSQLState(), resEx.getErrorCode(), snd.perRowCounterAsArray(), resEx);
BatchUpdateException e = new BatchUpdateException(batchException.getMessage(), batchException.getSQLState(), batchException.getErrorCode(), cntPerRow, batchException);
/** * */ void readResponses () { try { while (true) { JdbcResponse resp = cliIo.readResponse(); if (resp.response() instanceof JdbcOrderedBatchExecuteResult) { JdbcOrderedBatchExecuteResult res = (JdbcOrderedBatchExecuteResult)resp.response(); respSem.release(); if (res.errorCode() != ClientListenerResponse.STATUS_SUCCESS) { err = new BatchUpdateException(res.errorMessage(), IgniteQueryErrorCode.codeToSqlState(res.errorCode()), res.errorCode(), res.updateCounts()); } // Receive the response for the last request. if (res.order() == lastRespOrder) { lastRespFut.onDone(); break; } } if (resp.status() != ClientListenerResponse.STATUS_SUCCESS) err = new SQLException(resp.error(), IgniteQueryErrorCode.codeToSqlState(resp.status())); } } catch (Exception e) { err = e; } } }
/** {@inheritDoc} */ @Override public int[] executeBatch() throws SQLException { ensureNotClosed(); closeResults(); checkStatementBatchEmpty(); if (conn.isStream()) { int[] res = new int[batchSize]; batchSize = 0; return res; } if (F.isEmpty(batch)) return new int[0]; JdbcBatchExecuteRequest req = new JdbcBatchExecuteRequest(conn.getSchema(), batch, conn.getAutoCommit(), false); try { JdbcBatchExecuteResult res = conn.sendRequest(req, this); if (res.errorCode() != ClientListenerResponse.STATUS_SUCCESS) { throw new BatchUpdateException(res.errorMessage(), IgniteQueryErrorCode.codeToSqlState(res.errorCode()), res.errorCode(), res.updateCounts()); } return res.updateCounts(); } finally { batchSize = 0; batch = null; } }
@Override public BatchUpdateException createBatchUpdateException(String reason, String SQLState, int vendorCode, long[] updateCounts, Throwable cause) { return new BatchUpdateException(reason, SQLState, vendorCode, updateCounts, cause); } }
@Override public BatchUpdateException getException(int batchIdx, String message, Exception cause) { int[] counts; if (strict) { counts = Arrays.copyOf(this.counts, batchIdx); } else { counts = Arrays.copyOf(this.counts, batchIdx + 1); counts[batchIdx] = EXECUTE_FAILED; } return new BatchUpdateException(message, null, 0, counts, cause != null ? makeSQLException(cause) : null); }
@Override public BatchUpdateException createBatchUpdateException(String reason, String SQLState, int vendorCode, long[] updateCounts, Throwable cause) { return new BatchUpdateException(reason, SQLState, vendorCode, copyUpdateCount(updateCounts), cause); }
@Override public BatchUpdateException getException(int batchIdx, String message, Exception cause) { long[] counts; if (strict) { counts = Arrays.copyOf(this.counts, batchIdx); } else { counts = Arrays.copyOf(this.counts, batchIdx + 1); counts[batchIdx] = EXECUTE_FAILED; } return new BatchUpdateException(message, null, 0, counts, cause != null ? makeSQLException(cause) : null); }
@Test public void testSetRootCause() { Throwable t = new RuntimeException("bla1", new BatchUpdateException("Sql bla", "ORA-500", 500, new int[]{1, 2}, new RuntimeException("la la"))); Throwables.setRootCause(t, new RuntimeException("some other ex"), 3); LOG.debug("Thowable", t); List<Throwable> causalChain = com.google.common.base.Throwables.getCausalChain(t); Assert.assertEquals(4, causalChain.size()); Assert.assertEquals(Throwables.TrimmedException.class, causalChain.get(3).getClass()); }
@Test public void testSetRootCause2() { Throwable t = new RuntimeException("bla1", new BatchUpdateException("Sql bla", "ORA-500", 500, new int[]{1, 2})); Throwables.setRootCause(t, new RuntimeException("some other ex"), 3); LOG.debug("Thowable", t); List<Throwable> causalChain = com.google.common.base.Throwables.getCausalChain(t); Assert.assertEquals(3, causalChain.size()); Assert.assertEquals("some other ex", causalChain.get(2).getMessage()); }