Interface ReactiveSession

All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
DefaultBridgedReactiveSession, ObservableReactiveSession

public interface ReactiveSession extends Closeable
A session holds connections to a Cassandra cluster, allowing it to be queried. ReactiveSession executes queries and prepares statements in a reactive style returning results wrapped in Mono and Flux.

Each session maintains multiple connections to the cluster nodes, provides policies to choose which node to use for each query (round-robin on all nodes of the cluster by default), and handles retries for failed queries (when it makes sense).

Session instances are thread-safe and usually a single instance is enough per application. As a given session can only be "logged" into one keyspace at a time (where the "logged" keyspace is the one used by queries that don't explicitly use a fully qualified table name), it can make sense to create one session per keyspace used. This is however not necessary when querying multiple keyspaces since it is always possible to use a single session with fully qualified table names in queries.

Since:
2.0
Author:
Mark Paluch
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Initiates a shutdown of this session instance and blocks until that shutdown completes.
    reactor.core.publisher.Mono<ReactiveResultSet>
    execute(com.datastax.oss.driver.api.core.cql.Statement<?> statement)
    Executes the provided query.
    reactor.core.publisher.Mono<ReactiveResultSet>
    execute(String query)
    Executes the provided query.
    reactor.core.publisher.Mono<ReactiveResultSet>
    execute(String query, Object... values)
    Executes the provided query using the provided values.
    reactor.core.publisher.Mono<ReactiveResultSet>
    execute(String query, Map<String,Object> values)
    Executes the provided query using the provided named values.
    com.datastax.oss.driver.api.core.context.DriverContext
    Returns a context that provides access to all the policies used by this driver instance.
    Optional<com.datastax.oss.driver.api.core.CqlIdentifier>
    The keyspace that this session is currently connected to, or Optional.empty() if this session is not connected to any keyspace.
    com.datastax.oss.driver.api.core.metadata.Metadata
    Returns a snapshot of the Cassandra cluster's topology and schema metadata.
    boolean
    Whether this Session instance has been closed.
    reactor.core.publisher.Mono<com.datastax.oss.driver.api.core.cql.PreparedStatement>
    prepare(com.datastax.oss.driver.api.core.cql.SimpleStatement statement)
    Prepares the provided query.
    reactor.core.publisher.Mono<com.datastax.oss.driver.api.core.cql.PreparedStatement>
    prepare(String query)
    Prepares the provided query string.
  • Method Details

    • getMetadata

      com.datastax.oss.driver.api.core.metadata.Metadata getMetadata()
      Returns a snapshot of the Cassandra cluster's topology and schema metadata.

      In order to provide atomic updates, this method returns an immutable object: the node list, token map, and schema contained in a given instance will always be consistent with each other (but note that Node itself is not immutable: some of its properties will be updated dynamically, in particular Node.getState()).

      As a consequence of the above, you should call this method each time you need a fresh view of the metadata. Do not call it once and store the result, because it is a frozen snapshot that will become stale over time.

      If a metadata refresh triggers events (such as node added/removed, or schema events), then the new version of the metadata is guaranteed to be visible by the time you receive these events.

      Returns:
      never null, but may be empty if metadata has been disabled in the configuration.
      Since:
      3.2.2
    • getKeyspace

      Optional<com.datastax.oss.driver.api.core.CqlIdentifier> getKeyspace()
      The keyspace that this session is currently connected to, or Optional.empty() if this session is not connected to any keyspace.

      There are two ways that this can be set: before initializing the session (either with the session-keyspace option in the configuration, or with SessionBuilder.withKeyspace(CqlIdentifier)); or at runtime, if the client issues a request that changes the keyspace (such as a CQL USE query). Note that this second method is inherently unsafe, since other requests expecting the old keyspace might be executing concurrently. Therefore it is highly discouraged, aside from trivial cases (such as a cqlsh-style program where requests are never concurrent).

      Since:
      3.2.2
    • isClosed

      boolean isClosed()
      Whether this Session instance has been closed.

      Note that this method returns true as soon as the closing of this Session has started but it does not guarantee that the closing is done. If you want to guarantee that the closing is done, you can call close() and wait until it returns (or call the get method on closeAsync() with a very short timeout and check this doesn't timeout).

      Returns:
      true if this Session instance has been closed, false otherwise.
    • getContext

      com.datastax.oss.driver.api.core.context.DriverContext getContext()
      Returns a context that provides access to all the policies used by this driver instance.
      Returns:
      a context that provides access to all the policies used by this driver instance.
    • execute

      reactor.core.publisher.Mono<ReactiveResultSet> execute(String query)
      Executes the provided query.

      This is a convenience method for execute(new SimpleStatement(query)).

      Parameters:
      query - the CQL query to execute.
      Returns:
      the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
    • execute

      reactor.core.publisher.Mono<ReactiveResultSet> execute(String query, Object... values)
      Executes the provided query using the provided values.

      This is a convenience method for execute(new SimpleStatement(query, values)).

      Parameters:
      query - the CQL query to execute.
      values - values required for the execution of query. See SimpleStatement.newInstance(String, Object...) for more details.
      Returns:
      the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
    • execute

      reactor.core.publisher.Mono<ReactiveResultSet> execute(String query, Map<String,Object> values)
      Executes the provided query using the provided named values.

      This is a convenience method for execute(new SimpleStatement(query, values)).

      Parameters:
      query - the CQL query to execute.
      values - values required for the execution of query. See SimpleStatement.newInstance(String, Map) for more details.
      Returns:
      the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
    • execute

      reactor.core.publisher.Mono<ReactiveResultSet> execute(com.datastax.oss.driver.api.core.cql.Statement<?> statement)
      Executes the provided query.

      This method blocks until at least some result has been received from the database. However, for SELECT queries, it does not guarantee that the result has been received in full. But it does guarantee that some response has been received from the database, and in particular guarantees that if the request is invalid, an exception will be thrown by this method.

      Parameters:
      statement - the CQL query to execute (that can be any Statement).
      Returns:
      the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
    • prepare

      reactor.core.publisher.Mono<com.datastax.oss.driver.api.core.cql.PreparedStatement> prepare(String query)
      Prepares the provided query string.
      Parameters:
      query - the CQL query string to prepare
      Returns:
      the prepared statement corresponding to query.
    • prepare

      reactor.core.publisher.Mono<com.datastax.oss.driver.api.core.cql.PreparedStatement> prepare(com.datastax.oss.driver.api.core.cql.SimpleStatement statement)
      Prepares the provided query.

      This method behaves like prepare(String), but note that the resulting PreparedStatement will inherit the query properties set on statement. Concretely, this means that in the following code:

       Statement toPrepare = SimpleStatement.newInstance("SELECT * FROM test WHERE k=?")
                      .setConsistencyLevel(ConsistencyLevel.QUORUM);
       PreparedStatement prepared = session.prepare(toPrepare);
       session.execute(prepared.bind("someValue"));
       
      the final execution will be performed with Quorum consistency.

      Please note that if the same CQL statement is prepared more than once, all calls to this method will return the same PreparedStatement object but the method will still apply the properties of the prepared Statement to this object.

      Parameters:
      statement - the statement to prepare
      Returns:
      the prepared statement corresponding to statement.
      Throws:
      IllegalArgumentException - if statement.getValues() != null (values for executing a prepared statement should be provided after preparation though the PreparedStatement.bind(java.lang.Object...) method or through a corresponding BoundStatement).
    • close

      void close()
      Initiates a shutdown of this session instance and blocks until that shutdown completes.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable