HuangXi created FINERACT-2561:
---------------------------------
Summary: Performance issue: repeated organisation currency
validation query inside saveAllDebitOrCreditEntries() loop
Key: FINERACT-2561
URL: https://issues.apache.org/jira/browse/FINERACT-2561
Project: Apache Fineract
Issue Type: Bug
Components: Accounting
Affects Versions: 1.14.0
Reporter: HuangXi
h2. Summary
Performance issue: repeated organisation currency validation query inside
journal entry insertion loop.
h2. Description
There is a database-related performance issue in the manual journal entry
creation flow.
In the following class and method, the same organisation currency validation
query is executed inside the per-entry loop.
h3. Class
{{org.apache.fineract.accounting.journalentry.service.JournalEntryWritePlatformServiceJpaRepositoryImpl}}
h3. Source file
{{fineract-provider/src/main/java/org/apache/fineract/accounting/journalentry/service/JournalEntryWritePlatformServiceJpaRepositoryImpl.java}}
h3. Method
{{saveAllDebitOrCreditEntries(...)}}
h2. Current implementation
Inside the loop, the code executes:
{{this.organisationCurrencyRepository.findOneWithNotFoundDetection(currencyCode);}}
Since {{currencyCode}} does not change within a single request, this causes the
same validation query to be executed repeatedly for every debit/credit entry.
h2. Relevant code path
{{createJournalEntry(...)}} → {{saveAllDebitOrCreditEntries(...)}}
h2. Why this looks unnecessary
In the same class, the comparable method for opening balance processing already
performs the same currency validation once before entering the loop.
h3. Method
{{saveAllDebitOrCreditOpeningBalanceEntries(...)}}
That means the current implementation in {{saveAllDebitOrCreditEntries(...)}}
is inconsistent with the similar logic already used elsewhere in the same class.
h2. Current behavior
For each journal entry line, the service does the following:
# Loads the GL account
# Validates the GL account
# Validates the same organisation currency again
# Persists the journal entry
As a result, one manual journal entry request with many debit/credit lines
triggers repeated identical database lookups for the same {{currencyCode}}.
h2. Expected behavior
Organisation currency validation should still happen, but it should be executed
once per request (or once per method invocation before iterating over the
entries), instead of once per loop iteration.
h2. Impact
* Avoidable repeated database round-trips
* Increased latency when creating journal entries with many lines
* Unnecessary database load in an accounting write path
h2. Steps to reproduce
# Submit a manual journal entry request with multiple debit and/or credit
lines using the same currency code.
# Enable SQL logging or use query profiling.
# Observe that
{{organisationCurrencyRepository.findOneWithNotFoundDetection(currencyCode)}}
is executed repeatedly during the same request.
h2. Notes
This issue is not about removing validation. The issue is that the same
validation query is executed repeatedly inside the loop even though
{{currencyCode}} remains constant for the whole operation.
h2. Additional reference
A similar flow in the same class already validates currency outside the loop:
{{saveAllDebitOrCreditOpeningBalanceEntries(...)}}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)