Exemplo n.º 1
0
  private PreparedStatement toPreparedStatement(String query, Connection.Future future)
      throws NoHostAvailableException {

    try {
      Message.Response response = null;
      try {
        while (response == null) {
          try {
            response = future.get();
          } catch (InterruptedException e) {
            // TODO: decide wether we want to expose Interrupted exceptions or not
            throw new RuntimeException(e);
          }
        }
      } catch (ExecutionException e) {
        ResultSetFuture.extractCauseFromExecutionException(e);
        throw new AssertionError();
      }

      assert response != null;
      switch (response.type) {
        case RESULT:
          ResultMessage rm = (ResultMessage) response;
          switch (rm.kind) {
            case PREPARED:
              ResultMessage.Prepared pmsg = (ResultMessage.Prepared) rm;
              manager.cluster.manager.prepare(pmsg.statementId, query, future.getAddress());
              return PreparedStatement.fromMessage(pmsg, manager.cluster.getMetadata());
            default:
              throw new DriverInternalError(
                  String.format(
                      "%s response received when prepared statement was expected", rm.kind));
          }
        case ERROR:
          ResultSetFuture.extractCause(
              ResultSetFuture.convertException(((ErrorMessage) response).error));
          break;
        default:
          throw new DriverInternalError(
              String.format(
                  "%s response received when prepared statement was expected", response.type));
      }
      throw new AssertionError();
    } catch (QueryExecutionException e) {
      // Preparing a statement cannot throw any of the QueryExecutionException
      throw new DriverInternalError(
          "Received unexpected QueryExecutionException while preparing statement", e);
    }
  }
Exemplo n.º 2
0
  private PreparedStatement toPreparedStatement(String query, Connection.Future future) {

    try {
      Message.Response response = Uninterruptibles.getUninterruptibly(future);
      switch (response.type) {
        case RESULT:
          ResultMessage rm = (ResultMessage) response;
          switch (rm.kind) {
            case PREPARED:
              ResultMessage.Prepared pmsg = (ResultMessage.Prepared) rm;
              PreparedStatement stmt =
                  PreparedStatement.fromMessage(
                      pmsg, manager.cluster.getMetadata(), query, manager.poolsState.keyspace);
              try {
                manager.cluster.manager.prepare(pmsg.statementId, stmt, future.getAddress());
              } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                // This method doesn't propagate interruption, at least not for now. However, if
                // we've
                // interrupted preparing queries on other node it's not a problem as we'll
                // re-prepare
                // later if need be. So just ignore.
              }
              return stmt;
            default:
              throw new DriverInternalError(
                  String.format(
                      "%s response received when prepared statement was expected", rm.kind));
          }
        case ERROR:
          ResultSetFuture.extractCause(
              ResultSetFuture.convertException(((ErrorMessage) response).error));
          break;
        default:
          throw new DriverInternalError(
              String.format(
                  "%s response received when prepared statement was expected", response.type));
      }
      throw new AssertionError();
    } catch (ExecutionException e) {
      ResultSetFuture.extractCauseFromExecutionException(e);
      throw new AssertionError();
    }
  }