|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Session
A session holds connections to a Cassandra cluster, allowing it to be queried. 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 query (when it makes sense), etc...
Session instances are thread-safe and usually a single instance is enough per application. However, a given session can only be set to one keyspace at a time, so one instance per keyspace is necessary.
Method Summary | |
---|---|
void |
close()
Initiates a shutdown of this session instance and blocks until that shutdown completes. |
CloseFuture |
closeAsync()
Initiates a shutdown of this session instance. |
ResultSet |
execute(Statement statement)
Executes the provided query. |
ResultSet |
execute(String query)
Executes the provided query. |
ResultSet |
execute(String query,
Object... values)
Executes the provided query using the provided value. |
ResultSetFuture |
executeAsync(Statement statement)
Executes the provided query asynchronously. |
ResultSetFuture |
executeAsync(String query)
Executes the provided query asynchronously. |
ResultSetFuture |
executeAsync(String query,
Object... values)
Executes the provided query asynchronously using the provided values. |
Cluster |
getCluster()
Returns the Cluster object this session is part of. |
String |
getLoggedKeyspace()
The keyspace to which this Session is currently logged in, if any. |
PreparedStatement |
prepare(RegularStatement statement)
Prepares the provided query. |
PreparedStatement |
prepare(String query)
Prepares the provided query string. |
com.google.common.util.concurrent.ListenableFuture<PreparedStatement> |
prepareAsync(RegularStatement statement)
Prepares the provided query asynchronously. |
com.google.common.util.concurrent.ListenableFuture<PreparedStatement> |
prepareAsync(String query)
Prepares the provided query string asynchronously. |
Method Detail |
---|
String getLoggedKeyspace()
This correspond to the name passed to Cluster.connect(String)
, or to the
last keyspace logged into through a "USE" CQL query if one was used.
null
if the session is logged to no keyspace.ResultSet execute(String query)
execute(new SimpleStatement(query))
.
query
- the CQL query to execute.
NoHostAvailableException
- if no host in the cluster can be
contacted successfully to execute this query.
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.
QueryValidationException
- if the query if invalid (syntax error,
unauthorized or any other validation problem).ResultSet 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 detail.
NoHostAvailableException
- if no host in the cluster can be
contacted successfully to execute this query.
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.
QueryValidationException
- if the query if invalid (syntax error,
unauthorized or any other validation problem).
UnsupportedFeatureException
- if version 1 of the protocol
is in use (i.e. if you've force version 1 through Cluster.Builder.withProtocolVersion(int)
or you use Cassandra 1.2).ResultSet execute(Statement statement)
statement
- the CQL query to execute (that can be any Statement
).
NoHostAvailableException
- if no host in the cluster can be
contacted successfully to execute this query.
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.
QueryValidationException
- if the query if invalid (syntax error,
unauthorized or any other validation problem).
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, ResultSet paging and binary
values in RegularStatement.ResultSetFuture executeAsync(String query)
This is a convenience method for executeAsync(new SimpleStatement(query))
.
query
- the CQL query to execute.
ResultSetFuture executeAsync(String query, Object... values)
executeAsync(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 detail.
UnsupportedFeatureException
- if version 1 of the protocol
is in use (i.e. if you've force version 1 through Cluster.Builder.withProtocolVersion(int)
or you use Cassandra 1.2).ResultSetFuture executeAsync(Statement statement)
ResultSetFuture
.
Note that for queries that doesn't return a result (INSERT, UPDATE and DELETE), you will need to access the ResultSetFuture (that is call one of its get method to make sure the query was successful.
statement
- the CQL query to execute (that can be either any Statement
.
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, ResultSet paging and binary
values in RegularStatement.PreparedStatement prepare(String query)
query
- the CQL query string to prepare
query
.
NoHostAvailableException
- if no host in the cluster can be
contacted successfully to prepare this query.PreparedStatement prepare(RegularStatement statement)
This method is essentially a shortcut for prepare(statement.getQueryString())
,
but note that the resulting PreparedStamenent
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.
statement
- the statement to prepare
statement
.
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
).com.google.common.util.concurrent.ListenableFuture<PreparedStatement> prepareAsync(String query)
This method is equilavent to prepare(String)
except that it
does not block but return a future instead. Any error during preparation will
be thrown when accessing the future, not by this method itself.
query
- the CQL query string to prepare
query
.com.google.common.util.concurrent.ListenableFuture<PreparedStatement> prepareAsync(RegularStatement statement)
This method is essentially a shortcut for prepareAsync(statement.getQueryString())
,
but with the additional effect that the resulting PreparedStamenent
will inherit the query properties set on statement
.
statement
- the statement to prepare
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
).Session#prepare(Statement)
CloseFuture closeAsync()
This method is asynchronous and return a future on the completion of the shutdown process. As soon a the session is shutdown, no new request will be accepted, but already submitted queries are allowed to complete. This method closes all connections of this session and reclaims all resources used by it.
If for some reason you wish to expedite this process, the
CloseFuture.force()
can be called on the result future.
This method has no particular effect if the session was already closed (in which case the returned future will return immediately).
Note that if you want to close the full Cluster
instance
this session is part of, you should use Cluster.close()
instead
(which will call this method for all sessions but also release some
additional resources).
void close()
This method is a shortcut for closeAsync().get()
.
close
in interface Closeable
Cluster getCluster()
Cluster
object this session is part of.
Cluster
object this session is part of.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |