Repository navigation
[CRUI-124] fix: prevent concurrent duplicate open shift creation - #142
huytran2005 merged 4 commits into
Conversation
- Modified `PosShiftService.openShift()` to use `ReentrantLock` instead of a plain Java object lock. - Replaced the `synchronized` block with `ReentrantLock`. - Used `TransactionSynchronizationManager.registerSynchronization()` to correctly defer `lock.unlock()` until `afterCompletion` (transaction commit/rollback). - This prevents the race condition where `synchronized` block is released before the Spring proxy commits the newly created `PosShift`, resolving the `IncorrectResultSizeDataAccessException` observed in concurrent setups. - Avoided using `Propagation.REQUIRES_NEW` to prevent HikariCP connection pool exhaustion when multiple active outer transactions are waiting. Co-authored-by: huytran2005 <[email protected]>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Tip 🎯 Customize your risk assessments
✅ Automatic approvals, with your rules
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. CodeRabbit Verify
|
- Modified `PosShiftService.openShift()` to use `ReentrantLock` instead of a plain Java object lock. - Replaced the `synchronized` block with `ReentrantLock`. - Used `TransactionSynchronizationManager.registerSynchronization()` to correctly defer `lock.unlock()` until `afterCompletion` (transaction commit/rollback). - Addressed SonarQube rule java:S2222 (Locks should be released in a "finally" block) by ensuring that variable initializations are completely outside of the lock/try-catch structure according to best practices. - Avoided using `Propagation.REQUIRES_NEW` to prevent HikariCP connection pool exhaustion when multiple active outer transactions are waiting. Co-authored-by: huytran2005 <[email protected]>
- Modified `PosShiftService.openShift()` to use `ReentrantLock` instead of a plain Java object lock.
- Replaced the `synchronized` block with `ReentrantLock`.
- Used `TransactionSynchronizationManager.registerSynchronization()` to correctly defer `lock.unlock()` until `afterCompletion` (transaction commit/rollback).
- Addressed SonarQube rule java:S2222 (Locks should be released in a "finally" block) by ensuring that variable initializations are completely outside of the lock/try-catch structure according to best practices and adding a method-level warning suppression because standard unconditional `finally { lock.unlock(); }` is semantically incorrect here (as the unlock is deferred to the transaction manager).
- Avoided using `Propagation.REQUIRES_NEW` to prevent HikariCP connection pool exhaustion when multiple active outer transactions are waiting.
Co-authored-by: huytran2005 <[email protected]>
…968755668811305211
|



Problem statement and rationale
The CI build failed randomly in the
PosConcurrencyIntegrationTest.testConcurrentChargesNeverExceedCreditLimittest case.The test runs 10 threads, each executing a POS transaction. Inside
PosTransactionService.processChargeTransaction(which is@Transactional), it delegates toPosShiftService.openShiftto ensure a shift is open for the station.openShiftutilized aConcurrentHashMapwith asynchronizedblock to ensure exactly one thread creates an open shift per station.However, the
synchronizedblock would complete and release its lock before the outer Spring@Transactionalproxy could commit the new shift into the DB. This allowed a second thread to enter the lock, fail to find an open shift in the DB, and insert a duplicate. Subsequent reads threwIncorrectResultSizeDataAccessException(Query did not return a unique result).Summary of code and test changes
synchronizedblock with aReentrantLockstored in the map.TransactionSynchronizationManager.registerSynchronizationinsidePosShiftService.openShift()so that the thread creating the shift retains the lock until the underlying transaction commits or rolls back (afterCompletion).Propagation.REQUIRES_NEWwithin limited HikariCP connection pools.test_lock.java,test_lock.class).Verification commands executed and test results
Executed targeted integration test locally:
Executed full test suite:
Residual limitations or human review recommendations
The change depends on Spring's
TransactionSynchronizationManager, which guarantees that callbacks run on the thread bounding the transaction.ReentrantLockexpects unlocks on the locking thread, making this pairing perfectly safe for Spring declarative transactions. Ensure any future changes handlingstationLockscorrectly account for deferred unlocks ifisSynchronizationActive()evaluates to false.Fixes Run ID:
41425244669Fixes commit:
a3f203860609aa2639474f6a3551e8cdc0fa1f4dPR created automatically by Jules for task 8968755668811305211 started by @huytran2005