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

    • Constructor Detail

      • AbstractSession

        public AbstractSession()
    • Method Detail

      • execute

        public ResultSet execute​(String query)
        Executes the provided query.

        This is a convenience method for execute(new SimpleStatement(query)).

        Specified by:
        execute in interface Session
        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).
      • 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:
        execute in interface Session
        Parameters:
        query - the CQL query to execute.
        values - values required for the execution of query. See SimpleStatement(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:
        execute in interface Session
        Parameters:
        query - the CQL query to execute.
        values - values required for the execution of query. See SimpleStatement(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.

        Specified by:
        execute in interface Session
        Parameters:
        statement - the CQL query to execute (that can be any Statement).
        Returns:
        the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
      • executeAsync

        public ResultSetFuture executeAsync​(String query)
        Executes the provided query asynchronously.

        This is a convenience method for executeAsync(new SimpleStatement(query)).

        Specified by:
        executeAsync in interface Session
        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:
        executeAsync in interface Session
        Parameters:
        query - the CQL query to execute.
        values - values required for the execution of query. See SimpleStatement(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:
        executeAsync in interface Session
        Parameters:
        query - the CQL query to execute.
        values - values required for the execution of query. See SimpleStatement(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.
        Specified by:
        prepare in interface Session
        Parameters:
        query - the CQL query string to prepare
        Returns:
        the prepared statement corresponding to query.
      • prepare

        public PreparedStatement prepare​(RegularStatement statement)
        Prepares the provided query.

        This method behaves like Session.prepare(String), but the resulting PreparedStatement will inherit some of the properties set on statement: 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 PreparedStatement object but the method will still apply the properties of the prepared Statement to this object.

        Specified by:
        prepare in interface Session
        Parameters:
        statement - the statement to prepare
        Returns:
        the prepared statement corresponding to statement.
      • 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:
        prepareAsync in interface Session
        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 like 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.

        Specified by:
        prepareAsync in interface Session
        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 prepare
        customPayload - the custom payload to send along the query, or null if 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 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.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Specified by:
        close in interface Session
      • 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_DEADLOCKS to false.

        Throws:
        IllegalStateException - if the current thread is one of the Netty I/O thread used by the driver.