public interface ReactiveSession extends Closeable
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.Publisher
,
Mono
,
ReactiveResultSet
Modifier and Type | Method and Description |
---|---|
void |
close()
Initiates a shutdown of this session instance and blocks until that shutdown completes.
|
reactor.core.publisher.Mono<ReactiveResultSet> |
execute(com.datastax.driver.core.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,
Map<String,Object> values)
Executes the provided query using the provided named values.
|
reactor.core.publisher.Mono<ReactiveResultSet> |
execute(String query,
Object... values)
Executes the provided query using the provided values.
|
com.datastax.driver.core.Cluster |
getCluster()
Returns the
Cluster object this session is part of. |
boolean |
isClosed()
Whether this Session instance has been closed.
|
reactor.core.publisher.Mono<com.datastax.driver.core.PreparedStatement> |
prepare(com.datastax.driver.core.RegularStatement statement)
Prepares the provided query.
|
reactor.core.publisher.Mono<com.datastax.driver.core.PreparedStatement> |
prepare(String query)
Prepares the provided query string.
|
boolean isClosed()
close()
and wait
until it returns (or call the get method on closeAsync()
with a very short timeout and check this doesn't
timeout).true
if this Session instance has been closed, false
otherwise.com.datastax.driver.core.Cluster getCluster()
Cluster
object this session is part of.Cluster
object this session is part of.reactor.core.publisher.Mono<ReactiveResultSet> execute(String query)
execute(new SimpleStatement(query))
.query
- the CQL query to execute.com.datastax.driver.core.exceptions.NoHostAvailableException
- if no host in the cluster can be contacted successfully to execute this query.com.datastax.driver.core.exceptions.QueryExecutionException
- if the query triggered an execution exception, i.e. an exception thrown by
Cassandra when it cannot execute the query with the requested consistency level successfully.com.datastax.driver.core.exceptions.QueryValidationException
- if the query if invalid (syntax error, unauthorized or any other validation
problem).reactor.core.publisher.Mono<ReactiveResultSet> execute(String query, Object... values)
execute(new SimpleStatement(query, values))
.query
- the CQL query to execute.values
- values required for the execution of query
. See
SimpleStatement#SimpleStatement(String, Object...)
for more details.com.datastax.driver.core.exceptions.NoHostAvailableException
- if no host in the cluster can be contacted successfully to execute this query.com.datastax.driver.core.exceptions.QueryExecutionException
- if the query triggered an execution exception, i.e. an exception thrown by
Cassandra when it cannot execute the query with the requested consistency level successfully.com.datastax.driver.core.exceptions.QueryValidationException
- if the query if invalid (syntax error, unauthorized or any other validation
problem).com.datastax.driver.core.exceptions.UnsupportedFeatureException
- if version 1 of the protocol is in use (i.e. if you've forced version 1 through
Cluster.Builder#withProtocolVersion
or you use Cassandra 1.2).reactor.core.publisher.Mono<ReactiveResultSet> execute(String query, Map<String,Object> values)
execute(new SimpleStatement(query, values))
.query
- the CQL query to execute.values
- values required for the execution of query
. See
SimpleStatement#SimpleStatement(String, Map)
for more details.com.datastax.driver.core.exceptions.NoHostAvailableException
- if no host in the cluster can be contacted successfully to execute this query.com.datastax.driver.core.exceptions.QueryExecutionException
- if the query triggered an execution exception, i.e. an exception thrown by
Cassandra when it cannot execute the query with the requested consistency level successfully.com.datastax.driver.core.exceptions.QueryValidationException
- if the query if invalid (syntax error, unauthorized or any other validation
problem).com.datastax.driver.core.exceptions.UnsupportedFeatureException
- if version 1 or 2 of the protocol is in use (i.e. if you've forced it through
Cluster.Builder#withProtocolVersion
or you use Cassandra 1.2 or 2.0).reactor.core.publisher.Mono<ReactiveResultSet> execute(com.datastax.driver.core.Statement statement)
statement
- the CQL query to execute (that can be any Statement
).com.datastax.driver.core.exceptions.NoHostAvailableException
- if no host in the cluster can be contacted successfully to execute this query.com.datastax.driver.core.exceptions.QueryExecutionException
- if the query triggered an execution exception, i.e. an exception thrown by
Cassandra when it cannot execute the query with the requested consistency level successfully.com.datastax.driver.core.exceptions.QueryValidationException
- if the query if invalid (syntax error, unauthorized or any other validation
problem).com.datastax.driver.core.exceptions.UnsupportedFeatureException
- if the protocol version 1 is in use and a feature not supported has been used.
Features that are not supported by the version protocol 1 include: BatchStatement, ReactiveResultSet
paging and binary values in RegularStatement.reactor.core.publisher.Mono<com.datastax.driver.core.PreparedStatement> prepare(String query)
query
- the CQL query string to preparequery
.com.datastax.driver.core.exceptions.NoHostAvailableException
- if no host in the cluster can be contacted successfully to prepare this query.reactor.core.publisher.Mono<com.datastax.driver.core.PreparedStatement> prepare(com.datastax.driver.core.RegularStatement statement)
prepare(String)
, but note that the resulting PreparedStatement
will
inherit the query properties set on statement
. Concretely, this means that in the following code:
RegularStatement toPrepare = new SimpleStatement("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.statement
- the statement to preparestatement
.com.datastax.driver.core.exceptions.NoHostAvailableException
- if no host in the cluster can be contacted successfully to prepare this statement.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
).void close()
closeAsync().get()
.
Note that this method does not close the corresponding Cluster
instance (which holds additional resources,
in particular internal executors that must be shut down in order for the client program to terminate). If you want
to do so, use Cluster.close()
, but note that it will close all sessions created from that cluster.close
in interface AutoCloseable
close
in interface Closeable
Copyright © 2011–2019 Pivotal Software, Inc.. All rights reserved.