codeconsole commented on code in PR #16415:
URL: https://github.com/apache/grails-core/pull/16415#discussion_r4124741196


##########
grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/query/MongoQuery.java:
##########
@@ -464,8 +464,12 @@ public MongoQuery(AbstractMongoSession session, 
PersistentEntity entity) {
 
     @Override
     protected void flushBeforeQuery() {
-        // with Mongo we only flush the session if a transaction is not active 
to allow for session-managed transactions
-        if (!TransactionSynchronizationManager.isSynchronizationActive()) {
+        // Within a transaction the session is not flushed ahead of a query, 
so that a rollback can still
+        // discard what is queued: without a server-side transaction, a 
flushed write cannot be taken
+        // back. Inside one it is aborted with the transaction, so the query 
sees the transaction's own
+        // writes, as on Hibernate.
+        if (!TransactionSynchronizationManager.isSynchronizationActive() ||
+                (mongoSession != null && mongoSession.hasActiveTransaction())) 
{
             super.flushBeforeQuery();

Review Comment:
   Fixed in 84d48dd0b2 by guarding the flush rather than deferring the query 
flush. `MongoCodecSession.flush(WriteConcern)` and 
`MongoSession.flush(WriteConcern)` replace `AbstractSession.flush()` without 
its `flushActive` guard; both now return when a flush is already running.
   
   The recursion predates this PR: outside any transaction, where a query 
always flushes first, the same `beforeInsert { count() }` overflows on `8.0.x`. 
The query flush here extended it to server-side transactions.
   
   `MongoFlushListenerQuerySpec` covers `beforeInsert` and `beforeUpdate` 
queries with server-side transactions on, in a transaction without them, 
outside any transaction, and on the deprecated mapping engine (`MongoSession`). 
The first, third and fourth overflowed before the fix.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to