public abstract class AbstractSession extends Object implements Session
Session.State
Constructor and Description |
---|
AbstractSession() |
Modifier and Type | Method and Description |
---|---|
void |
close()
Initiates a shutdown of this session instance and blocks until
that shutdown completes.
|
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(String query)
Executes the provided query asynchronously.
|
ResultSetFuture |
executeAsync(String query,
Object... values)
Executes the provided query asynchronously using the provided values.
|
SimpleStatement |
newSimpleStatement(String query)
Builds, but does not execute, a simple statement containing the provided query.
|
SimpleStatement |
newSimpleStatement(String query,
Object... values)
Builds, but does not execute, a simple statement containing the provided query with
the provided parameters.
|
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.
|
protected abstract com.google.common.util.concurrent.ListenableFuture<PreparedStatement> |
prepareAsync(String query,
Map<String,ByteBuffer> customPayload)
Prepares the provided query string asynchronously,
sending along the provided custom payload, if any.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
closeAsync, executeAsync, getCluster, getLoggedKeyspace, getState, init, isClosed
public ResultSet execute(String query)
execute(new SimpleStatement(query))
.public ResultSet execute(String query, Object... values)
execute(new SimpleStatement(query, values))
.execute
in interface Session
query
- the CQL query to execute.values
- values required for the execution of query
. See
Session.newSimpleStatement(String, Object...)
for more detail.public ResultSet execute(Statement statement)
public ResultSetFuture executeAsync(String query)
This is a convenience method for executeAsync(new SimpleStatement(query))
.
executeAsync
in interface Session
query
- the CQL query to execute.public ResultSetFuture executeAsync(String query, Object... values)
executeAsync(new SimpleStatement(query, values))
.executeAsync
in interface Session
query
- the CQL query to execute.values
- values required for the execution of query
. See
Session.newSimpleStatement(String, Object...)
for more detail.public SimpleStatement newSimpleStatement(String query)
Session.execute(Statement)
.newSimpleStatement
in interface Session
query
- the CQL query to execute.public SimpleStatement newSimpleStatement(String query, Object... values)
Session.execute(Statement)
.
Parameterized simple statements are useful when you want to execute a query only once (and thus do not want to resort to prepared statements), but do not want to convert all column values to string (typically, if you have blob values, encoding them to a hexadecimal string is not very efficient). In that case, you can provide a query string with bind marker to this method, along with the values for those bind variables. When executed, the server will prepare the provided query, bind the provided values to that prepare statement and execute the resulting statement. Thus,
session.execute(session.newSimpleStatement(query, value1, value2, value3));is functionally equivalent to
PreparedStatement ps = session.prepare(query); session.execute(ps.bind(value1, value2, value3));except that the former version:
Note that the type of the values
provided to this method will
not be validated by the driver as is done by BoundStatement.bind(java.lang.Object...)
since
query
is not parsed (and hence the driver cannot know what those value
should be). The codec to serialize each value will be chosen in the codec registry
associated with this session's cluster, based on the value's Java type
(this is the equivalent to calling CodecRegistry.codecFor(Object)
).
If too many or too few values are provided, or if a value is not a valid one for
the variable it is bound to, an
InvalidQueryException
will be thrown
by Cassandra at execution time. A CodecNotFoundException
may be
thrown by this constructor however, if the codec registry does not know how to
handle one of the values.
newSimpleStatement
in interface Session
query
- the CQL query to execute.values
- values required for the execution of query
.public PreparedStatement prepare(String query)
public PreparedStatement prepare(RegularStatement statement)
This method behaves like Session.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.
public com.google.common.util.concurrent.ListenableFuture<PreparedStatement> prepareAsync(String query)
This method is equivalent to Session.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.
prepareAsync
in interface Session
query
- the CQL query string to preparequery
.public com.google.common.util.concurrent.ListenableFuture<PreparedStatement> prepareAsync(RegularStatement statement)
Session.prepareAsync(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.
prepareAsync
in interface Session
statement
- the statement to preparestatement
.Session.prepare(RegularStatement)
protected abstract com.google.common.util.concurrent.ListenableFuture<PreparedStatement> prepareAsync(String query, Map<String,ByteBuffer> customPayload)
query
- the CQL query string to preparecustomPayload
- the custom payload to send along the query, or null
if no payload is to be sentquery
.public void close()
This method is a shortcut for 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.
Copyright © 2012–2015. All rights reserved.