下面列出了怎么用com.datastax.driver.core.exceptions.TruncateException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testShouldReturnTruncateError() throws Exception {
String message = "This is a truncate error";
server.prime(when(query).then(truncateError(message)));
thrown.expect(TruncateException.class);
thrown.expectMessage(message);
query();
}
private static boolean truncateTableInternal(CommandContext context, String table) {
PreparedStatement truncateStatement = context.getTargetSession().prepare(
"TRUNCATE \"" + table + "\""
);
try {
context.getTargetCassandraExecutor().executeUpdate(truncateStatement.bind()).toBlocking().firstOrDefault(null);
} catch (TruncateException e) {
// Check if the table is empty
logger.info("Couldn't complete the truncate operation. Checking if the table is empty: {}", table);
Pair<Object, Object> value = readTwoColumnTable(context.getTargetSession(), table).take(1).toBlocking().firstOrDefault(null);
if (value == null) {
// Truncate failed, but the table is empty. It is ok to move on.
logger.info("Truncate deemed as successful, as the table is empty: {}", table);
return true;
}
if (e.getMessage().contains("Cannot achieve consistency level ALL")) {
logger.warn("Recoverable truncate operations for table {}. Cause: {}", table, e.getMessage());
return false;
}
// Not recoverable error. Re-throw it.
throw e;
}
logger.info("Truncated table {}.{}", truncateStatement.getQueryKeyspace(), table);
return true;
}
protected void checkCassandraException(Exception e) {
_mexceptions.incr();
if (e instanceof AlreadyExistsException ||
e instanceof AuthenticationException ||
e instanceof DriverException ||
e instanceof DriverInternalError ||
e instanceof InvalidConfigurationInQueryException ||
e instanceof InvalidQueryException ||
e instanceof InvalidTypeException ||
e instanceof QueryExecutionException ||
e instanceof QueryValidationException ||
e instanceof ReadTimeoutException ||
e instanceof SyntaxError ||
e instanceof TraceRetrievalException ||
e instanceof TruncateException ||
e instanceof UnauthorizedException ||
e instanceof UnavailableException ||
e instanceof ReadTimeoutException ||
e instanceof WriteTimeoutException ||
e instanceof ReadFailureException ||
e instanceof WriteFailureException ||
e instanceof FunctionExecutionException) {
throw new ReportedFailedException(e);
} else {
throw new RuntimeException(e);
}
}