Skip to content

Avoid calling EntityManager::flush() inside CalendarBalanceListener (stability/perf) #369

@algirdasc

Description

@algirdasc

Summary

CalendarBalanceListener calls flush() inside Doctrine entity listeners (postPersist, postUpdate, postRemove). This pattern can cause nested flush side-effects, make transactions harder to reason about, and can severely degrade performance during bulk operations (imports, future recurring generators).

Affected code

  • backend/src/EventListener/CalendarBalanceListener.php

Current behavior

Every Expense persist/update/remove triggers a listener that updates Calendar.balance and immediately flushes.

Problems

  • Potential nested flush / UnitOfWork edge cases.
  • Poor performance for bulk inserts/updates (flush per row).
  • Harder to test/debug because side effects happen implicitly.

Suggested approaches

Option A (recommended):

  • Move balance recomputation/update to the service layer where Expense changes occur; flush once per request/batch.

Option B:

  • Use Doctrine onFlush/preFlush and schedule the calendar update without triggering a nested flush.

Option C:

  • Remove materialized Calendar.balance and compute on-demand (may be too invasive).

Acceptance criteria

  • No flush() calls occur inside entity listeners.
  • Balance remains correct after create/update/delete and after bulk import.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions