Class MongoDatabaseUtils

java.lang.Object
org.springframework.data.mongodb.MongoDatabaseUtils

public class MongoDatabaseUtils extends Object
Helper class for managing a MongoDatabase instances via MongoDatabaseFactory. Used for obtaining session bound resources, such as MongoDatabase and MongoCollection suitable for transactional usage.
Note: Intended for internal usage only.
Since:
2.1
Author:
Christoph Strobl, Mark Paluch
  • Constructor Details

    • MongoDatabaseUtils

      public MongoDatabaseUtils()
  • Method Details

    • getDatabase

      public static com.mongodb.client.MongoDatabase getDatabase(MongoDatabaseFactory factory)
      Obtain the default database form the given factory using native session synchronization.
      Registers a MongoDB specific transaction synchronization within the current Thread if synchronization is active.
      Parameters:
      factory - the MongoDatabaseFactory to get the MongoDatabase from.
      Returns:
      the MongoDatabase that is potentially associated with a transactional ClientSession.
    • getDatabase

      public static com.mongodb.client.MongoDatabase getDatabase(MongoDatabaseFactory factory, SessionSynchronization sessionSynchronization)
      Obtain the default database form the given factory.
      Registers a MongoDB specific transaction synchronization within the current Thread if synchronization is active.
      Parameters:
      factory - the MongoDatabaseFactory to get the MongoDatabase from.
      sessionSynchronization - the synchronization to use. Must not be null.
      Returns:
      the MongoDatabase that is potentially associated with a transactional ClientSession.
    • getDatabase

      public static com.mongodb.client.MongoDatabase getDatabase(@Nullable String dbName, MongoDatabaseFactory factory)
      Obtain the database with given name form the given factory using native session synchronization.
      Registers a MongoDB specific transaction synchronization within the current Thread if synchronization is active.
      Parameters:
      dbName - the name of the MongoDatabase to get.
      factory - the MongoDatabaseFactory to get the MongoDatabase from.
      Returns:
      the MongoDatabase that is potentially associated with a transactional ClientSession.
    • getDatabase

      public static com.mongodb.client.MongoDatabase getDatabase(@Nullable String dbName, MongoDatabaseFactory factory, SessionSynchronization sessionSynchronization)
      Obtain the database with given name form the given factory.
      Registers a MongoDB specific transaction synchronization within the current Thread if synchronization is active.
      Parameters:
      dbName - the name of the MongoDatabase to get.
      factory - the MongoDatabaseFactory to get the MongoDatabase from.
      sessionSynchronization - the synchronization to use. Must not be null.
      Returns:
      the MongoDatabase that is potentially associated with a transactional ClientSession.
    • isTransactionActive

      public static boolean isTransactionActive(MongoDatabaseFactory dbFactory)
      Check if the MongoDatabaseFactory is actually bound to a ClientSession that has an active transaction, or if a TransactionSynchronization has been registered for the resource and if the associated ClientSession has an active transaction.
      Parameters:
      dbFactory - the resource to check transactions for. Must not be null.
      Returns:
      true if the factory has an ongoing transaction.
      Since:
      2.1.3