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
FieldsModifier and TypeFieldDescriptionprotected ClientRepositoryFactoryUsed for creation ofClientPostgresRepositoryinstances.protected static final org.apache.logging.log4j.LoggerCreates meaningful logs on behalf of the class.protected ReservationRepositoryFactoryUsed for creation ofReservationPostgresRepositoryinstances.protected TransactionHandlerFactoryUsed for creation ofEntityManagerinstances. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTransactionManager(TransactionHandlerFactory transactionHandlerFactory, ClientRepositoryFactory clientRepositoryFactory, ReservationRepositoryFactory reservationRepositoryFactory) Sets the handler and repository factories used by the service layer. -
Method Summary
Modifier and TypeMethodDescriptionabstract <R> RPrepares to execution of code that involves bothClientRepository's andReservationRepository's methods in a single transaction.abstract <R> RdoInTransaction(ClientTransactionCode<R> code) Prepares to execution of code that involves theClientRepository's method(s) in a single transaction.abstract <R> RPrepares to execution of code that involves theReservationRepository's method(s) in a single transaction.protected <R> RexecuteInTransaction(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> RexecuteInTransaction(ClientTransactionCode<R> code, TransactionHandler<?> handler, ClientRepository clientRepository) Executes code that involves theClientRepository's method(s) in a single transaction managed by aTransactionHandler.protected <R> RexecuteInTransaction(ReservationTransactionCode<R> code, TransactionHandler<?> handler, ReservationRepository reservationRepository) Executes code that involves theReservationRepository's method(s) in a single transaction managed by aTransactionHandler.protected StringtransactionFailureMsg(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 ofEntityManagerinstances. -
clientRepositoryFactory
Used for creation ofClientPostgresRepositoryinstances. -
reservationRepositoryFactory
Used for creation ofReservationPostgresRepositoryinstances.
-
-
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 createClientRepositoryinstances.reservationRepositoryFactory- the factory to createReservationRepositoryinstances.
-
-
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- ifcodethrows aRuntimeExceptiondue 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- ifcodethrows aRuntimeExceptiondue 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- ifcodethrows aRuntimeExceptiondue 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 ofCliententities used by the code.- Returns:
- something depending on execution code.
- Throws:
TransactionException- ifcodethrowsIllegalArgumentException,UpdateFailureException,NotNullConstraintViolationExceptionorUniquenessConstraintViolationException.
-
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 ofReservationentities used by the code.- Returns:
- something depending on execution code.
- Throws:
TransactionException- ifcodethrowsIllegalArgumentException,UpdateFailureException,NotNullConstraintViolationExceptionorUniquenessConstraintViolationException.
-
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 ofCliententities used by the code.reservationRepository- a repository ofReservationentities used by the code.- Returns:
- something depending on execution code.
- Throws:
TransactionException- ifcodethrowsIllegalArgumentException,UpdateFailureException,NotNullConstraintViolationExceptionorUniquenessConstraintViolationException.
-
transactionFailureMsg
Generates a message for the failure of the transaction.- Parameters:
reason- the cause of the failure.- Returns:
- a
Stringmessage about the failure.
-