Class AbstractSession
- java.lang.Object
-
- com.datastax.driver.core.AbstractSession
-
- All Implemented Interfaces:
Session,Closeable,AutoCloseable
public abstract class AbstractSession extends Object implements Session
Abstract implementation of the Session interface.This is primarly intended to make mocking easier.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.datastax.driver.core.Session
Session.State
-
-
Constructor Summary
Constructors Constructor Description AbstractSession()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidcheckNotInEventLoop()Checks that the current thread is not one of the Netty I/O threads used by the driver.voidclose()Initiates a shutdown of this session instance and blocks until that shutdown completes.ResultSetexecute(Statement statement)Executes the provided query.ResultSetexecute(String query)Executes the provided query.ResultSetexecute(String query, Object... values)Executes the provided query using the provided values.ResultSetexecute(String query, Map<String,Object> values)Executes the provided query using the provided named values.ResultSetFutureexecuteAsync(String query)Executes the provided query asynchronously.ResultSetFutureexecuteAsync(String query, Object... values)Executes the provided query asynchronously using the provided values.ResultSetFutureexecuteAsync(String query, Map<String,Object> values)Executes the provided query asynchronously using the provided values.PreparedStatementprepare(RegularStatement statement)Prepares the provided query.PreparedStatementprepare(String query)Prepares the provided query string.ListenableFuture<PreparedStatement>prepareAsync(RegularStatement statement)Prepares the provided query asynchronously.ListenableFuture<PreparedStatement>prepareAsync(String query)Prepares the provided query string asynchronously.protected abstract ListenableFuture<PreparedStatement>prepareAsync(String query, Map<String,ByteBuffer> customPayload)Prepares the provided query string asynchronously, sending along the provided custom payload, if any.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.datastax.driver.core.Session
closeAsync, executeAsync, getCluster, getLoggedKeyspace, getState, init, initAsync, isClosed
-
-
-
-
Method Detail
-
execute
public ResultSet execute(String query)
Executes the provided query.This is a convenience method for
execute(new SimpleStatement(query)).
-
execute
public ResultSet execute(String query, Object... values)
Executes the provided query using the provided values.This is a convenience method for
execute(new SimpleStatement(query, values)).- Specified by:
executein interfaceSession- Parameters:
query- the CQL query to execute.values- values required for the execution ofquery. SeeSimpleStatement(String, Object...)for more details.- Returns:
- the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
-
execute
public ResultSet execute(String query, Map<String,Object> values)
Executes the provided query using the provided named values.This is a convenience method for
execute(new SimpleStatement(query, values)).- Specified by:
executein interfaceSession- Parameters:
query- the CQL query to execute.values- values required for the execution ofquery. SeeSimpleStatement(String, Map)for more details.- Returns:
- the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
-
execute
public ResultSet execute(Statement statement)
Executes 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 guarantees that if the request is invalid, an exception will be thrown by this method.
-
executeAsync
public ResultSetFuture executeAsync(String query)
Executes the provided query asynchronously.This is a convenience method for
executeAsync(new SimpleStatement(query)).- Specified by:
executeAsyncin interfaceSession- Parameters:
query- the CQL query to execute.- Returns:
- a future on the result of the query.
-
executeAsync
public ResultSetFuture executeAsync(String query, Map<String,Object> values)
Executes the provided query asynchronously using the provided values.This is a convenience method for
executeAsync(new SimpleStatement(query, values)).- Specified by:
executeAsyncin interfaceSession- Parameters:
query- the CQL query to execute.values- values required for the execution ofquery. SeeSimpleStatement(String, Map)for more details.- Returns:
- a future on the result of the query.
-
executeAsync
public ResultSetFuture executeAsync(String query, Object... values)
Executes the provided query asynchronously using the provided values.This is a convenience method for
executeAsync(new SimpleStatement(query, values)).- Specified by:
executeAsyncin interfaceSession- Parameters:
query- the CQL query to execute.values- values required for the execution ofquery. SeeSimpleStatement(String, Object...)for more details.- Returns:
- a future on the result of the query.
-
prepare
public PreparedStatement prepare(String query)
Prepares the provided query string.
-
prepare
public PreparedStatement prepare(RegularStatement statement)
Prepares the provided query.This method behaves like
Session.prepare(String), but the resultingPreparedStatementwill inherit some of the properties set onstatement: routing key, consistency level, serial consistency level, tracing flag, retry policy, outgoing payload, and idempotence. 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
PreparedStatementobject but the method will still apply the properties of the preparedStatementto this object.
-
prepareAsync
public ListenableFuture<PreparedStatement> prepareAsync(String query)
Prepares the provided query string asynchronously.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.- Specified by:
prepareAsyncin interfaceSession- Parameters:
query- the CQL query string to prepare- Returns:
- a future on the prepared statement corresponding to
query.
-
prepareAsync
public ListenableFuture<PreparedStatement> prepareAsync(RegularStatement statement)
Prepares the provided query asynchronously. This method behaves likeSession.prepareAsync(String), but note that the resultingPreparedStatementwill inherit the query properties set onstatement. 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
PreparedStatementobject but the method will still apply the properties of the preparedStatementto this object.- Specified by:
prepareAsyncin interfaceSession- Parameters:
statement- the statement to prepare- Returns:
- a future on the prepared statement corresponding to
statement. - See Also:
Session.prepare(RegularStatement)
-
prepareAsync
protected abstract ListenableFuture<PreparedStatement> prepareAsync(String query, Map<String,ByteBuffer> customPayload)
Prepares the provided query string asynchronously, sending along the provided custom payload, if any.- Parameters:
query- the CQL query string to preparecustomPayload- the custom payload to send along the query, ornullif no payload is to be sent- Returns:
- a future on the prepared statement corresponding to
query.
-
close
public void close()
Initiates a shutdown of this session instance and blocks until that shutdown completes.This method is a shortcut for
closeAsync().get().Note that this method does not close the corresponding
Clusterinstance (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, useCluster.close(), but note that it will close all sessions created from that cluster.
-
checkNotInEventLoop
public void checkNotInEventLoop()
Checks that the current thread is not one of the Netty I/O threads used by the driver.This method is called from all the synchronous methods of this class to prevent deadlock issues.
User code extending this class can also call this method at any time to check if any code making blocking calls is being wrongly executed on a Netty I/O thread.
Note that the check performed by this method has a small overhead; if that is an issue, checks can be disabled by setting the System property
com.datastax.driver.CHECK_IO_DEADLOCKStofalse.- Throws:
IllegalStateException- if the current thread is one of the Netty I/O thread used by the driver.
-
-