Class TransactionManager

java.lang.Object
io.github.marcopaglio.booking.transaction.manager.TransactionManager
Direct Known Subclasses:
TransactionMongoManager, TransactionPostgresManager

public abstract class TransactionManager extends Object
Provides methods for managing transactions in the booking application.
  • Field Details

    • LOGGER

      protected static final org.apache.logging.log4j.Logger LOGGER
      Creates meaningful logs on behalf of the class.
    • transactionHandlerFactory

      protected TransactionHandlerFactory transactionHandlerFactory
      Used for creation of EntityManager instances.
    • clientRepositoryFactory

      protected ClientRepositoryFactory clientRepositoryFactory
      Used for creation of ClientPostgresRepository instances.
    • reservationRepositoryFactory

      protected ReservationRepositoryFactory reservationRepositoryFactory
      Used for creation of ReservationPostgresRepository instances.
  • Constructor Details

    • TransactionManager

      protected TransactionManager(TransactionHandlerFactory transactionHandlerFactory, ClientRepositoryFactory clientRepositoryFactory, ReservationRepositoryFactory reservationRepositoryFactory)
      Sets the handler and repository factories used by the service layer.
      Parameters:
      transactionHandlerFactory - the factory to create handler instances.
      clientRepositoryFactory - the factory to create ClientRepository instances.
      reservationRepositoryFactory - the factory to create ReservationRepository instances.
  • Method Details

    • doInTransaction

      public abstract <R> R doInTransaction(ClientTransactionCode<R> code) throws TransactionException
      Prepares to execution of code that involves the ClientRepository's method(s) in a single transaction.
      Type Parameters:
      R - the returned type of executed code.
      Parameters:
      code - the code to execute.
      Returns:
      something depending on execution code.
      Throws:
      TransactionException - if code throws a RuntimeException due to database inconsistency.
    • doInTransaction

      public abstract <R> R doInTransaction(ReservationTransactionCode<R> code) throws TransactionException
      Prepares to execution of code that involves the ReservationRepository's method(s) in a single transaction.
      Type Parameters:
      R - the returned type of executed code.
      Parameters:
      code - the code to execute.
      Returns:
      something depending on execution code.
      Throws:
      TransactionException - if code throws a RuntimeException due to database inconsistency.
    • doInTransaction

      public abstract <R> R doInTransaction(ClientReservationTransactionCode<R> code) throws TransactionException
      Prepares to execution of code that involves both ClientRepository's and ReservationRepository's methods in a single transaction.
      Type Parameters:
      R - the returned type of executed code.
      Parameters:
      code - the code to execute.
      Returns:
      something depending on execution code.
      Throws:
      TransactionException - if code throws a RuntimeException due to database inconsistency.
    • executeInTransaction

      protected <R> R executeInTransaction(ClientTransactionCode<R> code, TransactionHandler<?> handler, ClientRepository clientRepository) throws TransactionException
      Executes code that involves the ClientRepository's method(s) in a single transaction managed by a TransactionHandler.
      Type Parameters:
      R - the returned type of executed code.
      Parameters:
      code - the code to execute.
      handler - the handler of the transaction.
      clientRepository - a repository of Client entities used by the code.
      Returns:
      something depending on execution code.
      Throws:
      TransactionException - if code throws IllegalArgumentException, UpdateFailureException, NotNullConstraintViolationException or UniquenessConstraintViolationException.
    • executeInTransaction

      protected <R> R executeInTransaction(ReservationTransactionCode<R> code, TransactionHandler<?> handler, ReservationRepository reservationRepository) throws TransactionException
      Executes code that involves the ReservationRepository's method(s) in a single transaction managed by a TransactionHandler.
      Type Parameters:
      R - the returned type of executed code.
      Parameters:
      code - the code to execute.
      handler - the handler of the transaction.
      reservationRepository - a repository of Reservation entities used by the code.
      Returns:
      something depending on execution code.
      Throws:
      TransactionException - if code throws IllegalArgumentException, UpdateFailureException, NotNullConstraintViolationException or UniquenessConstraintViolationException.
    • executeInTransaction

      protected <R> R executeInTransaction(ClientReservationTransactionCode<R> code, TransactionHandler<?> handler, ClientRepository clientRepository, ReservationRepository reservationRepository) throws TransactionException
      Executes code that involves both ClientRepository's and ReservationRepository's methods in a single transaction managed by a TransactionHandler.
      Type Parameters:
      R - the returned type of executed code.
      Parameters:
      code - the code to execute.
      handler - the handler of the transaction.
      clientRepository - a repository of Client entities used by the code.
      reservationRepository - a repository of Reservation entities used by the code.
      Returns:
      something depending on execution code.
      Throws:
      TransactionException - if code throws IllegalArgumentException, UpdateFailureException, NotNullConstraintViolationException or UniquenessConstraintViolationException.
    • transactionFailureMsg

      protected String transactionFailureMsg(String reason)
      Generates a message for the failure of the transaction.
      Parameters:
      reason - the cause of the failure.
      Returns:
      a String message about the failure.