Skip to content

Fixed issue where a flushed save of a plain class survives a failed tenant transaction - #17075

Open
ruthst00 wants to merge 8 commits into
apache:9.0.xfrom
ruthst00:17031-flushed-save
Open

ruthst00 wants to merge 8 commits into
apache:9.0.xfrom
ruthst00:17031-flushed-save

Conversation

@ruthst00

@ruthst00 ruthst00 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #17031

Root Cause

flush: true in AbstractGormApi.requireTransactionForWrite incorrectly bypassed the transaction-safety check.

In DATABASE or SCHEMA multi-tenancy, a plain domain class—one that does not implement MultiTenant—writes to its own datastore, typically the default database. A transaction running for a tenant belongs to a different datastore and cannot roll back that plain class’s writes.

The guard in AbstractGormApi.requireTransactionForWrite was intended to reject those writes unless the plain class had a transaction in its own datastore. However, it returned immediately when flushing was requested:

if (flush || MultiTenant.isAssignableFrom(persistentClass)) {
    return
}

Consequently, save(flush: true) could write the row immediately outside the tenant’s transaction. When the tenant transaction subsequently failed, the row remained.

Changes

  • Removed the existing guard’s flush: true bypass.
  • Ensured that with no active transaction, a tenant write needs no tenant datastore lookup.
  • Extended existing in-memory and MongoDB tests for flushed writes and already-bound sessions.
  • Corrected the exception message, 9.0 upgrade guide, and matching upgrade guidance.

Validation

All affected suites passed with zero failures:

  • Datamapping core: 2,694 tests
  • Datamapping CLI: 213 tests
  • Datamapping TCK: 765 tests
  • MongoDB core: 1,329 tests

Assisted with GPT 6 Sol

Contributor Checklist

Please review the following checklist before submitting your pull request. Pull requests that do not meet these requirements may be closed without review.

Issue and Scope

  • This PR is linked to an existing issue that has been acknowledged or approved by the project team. If no approved issue exists, please give background on why this change is necessary. Tickets are preferred for release change log history.
  • This PR addresses the complete scope of the linked issue. Partial implementations or unfinished work should not be submitted for review.
  • This PR contains a single, focused change. Unrelated changes should be submitted as separate pull requests.
  • This PR targets the correct branch for the type of change:
    • Patch release branches (e.g., 7.0.x): Bug fixes only. No new features or API changes.
    • Minor release branches (e.g., 7.1.x): New features are welcome, but breaking existing APIs must be avoided.
    • Major release branches (e.g., 8.0.x): Reserved for major changes. Breaking API changes are permitted.

Code Quality

  • I have added or updated tests that cover the changes introduced in this PR. All code contributions are expected to include appropriate test coverage.
  • I have verified that all existing tests pass by running ./gradlew build --rerun-tasks.
  • My code follows the project's code style guidelines. I have run ./gradlew codeStyle and resolved any violations. See Code Style for details.
  • This PR does not include mass reformatting, style-only changes, or large-scale refactoring unless it was explicitly approved in the linked issue. Unsolicited reformatting will not be accepted.
  • If generative AI tooling was used in preparing this contribution, a quality model was used to ensure contributions are consistent with the project's quality standards.

Licensing and Attribution

Documentation

  • If this PR introduces user-facing changes, I have included or updated the relevant documentation.
  • If this PR adds a new feature, I have updated the What's New section of the Grails Guide.
  • If this PR introduces breaking changes or changes that require user action during an upgrade, I have updated the Upgrade Notes for the corresponding version in the Grails Guide.
  • The PR description clearly explains what was changed and why.

…enant transaction.

### Changes
- Removed the existing guard’s `flush: true` bypass.
- Preserved its method signature.
- Extended existing in-memory and MongoDB tests for flushed writes and already-bound sessions.
- Corrected the exception message, 9.0 upgrade guide, and matching upgrade guidance.

