com.datastax.driver.core
Class Session

java.lang.Object
  extended by com.datastax.driver.core.Session

public class Session
extends Object

A session holds connections to a Cassandra cluster, allowing to query it. Each session will maintain multiple connections to the cluster nodes, and provides policies to choose which node to use for each query (round-robin on all nodes of the cluster by default), 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
 ResultSet execute(Query query)
          Execute the provided query.
 ResultSet execute(String query)
          Execute the provided query.
 ResultSetFuture executeAsync(Query query)
          Execute the provided query asynchronously.
 ResultSetFuture executeAsync(String query)
          Execute the provided query asynchronously.
 Cluster getCluster()
          The Cluster object this session is part of.
 PreparedStatement prepare(String query)
          Prepare the provided query.
 void shutdown()
          Shutdown this session instance.
 boolean shutdown(long timeout, TimeUnit unit)
          Shutdown this session instance, only waiting a definite amount of time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

execute

public ResultSet execute(String query)
Execute the provided query. This method is a shortcut 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).
Throws:
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).

execute

public ResultSet execute(Query query)
Execute 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 guarantee that if the request is invalid, an exception will be thrown by this method.

Parameters:
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).
Returns:
the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
Throws:
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().

executeAsync

public ResultSetFuture executeAsync(String query)
Execute the provided query asynchronously. This method is a shortcut for executeAsync(new SimpleStatement(query)).

Parameters:
query - the CQL query to execute.
Returns:
a future on the result of the query.

executeAsync

public ResultSetFuture executeAsync(Query query)
Execute the provided query asynchronously. This method does not block. It returns as soon as the query has been passed to the underlying network stack. In particular, returning from this method does not guarantee that the query is valid or have even been submitted to a live node. Any exception pertaining to the failure of the query will be thrown when accessing the ResultSetFuture. Note that for queries that doesn't return a result (INSERT, UPDATE and DELETE), you will need to access the ResultSetFuture (i.e. call one of its get method to make sure the query was successful.

Parameters:
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).
Returns:
a future on the result of the query.
Throws:
IllegalStateException - if query is a BoundStatement but !query.isReady().

prepare

public PreparedStatement prepare(String query)
Prepare the provided query.

Parameters:
query - the CQL query to prepare
Returns:
the prepared statement corresponding to query.
Throws:
NoHostAvailableException - if no host in the cluster can be contacted successfully to execute this query.

shutdown

public void shutdown()
Shutdown this session instance.

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).

This method has no effect if the session was already shutdown.


shutdown

public boolean shutdown(long timeout,
                        TimeUnit unit)
Shutdown this session instance, only waiting a definite amount of time.

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.

Parameters:
timeout - how long to wait for the session to shutdown.
unit - the unit for the timeout.
Returns:
true if the session has been properly shutdown within the timeout, false otherwise.

getCluster

public Cluster getCluster()
The Cluster object this session is part of.

Returns:
the Cluster object this session is part of.


Copyright © 2013. All Rights Reserved.