Session is the main object used in the driver. It manages all connections to the cluster and allows executing CQL requests.
Overview
TheSession type provides methods for:
- Executing unprepared queries
- Preparing and executing prepared statements
- Batch operations
- Schema management
- Cluster metadata access
Creating a Session
UseSessionBuilder to create a new session:
Session::connect() with a SessionConfig:
Unprepared Queries
query_unpaged
Executes an unprepared CQL statement without paging.Parameters:
statement: impl Into<Statement>- The CQL statement to executevalues: impl SerializeRow- Bound values for the statement
Result<QueryResult, ExecutionError>Example:query_single_page
Queries a single page from the database, optionally continuing from a saved point.Parameters:
statement: impl Into<Statement>- The CQL statementvalues: impl SerializeRow- Bound valuespaging_state: PagingState- Previously received paging state orPagingState::start()
Result<(QueryResult, PagingStateResponse), ExecutionError>Example:query_iter
Executes an unprepared CQL statement with automatic paging, returning a stream over all results.Parameters:
statement: impl Into<Statement>- The CQL statementvalues: impl SerializeRow- Bound values
Result<QueryPager, PagerExecutionError>Example:Prepared Statements
prepare
Prepares a statement on all nodes in the cluster.Prepared statements are much faster than unprepared statements:
- Database doesn’t need to parse the statement upon each execution
- They enable proper load balancing using token-aware routing
statement: impl Into<Statement>- Statement to prepare
Result<PreparedStatement, PrepareError>Example:execute_unpaged
Executes a prepared statement without paging.Parameters:
prepared: &PreparedStatement- The prepared statementvalues: impl SerializeRow- Bound values
Result<QueryResult, ExecutionError>Example:execute_single_page
Executes a prepared statement, restricting results to a single page.Parameters:
prepared: &PreparedStatement- The prepared statementvalues: impl SerializeRow- Bound valuespaging_state: PagingState- Paging continuation orPagingState::start()
Result<(QueryResult, PagingStateResponse), ExecutionError>Example:execute_iter
Executes a prepared statement with automatic paging, returning a stream over all results.Parameters:
prepared: impl Into<PreparedStatement>- The prepared statementvalues: impl SerializeRow- Bound values
Result<QueryPager, PagerExecutionError>Example:Batch Operations
batch
Executes a batch statement containing multiple queries.Parameters:
batch: &Batch- The batch to executevalues: impl BatchValues- Values for each statement
Result<QueryResult, ExecutionError>Example:prepare_batch
Prepares all statements within a batch and returns a new batch where every statement is prepared.Parameters:
batch: &Batch- The batch with unprepared statements
Result<Batch, PrepareError>Example:Schema Management
use_keyspace
Sends
USE <keyspace_name> on all connections, allowing queries without fully-qualified table names.Parameters:keyspace_name: impl Into<String>- Keyspace name (up to 48 alphanumeric characters and underscores)case_sensitive: bool- If true, puts keyspace name in quotes
Result<(), UseKeyspaceError>Example:get_keyspace
Gets the name of the currently set keyspace, or
None if no keyspace was set.Returns: Option<Arc<String>>Example:await_schema_agreement
Waits until all nodes in the cluster agree on the same schema version.Returns:
Result<Uuid, SchemaAgreementError> - The agreed upon schema versionExample:check_schema_agreement
Checks if all reachable nodes have the same schema version without waiting.Returns:
Result<Option<Uuid>, SchemaAgreementError> - Some(version) if in agreement, None if notExample:refresh_metadata
Manually triggers a metadata refresh, fetching current nodes in the cluster.Normally this is not needed - the driver automatically detects metadata changes.Returns:
Result<(), MetadataError>Example:Cluster Information
get_cluster_state
Access cluster state visible by the driver, including network topology and schema information.Returns:
Arc<ClusterState>Example:get_metrics
Access metrics collected by the driver (requires
metrics feature).Returns: Arc<Metrics>Example:get_default_execution_profile_handle
Retrieves the handle to the execution profile used by default.Returns:
&ExecutionProfileHandleExample:Tracing
get_tracing_info
Retrieves tracing information for a traced query performed earlier.The driver will make multiple attempts to fetch tracing info, as it might not be available immediately.Parameters:
tracing_id: &Uuid- Tracing ID from a traced query result
Result<TracingInfo, TracingError>Example:Related Types
SessionBuilder- Builder for creating sessionsCachingSession- Wrapper that automatically caches prepared statementsPreparedStatement- Prepared statement for efficient executionQueryResult- Result from query executionBatch- Container for batch operations
