Class TransactionManager
java.lang.Object
io.github.marcopaglio.booking.transaction.manager.TransactionManager
- Direct Known Subclasses:
TransactionMongoManager
,TransactionPostgresManager
Provides methods for managing transactions in the booking application.
-
Field Summary
Modifier and TypeFieldDescriptionprotected ClientRepositoryFactory
Used for creation ofClientPostgresRepository
instances.protected static final org.apache.logging.log4j.Logger
Creates meaningful logs on behalf of the class.protected ReservationRepositoryFactory
Used for creation ofReservationPostgresRepository
instances.protected TransactionHandlerFactory
Used for creation ofEntityManager
instances. -
Constructor Summary
ModifierConstructorDescriptionprotected
TransactionManager
(TransactionHandlerFactory transactionHandlerFactory, ClientRepositoryFactory clientRepositoryFactory, ReservationRepositoryFactory reservationRepositoryFactory) Sets the handler and repository factories used by the service layer. -
Method Summary
Modifier and TypeMethodDescriptionabstract <R> R
Prepares to execution of code that involves bothClientRepository
's andReservationRepository
's methods in a single transaction.abstract <R> R
doInTransaction
(ClientTransactionCode<R> code) Prepares to execution of code that involves theClientRepository
's method(s) in a single transaction.abstract <R> R
Prepares to execution of code that involves theReservationRepository
's method(s) in a single transaction.protected <R> R
executeInTransaction
(ClientReservationTransactionCode<R> code, TransactionHandler<?> handler, ClientRepository clientRepository, ReservationRepository reservationRepository) Executes code that involves bothClientRepository
's andReservationRepository
's methods in a single transaction managed by aTransactionHandler
.protected <R> R
executeInTransaction
(ClientTransactionCode<R> code, TransactionHandler<?> handler, ClientRepository clientRepository) Executes code that involves theClientRepository
's method(s) in a single transaction managed by aTransactionHandler
.protected <R> R
executeInTransaction
(ReservationTransactionCode<R> code, TransactionHandler<?> handler, ReservationRepository reservationRepository) Executes code that involves theReservationRepository
's method(s) in a single transaction managed by aTransactionHandler
.protected String
transactionFailureMsg
(String reason) Generates a message for the failure of the transaction.
-
Field Details
-
LOGGER
protected static final org.apache.logging.log4j.Logger LOGGERCreates meaningful logs on behalf of the class. -
transactionHandlerFactory
Used for creation ofEntityManager
instances. -
clientRepositoryFactory
Used for creation ofClientPostgresRepository
instances. -
reservationRepositoryFactory
Used for creation ofReservationPostgresRepository
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 createClientRepository
instances.reservationRepositoryFactory
- the factory to createReservationRepository
instances.
-
-
Method Details
-
doInTransaction
Prepares to execution of code that involves theClientRepository
'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
- ifcode
throws aRuntimeException
due to database inconsistency.
-
doInTransaction
public abstract <R> R doInTransaction(ReservationTransactionCode<R> code) throws TransactionException Prepares to execution of code that involves theReservationRepository
'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
- ifcode
throws aRuntimeException
due to database inconsistency.
-
doInTransaction
public abstract <R> R doInTransaction(ClientReservationTransactionCode<R> code) throws TransactionException Prepares to execution of code that involves bothClientRepository
's andReservationRepository
'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
- ifcode
throws aRuntimeException
due to database inconsistency.
-
executeInTransaction
protected <R> R executeInTransaction(ClientTransactionCode<R> code, TransactionHandler<?> handler, ClientRepository clientRepository) throws TransactionException Executes code that involves theClientRepository
's method(s) in a single transaction managed by aTransactionHandler
.- Type Parameters:
R
- the returned type of executed code.- Parameters:
code
- the code to execute.handler
- the handler of the transaction.clientRepository
- a repository ofClient
entities used by the code.- Returns:
- something depending on execution code.
- Throws:
TransactionException
- ifcode
throwsIllegalArgumentException
,UpdateFailureException
,NotNullConstraintViolationException
orUniquenessConstraintViolationException
.
-
executeInTransaction
protected <R> R executeInTransaction(ReservationTransactionCode<R> code, TransactionHandler<?> handler, ReservationRepository reservationRepository) throws TransactionException Executes code that involves theReservationRepository
's method(s) in a single transaction managed by aTransactionHandler
.- Type Parameters:
R
- the returned type of executed code.- Parameters:
code
- the code to execute.handler
- the handler of the transaction.reservationRepository
- a repository ofReservation
entities used by the code.- Returns:
- something depending on execution code.
- Throws:
TransactionException
- ifcode
throwsIllegalArgumentException
,UpdateFailureException
,NotNullConstraintViolationException
orUniquenessConstraintViolationException
.
-
executeInTransaction
protected <R> R executeInTransaction(ClientReservationTransactionCode<R> code, TransactionHandler<?> handler, ClientRepository clientRepository, ReservationRepository reservationRepository) throws TransactionException Executes code that involves bothClientRepository
's andReservationRepository
's methods in a single transaction managed by aTransactionHandler
.- Type Parameters:
R
- the returned type of executed code.- Parameters:
code
- the code to execute.handler
- the handler of the transaction.clientRepository
- a repository ofClient
entities used by the code.reservationRepository
- a repository ofReservation
entities used by the code.- Returns:
- something depending on execution code.
- Throws:
TransactionException
- ifcode
throwsIllegalArgumentException
,UpdateFailureException
,NotNullConstraintViolationException
orUniquenessConstraintViolationException
.
-
transactionFailureMsg
Generates a message for the failure of the transaction.- Parameters:
reason
- the cause of the failure.- Returns:
- a
String
message about the failure.
-