Apply transactional AST transformation to accessor methods in non-domain classes #15347
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, methods whose names start with
get*orset*are explicitly excludedfrom transactional method decoration by
AbstractMethodDecoratingTransformation.While this behavior makes sense for stateful objects such as GORM domain classes
(where getters/setters may trigger lazy-loading, hydration, or expected exceptions),
it can lead to surprising behavior in stateless or prototype-scoped beans, especially
Services and background jobs.
In these cases, annotating a method like
setAsDeleted()orgetOrCreate()with@Transactionalsilently results in the method not participating in a transaction,which may cause data to be flushed and persisted even when an exception is thrown.
This behavior is non-obvious and has been reported as a gotcha by multiple users
(see #14539).
Proposed change
Introduce an opt-in mechanism to allow accessor-style methods (
get*/set*)to be wrapped in transactional logic when explicitly requested.
This preserves the existing default behavior (no transactional decoration for
accessors), while allowing advanced use cases—such as stateless services,
batch jobs, and background processes—to opt in safely.
Rationale
The original exclusion of getters and setters is reasonable for domain classes
and other stateful objects, where invoking accessors inside a transaction can
lead to unexpected side effects.
However, in stateless service-style beans, accessor-prefixed methods are often
used as semantic operations rather than property accessors, and excluding them
from transactional decoration can result in silent and hard-to-debug data
consistency issues.
Making this behavior opt-in avoids breaking existing assumptions while giving
developers an explicit and predictable escape hatch.
Related discussions