Interface ClientSession

All Superinterfaces:
AutoCloseable, com.mongodb.session.ClientSession, Closeable

public interface ClientSession extends com.mongodb.session.ClientSession
A client session that supports transactions.
Since:
3.8
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Abort a transaction in the context of this session.
    void
    Commit a transaction in the context of this session.
    com.mongodb.ServerAddress
    Returns the server address of the pinned mongos on this session.
    com.mongodb.TransactionOptions
    Gets the transaction options.
    com.mongodb.internal.observability.micrometer.TransactionSpan
    Get the transaction span (if started).
    boolean
    Returns true if there is an active transaction on this session, and false otherwise
    boolean
    Notify the client session that a message has been sent.
    void
    Notify the client session that command execution is being initiated.
    void
    Start a transaction in the context of this session with default transaction options.
    void
    startTransaction(com.mongodb.TransactionOptions transactionOptions)
    Start a transaction in the context of this session with the given transaction options.
    <T> T
    withTransaction(TransactionBody<T> transactionBody)
    Execute the given function within a transaction.
    <T> T
    withTransaction(TransactionBody<T> transactionBody, com.mongodb.TransactionOptions options)
    Execute the given function within a transaction.

    Methods inherited from interface com.mongodb.session.ClientSession

    advanceClusterTime, advanceOperationTime, clearTransactionContext, close, getClusterTime, getOperationTime, getOptions, getOriginator, getRecoveryToken, getServerSession, getSnapshotTimestamp, getTimeoutContext, getTransactionContext, isCausallyConsistent, setRecoveryToken, setSnapshotTimestamp, setTransactionContext
  • Method Details

    • getPinnedServerAddress

      @Nullable com.mongodb.ServerAddress getPinnedServerAddress()
      Returns the server address of the pinned mongos on this session.
      Specified by:
      getPinnedServerAddress in interface com.mongodb.session.ClientSession
      Returns:
      the server address of the pinned mongos.
      Since:
      3.11
      Since server release
      4.2
    • hasActiveTransaction

      boolean hasActiveTransaction()
      Returns true if there is an active transaction on this session, and false otherwise
      Returns:
      true if there is an active transaction on this session
      Since server release
      4.0
    • notifyMessageSent

      boolean notifyMessageSent()
      Notify the client session that a message has been sent.

      For internal use only

      Returns:
      true if this is the first message sent, false otherwise
    • notifyOperationInitiated

      void notifyOperationInitiated(Object operation)
      Notify the client session that command execution is being initiated. This should be called before server selection occurs.

      For internal use only

      Parameters:
      operation - the operation
    • getTransactionOptions

      com.mongodb.TransactionOptions getTransactionOptions()
      Gets the transaction options. Only call this method of the session has an active transaction
      Returns:
      the transaction options
    • startTransaction

      void startTransaction()
      Start a transaction in the context of this session with default transaction options. A transaction can not be started if there is already an active transaction on this session.
      Since server release
      4.0
    • startTransaction

      void startTransaction(com.mongodb.TransactionOptions transactionOptions)
      Start a transaction in the context of this session with the given transaction options. A transaction can not be started if there is already an active transaction on this session.
      Parameters:
      transactionOptions - the options to apply to the transaction
      Since server release
      4.0
    • commitTransaction

      void commitTransaction()
      Commit a transaction in the context of this session. A transaction can only be commmited if one has first been started.
      Since server release
      4.0
    • abortTransaction

      void abortTransaction()
      Abort a transaction in the context of this session. A transaction can only be aborted if one has first been started.
      Since server release
      4.0
    • withTransaction

      <T> T withTransaction(TransactionBody<T> transactionBody)
      Execute the given function within a transaction.
      Type Parameters:
      T - the return type of the transaction body
      Parameters:
      transactionBody - the body of the transaction
      Returns:
      the return value of the transaction body
      Since:
      3.11
      Since server release
      4.0
    • withTransaction

      <T> T withTransaction(TransactionBody<T> transactionBody, com.mongodb.TransactionOptions options)
      Execute the given function within a transaction.
      Type Parameters:
      T - the return type of the transaction body
      Parameters:
      transactionBody - the body of the transaction
      options - the transaction options
      Returns:
      the return value of the transaction body
      Since:
      3.11
      Since server release
      4.0
    • getTransactionSpan

      @Nullable com.mongodb.internal.observability.micrometer.TransactionSpan getTransactionSpan()
      Get the transaction span (if started).
      Returns:
      the transaction span
      Since:
      5.7