Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify documentation for @Transactional on interfaces #23538

Closed
aCodeRancher opened this issue Aug 28, 2019 · 4 comments
Closed

Clarify documentation for @Transactional on interfaces #23538

aCodeRancher opened this issue Aug 28, 2019 · 4 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Milestone

Comments

@aCodeRancher
Copy link

aCodeRancher commented Aug 28, 2019

Affects: Spring framework version 5

The Spring Reference Manual states the following.

The Spring team recommends that you annotate only concrete classes (and methods of concrete classes) with the @Transactional annotation, as opposed to annotating interfaces. You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you use interface-based proxies. The fact that Java annotations are not inherited from interfaces means that, if you use class-based proxies (proxy-target-class="true") or the weaving-based aspect (mode="aspectj"), the transaction settings are not recognized by the proxying and weaving infrastructure, and the object is not wrapped in a transactional proxy, which would be decidedly bad.

But I tried an example using CGLIB proxy, and annotate @Transactional on an interface. I expect an error. But it does not throw any error, to my surprise. I guess Spring documentation needs to be updated, maybe?

We posted our code example here in coderanch.com/forum and one staff recommends me to ask Spring community. The detail code example and discussion is here. Please take a look: https://coderanch.com/t/714204/frameworks/Spring-Transactional-inheritance

Thanks.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 28, 2019
@sbrannen sbrannen changed the title About annotating @Transactional on interface Clarify documentation for @Transactional on interfaces Aug 29, 2019
@sbrannen sbrannen assigned sbrannen and jhoeller and unassigned sbrannen Aug 29, 2019
@sbrannen
Copy link
Member

@jhoeller, I've tentatively assigned this to you.

@DcortezMeleth
Copy link

DcortezMeleth commented Nov 24, 2019

Hi. Anyone can explain why this documentation fragment is no longer relevant?

@bipster
Copy link

bipster commented Jan 30, 2020

@DcortezMeleth The documentation fragment is accurate. If you're using class-based proxies or AspectJ, the @Transactional annotation will not be processed.

I believe the original poster interpreted the last sentence (perhaps the "which would be decidedly bad" part) to mean that an error would be thrown if you use @Transactional in those contexts, which is not the case. Perhaps the documentation could be clarified by replacing:

... and the object is not wrapped in a transactional proxy, which would be decidedly bad.

with the danger spelled out:

... and the object is not wrapped in a transactional proxy. In other words, the annotations will be silently ignored and you will not realize your code does not have transactional semantics. Your code might still appear to "work" until, for example, a runtime exception in the middle of your "transaction" does not trigger a rollback.

@rstoyanchev rstoyanchev added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Nov 10, 2021
@jhoeller jhoeller added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 10, 2023
@jhoeller jhoeller added this to the 6.0.13 milestone Oct 10, 2023
@jhoeller
Copy link
Contributor

We do detect interface-annotated methods with CGLIB proxies as of #18894 now which needs to be reflected in the docs.

@jhoeller jhoeller added the for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x label Oct 10, 2023
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x labels Oct 10, 2023
jhoeller added a commit that referenced this issue Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

7 participants