| 1 | package io.github.marcopaglio.booking.transaction.manager.postgres; | |
| 2 | ||
| 3 | import io.github.marcopaglio.booking.exception.TransactionException; | |
| 4 | import io.github.marcopaglio.booking.repository.factory.ClientRepositoryFactory; | |
| 5 | import io.github.marcopaglio.booking.repository.factory.ReservationRepositoryFactory; | |
| 6 | import io.github.marcopaglio.booking.repository.postgres.ClientPostgresRepository; | |
| 7 | import io.github.marcopaglio.booking.repository.postgres.ReservationPostgresRepository; | |
| 8 | import io.github.marcopaglio.booking.transaction.code.ClientReservationTransactionCode; | |
| 9 | import io.github.marcopaglio.booking.transaction.code.ClientTransactionCode; | |
| 10 | import io.github.marcopaglio.booking.transaction.code.ReservationTransactionCode; | |
| 11 | import io.github.marcopaglio.booking.transaction.handler.factory.TransactionHandlerFactory; | |
| 12 | import io.github.marcopaglio.booking.transaction.handler.postgres.TransactionPostgresHandler; | |
| 13 | import io.github.marcopaglio.booking.transaction.manager.TransactionManager; | |
| 14 | import jakarta.persistence.EntityManagerFactory; | |
| 15 | import jakarta.persistence.RollbackException; | |
| 16 | ||
| 17 | /** | |
| 18 | * An implementation of {@code TransactionManager} for managing code executed | |
| 19 | * on PostgreSQL within transactions. | |
| 20 | */ | |
| 21 | public class TransactionPostgresManager extends TransactionManager { | |
| 22 | /** | |
| 23 | * Specifies that the reason the transaction fails is a commitment failure. | |
| 24 | */ | |
| 25 | private static final String COMMIT_FAILURE = "a commitment failure"; | |
| 26 | ||
| 27 | /** | |
| 28 | * Used for executing code on {@code ClientRepository} and/or {@code ReservationRepository} | |
| 29 | * into transactions. | |
| 30 | * Particularly, it allows to create entity manager, transactions and repositories. | |
| 31 | */ | |
| 32 | private EntityManagerFactory emf; | |
| 33 | ||
| 34 | /** | |
| 35 | * Constructs a manager for applying code that uses entity repositories | |
| 36 | * using PostgreSQL transactions. | |
| 37 | * | |
| 38 | * @param emf the entity manager factory used to interact | |
| 39 | * with the persistence provider. | |
| 40 | * @param transactionHandlerFactory the factory to create {@code EntityManager} instances. | |
| 41 | * @param clientRepositoryFactory the factory to create | |
| 42 | * {@code ClientPostgresRepository} instances. | |
| 43 | * @param reservationRepositoryFactory the factory to create | |
| 44 | * {@code ReservationPostgresRepository} instances. | |
| 45 | */ | |
| 46 | public TransactionPostgresManager(EntityManagerFactory emf, | |
| 47 | TransactionHandlerFactory transactionHandlerFactory, | |
| 48 | ClientRepositoryFactory clientRepositoryFactory, | |
| 49 | ReservationRepositoryFactory reservationRepositoryFactory) { | |
| 50 | super(transactionHandlerFactory, clientRepositoryFactory, reservationRepositoryFactory); | |
| 51 | this.emf = emf; | |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * Prepares to execution of code that involves the {@code ClientRepository}'s method(s) | |
| 56 | * on PostgreSQL in a single transaction. | |
| 57 | * | |
| 58 | * @param <R> the returned type of executed code. | |
| 59 | * @param code the code to execute. | |
| 60 | * @return something depending on execution code. | |
| 61 | * @throws TransactionException if the execution or the commitment of the transaction fails. | |
| 62 | */ | |
| 63 | @Override | |
| 64 | public <R> R doInTransaction(ClientTransactionCode<R> code) throws TransactionException { | |
| 65 | TransactionPostgresHandler sessionHandler = | |
| 66 | transactionHandlerFactory.createTransactionHandler(emf); | |
| 67 | ClientPostgresRepository clientRepository = clientRepositoryFactory | |
| 68 | .createClientRepository(sessionHandler.getHandler()); | |
| 69 | try { | |
| 70 |
1
1. doInTransaction : replaced return value with null for io/github/marcopaglio/booking/transaction/manager/postgres/TransactionPostgresManager::doInTransaction → KILLED |
return executeInTransaction(code, sessionHandler, clientRepository); |
| 71 | } catch(RollbackException e) { | |
| 72 | LOGGER.warn(e.getMessage()); | |
| 73 | throw new TransactionException( | |
| 74 | transactionFailureMsg(COMMIT_FAILURE), e.getCause()); | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Prepares to execution of code that involves the {@code ReservationRepository}'s method(s) | |
| 80 | * on PostgreSQL in a single transaction. | |
| 81 | * | |
| 82 | * @param <R> the returned type of executed code. | |
| 83 | * @param code the code to execute. | |
| 84 | * @return something depending on execution code. | |
| 85 | * @throws TransactionException if the execution or the commitment of the transaction fails. | |
| 86 | */ | |
| 87 | @Override | |
| 88 | public <R> R doInTransaction(ReservationTransactionCode<R> code) throws TransactionException { | |
| 89 | TransactionPostgresHandler sessionHandler = | |
| 90 | transactionHandlerFactory.createTransactionHandler(emf); | |
| 91 | ReservationPostgresRepository reservationRepository = reservationRepositoryFactory | |
| 92 | .createReservationRepository(sessionHandler.getHandler()); | |
| 93 | try { | |
| 94 |
1
1. doInTransaction : replaced return value with null for io/github/marcopaglio/booking/transaction/manager/postgres/TransactionPostgresManager::doInTransaction → KILLED |
return executeInTransaction(code, sessionHandler, reservationRepository); |
| 95 | } catch(RollbackException e) { | |
| 96 | LOGGER.warn(e.getMessage()); | |
| 97 | throw new TransactionException( | |
| 98 | transactionFailureMsg(COMMIT_FAILURE), e.getCause()); | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * Prepares to execution of code that involves both {@code ClientRepository}'s and | |
| 104 | * {@code ReservationRepository}'s methods on PostgreSQL in a single transaction. | |
| 105 | * | |
| 106 | * @param <R> the returned type of executed code. | |
| 107 | * @param code the code to execute. | |
| 108 | * @return something depending on execution code. | |
| 109 | * @throws TransactionException if the execution or the commitment of the transaction fails. | |
| 110 | */ | |
| 111 | @Override | |
| 112 | public <R> R doInTransaction(ClientReservationTransactionCode<R> code) throws TransactionException { | |
| 113 | TransactionPostgresHandler sessionHandler = | |
| 114 | transactionHandlerFactory.createTransactionHandler(emf); | |
| 115 | ClientPostgresRepository clientRepository = clientRepositoryFactory | |
| 116 | .createClientRepository(sessionHandler.getHandler()); | |
| 117 | ReservationPostgresRepository reservationRepository = reservationRepositoryFactory | |
| 118 | .createReservationRepository(sessionHandler.getHandler()); | |
| 119 | try { | |
| 120 |
1
1. doInTransaction : replaced return value with null for io/github/marcopaglio/booking/transaction/manager/postgres/TransactionPostgresManager::doInTransaction → KILLED |
return executeInTransaction(code, sessionHandler, clientRepository, reservationRepository); |
| 121 | } catch(RollbackException e) { | |
| 122 | LOGGER.warn(e.getMessage()); | |
| 123 | throw new TransactionException( | |
| 124 | transactionFailureMsg(COMMIT_FAILURE), e.getCause()); | |
| 125 | } | |
| 126 | } | |
| 127 | } | |
Mutations | ||
| 70 |
1.1 |
|
| 94 |
1.1 |
|
| 120 |
1.1 |