Class TransactionMongoManager

java.lang.Object
io.github.marcopaglio.booking.transaction.manager.TransactionManager
io.github.marcopaglio.booking.transaction.manager.mongo.TransactionMongoManager

public class TransactionMongoManager extends TransactionManager
An implementation of TransactionManager for managing code executed on MongoDB within transactions.
  • Field Details

    • TXN_OPTIONS

      public static final com.mongodb.TransactionOptions TXN_OPTIONS
      Options used to configure transactions. Note: casually consistency is applied when both read and write concerns has value 'majority'.
  • Constructor Details

    • TransactionMongoManager

      public TransactionMongoManager(com.mongodb.client.MongoClient mongoClient, String databaseName, TransactionHandlerFactory transactionHandlerFactory, ClientRepositoryFactory clientRepositoryFactory, ReservationRepositoryFactory reservationRepositoryFactory)
      Constructs a manager for applying code that uses entity repositories using MongoDB transactions.
      Parameters:
      mongoClient - the client connected to the MongoDB database.
      databaseName - the name of the MongoDB database.
      transactionHandlerFactory - the factory to create ClientSession instances.
      clientRepositoryFactory - the factory to create ClientMongoRepository instances.
      reservationRepositoryFactory - the factory to create ReservationMongoRepository instances.
  • Method Details

    • doInTransaction

      public <R> R doInTransaction(ClientTransactionCode<R> code) throws TransactionException
      Prepares to execution of code that involves the ClientRepository's method(s) on MongoDB in a single transaction.
      Specified by:
      doInTransaction in class TransactionManager
      Type Parameters:
      R - the returned type of executed code.
      Parameters:
      code - the code to execute.
      Returns:
      something depending on execution code.
      Throws:
      TransactionException - if the execution or the commitment of the transaction fails.
    • doInTransaction

      public <R> R doInTransaction(ReservationTransactionCode<R> code) throws TransactionException
      Prepares to execution of code that involves the ReservationRepository's method(s) on MongoDB in a single transaction.
      Specified by:
      doInTransaction in class TransactionManager
      Type Parameters:
      R - the returned type of executed code.
      Parameters:
      code - the code to execute.
      Returns:
      something depending on execution code.
      Throws:
      TransactionException - if the execution or the commitment of the transaction fails.
    • doInTransaction

      public <R> R doInTransaction(ClientReservationTransactionCode<R> code) throws TransactionException
      Prepares to execution of code that involves both ClientRepository's and ReservationRepository's methods on MongoDB in a single transaction.
      Specified by:
      doInTransaction in class TransactionManager
      Type Parameters:
      R - the returned type of executed code.
      Parameters:
      code - the code to execute.
      Returns:
      something depending on execution code.
      Throws:
      TransactionException - if the execution or the commitment of the transaction fails.