public interface Session
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.
Modifier and Type | Method and Description |
---|---|
ResultSet |
execute(Query query)
Executes the provided query.
|
ResultSet |
execute(String query)
Executes the provided query.
|
ResultSetFuture |
executeAsync(Query query)
Executes the provided query asynchronously.
|
ResultSetFuture |
executeAsync(String query)
Executes the provided query asynchronously.
|
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.
|
boolean |
isShutdown()
Whether shutdown has been called on this Session instance.
|
PreparedStatement |
prepare(Statement statement)
Prepares the provided query.
|
PreparedStatement |
prepare(String query)
Prepares the provided query string.
|
com.google.common.util.concurrent.ListenableFuture<PreparedStatement> |
prepareAsync(Statement statement)
Prepares the provided query asynchronously.
|
com.google.common.util.concurrent.ListenableFuture<PreparedStatement> |
prepareAsync(String query)
Prepares the provided query string asynchronously.
|
void |
shutdown()
Shuts down this session instance.
|
boolean |
shutdown(long timeout,
TimeUnit unit)
Shutdown this session instance, only waiting a definite amount of time.
|
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(Query query)
query
- the CQL query to execute (that can be either a Statement
or a BoundStatement
). If it is a BoundStatement
, all variables must have been bound (the statement must
be ready).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).IllegalStateException
- if query
is a BoundStatement
but !query.isReady()
.ResultSetFuture executeAsync(String query)
This is a convenience method for executeAsync(new SimpleStatement(query))
.
query
- the CQL query to execute.ResultSetFuture executeAsync(Query query)
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.
query
- the CQL query to execute (that can be either a Statement
or a BoundStatement
). If it is a BoundStatement
, all variables must have been bound (the statement must
be ready).IllegalStateException
- if query
is a BoundStatement
but !query.isReady()
.PreparedStatement prepare(String query)
query
- the CQL query string to preparequery
.NoHostAvailableException
- if no host in the cluster can be
contacted successfully to prepare this query.PreparedStatement prepare(Statement 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:
Statement 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
.NoHostAvailableException
- if no host in the cluster can be
contacted successfully to prepare this statement.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 preparequery
.com.google.common.util.concurrent.ListenableFuture<PreparedStatement> prepareAsync(Statement 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
.
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
.prepare(Statement)
void shutdown()
This closes all connections used by this sessions. Note that if you want
to shut down the full Cluster
instance this session is part of,
you should use Cluster.shutdown()
instead (which will call this
method for all session but also release some additional resources).
This method has no effect if the session was already shutdown.
boolean shutdown(long timeout, TimeUnit unit)
This closes all connections used by this sessions. Note that if you want
to shutdown the full Cluster
instance this session is part of,
you should use Cluster.shutdown()
instead (which will call this
method for all session but also release some additional resources).
Note that this method is not thread safe in the sense that if another
shutdown is perform in parallel, it might return true
even if
the instance is not yet fully shutdown.
timeout
- how long to wait for the session to shutdown.unit
- the unit for the timeout.true
if the session has been properly shutdown within
the timeout
, false
otherwise.boolean isShutdown()
true
if this instance is shut down, false
otherwise.Cluster getCluster()
Cluster
object this session is part of.Cluster
object this session is part of.Copyright © 2014. All Rights Reserved.