### Validation
All affected suites passed with zero failures:
- Datamapping core: 2,694 tests
- Datamapping CLI: 213 tests
- Datamapping TCK: 765 tests
- MongoDB core: 1,329 tests
@ruthst00
ruthst00 changed the base branch from 8.0.x to 9.0.x November 19, 2026 17:39

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A flushed save inside a failed tenant block is refused. A save of a plain class with no tenant transaction is now refused too.

…tenant datastore lookup:

- Skip the write guard before tenant lookup when no transaction is active.
- Preserve rejection of unsafe writes during an active tenant transaction, including `flush: true`.
- Add regression coverage for unregistered tenant IDs and bound sessions in the in-memory and MongoDB suites.
- Clarify the upgrade guide.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A save of a plain class with no tenant transaction still throws when another transaction is active.

… unrelated active transactions.

- Plain-class writes still reach the default datastore with an unregistered tenant ID bound.
- Unsafe writes during a real tenant transaction remain rejected, including `flush: true`.
- Added regression tests and updated the upgrade guide.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A save inside a tenant transaction is allowed when that transaction does not use synchronization.

…that transaction does not use synchronization:

- Removed the thread-level transaction flag shortcut; active tenant transactions are now detected through session holders even when synchronization is disabled.
- Added regression coverage and expanded the database/schema tenancy tests.
- Updated the upgrade guide.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The items this review found have been addressed. A save inside a tenant transaction is refused even when that transaction does not use synchronization, and a save with no tenant transaction is not refused because another transaction is active.

sbglasius
sbglasius previously approved these changes Nov 23, 2026

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI assisted review

Looks good. Two small suggestions:

* @param flush Whether the write is flushed at once
* @param flush Whether the write is flushed at once; this does not exempt it from requiring its own transaction
*/
protected void requireTransactionForWrite(boolean flush) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush is no longer used. Since 9.0 can change this API, consider dropping the parameter from this method and its six callers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 469a52f

return
}
boolean tenantTransactionActive = false
for (Map.Entry<Object, Object> entry : TransactionSynchronizationManager.getResourceMap().entrySet()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the tenantConnection == null check, this loop looks the same as isTransactionActive(multiTenantDatastore.getDatastoreForTenantId(tenantId)). Resource binding doesn't depend on synchronization, and the datastore identity check already implies the connection check. Can it be simplified, or is there a case it covers that I'm missing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 469a52f

Removed the unused `flush` parameter and its Javadoc entry.
- Updated all six callers to use `requireTransactionForWrite()`.
- Expanded regression coverage for all affected write operations, including bulk iterable overloads.
- Left flushing and transaction-detection behavior unchanged.

Replaced the resource-map loop with a direct `isTransactionActive(...)` check on the tenant datastore.
- Retained the unregistered-tenant guard.
- Updated the Javadoc and expanded tests for registered tenants and inactive tenant sessions, including unrelated transactions and disabled synchronization.
@github-actions
github-actions Bot dismissed sbglasius’s stale review November 23, 2026 16:56

Dismissed automatically: the pull request changed after this approval, from b483091 to 469a52f.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A flushed save of a class mapped only to a named connection throws when a tenant id is bound.

}
Datastore tenantDatastore = multiTenantDatastore.getDatastoreForTenantId(tenantId)
if (tenantDatastore.is(ds) || !isTransactionActive(tenantDatastore)) {
ConnectionSource tenantConnection = multiTenantDatastore.connectionSources.getConnectionSource(tenantId.toString())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A class is mapped only to a named connection, and it is not multi-tenant. A tenant id is bound, and the class is saved with a flush. That save should go through. It throws, because the named connection is treated as a tenant datastore that has no transaction. Allow the save.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aaca71a

…ferent tenant’s datastore.

- Added regression coverage for matching, different, and unregistered tenant IDs.
- Updated the upgrade guide.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

A flushed save of a plain class survives a failed tenant transaction

3 participants