Skip to content

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

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

ruthst00 wants to merge 6 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.

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

2 participants