Skip to content

Commit

Permalink
FINERACT-2081: Support read-only transactions via Batch API
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsaghy committed Dec 16, 2024
1 parent 51433ff commit 13373ab
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.fineract.infrastructure.core.filters.BatchCallHandler;
import org.apache.fineract.infrastructure.core.filters.BatchFilter;
import org.apache.fineract.infrastructure.core.filters.BatchRequestPreprocessor;
import org.apache.fineract.infrastructure.core.persistence.ExtendedJpaTransactionManager;
import org.jetbrains.annotations.NotNull;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.NonTransientDataAccessException;
Expand Down Expand Up @@ -141,6 +142,9 @@ private List<BatchResponse> callInTransaction(Consumer<TransactionTemplate> tran
List<BatchResponse> responseList = new ArrayList<>();
try {
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
if (transactionManager instanceof ExtendedJpaTransactionManager extendedJpaTransactionManager) {
transactionTemplate.setReadOnly(extendedJpaTransactionManager.isReadOnlyConnection());
}
transactionConfigurator.accept(transactionTemplate);
return transactionTemplate.execute(status -> {
BatchRequestContextHolder.setEnclosingTransaction(status);
Expand Down Expand Up @@ -240,7 +244,8 @@ private BatchResponse executeRequest(BatchRequest request, UriInfo uriInfo) {
BatchRequestContextHolder.setRequestAttributes(new HashMap<>(Optional.ofNullable(request.getHeaders())
.map(list -> list.stream().collect(Collectors.toMap(Header::getName, Header::getValue)))
.orElse(Collections.emptyMap())));
if (BatchRequestContextHolder.isEnclosingTransaction()) {
if (BatchRequestContextHolder.isEnclosingTransaction()
&& BatchRequestContextHolder.getEnclosingTransaction().stream().anyMatch(ts -> !ts.isReadOnly())) {
entityManager.flush();
}
BatchCallHandler callHandler = new BatchCallHandler(this.batchFilters, commandStrategy::execute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void doCommit(DefaultTransactionStatus status) {
invokeLifecycleCallbacks(TransactionLifecycleCallback::afterCommit);
}

private boolean isReadOnlyConnection() {
public boolean isReadOnlyConnection() {
try (Connection connection = getDataSource().getConnection()) {
return connection.isReadOnly();
} catch (SQLException e) {
Expand Down

0 comments on commit 13373ab

Please sign in to comment.