Skip to content

Doc: Class-level @Transactional doesn't demarcate transactions for public methods in parent class [SPR-17445] #21977

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

Closed
spring-projects-issues opened this issue Oct 30, 2018 · 3 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: task A general task
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Josh Long opened SPR-17445 and commented

i have a class that has a @Transactional on the class itself. It inherits from a base class. The base class implements an interface with methods on it and has a public methods in it. 

@Transactional 
@Service

class FooService extends BaseService \{ }

 

My expectation is that the public methods in BaseService should be demarcated in a transaction. If I do nothing but override the methods and then call the super implementation, things work as expected. 

 

the good Dr. dsyer investigated the issue further.

My code is https://github.com/reactive-spring-book/bootstrap/blob/master/src/main/java/rsb/bootstrap/enable/TransactionalCustomerService.java 

 

If you remove the 3 methods, findById, save, and findAll, they are still public methods in BaseCustomerService. I have a test that tests that i can write data to the DB and see it roll back. the test fails if i have removed those methods. it succeeds when i add them.


Affects: 5.1.2

Referenced from: commits ea3250c, 561511f, f002ebd

Backported to: 5.0.11, 4.3.21

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

It looks to me like the logic in AbstractFallbackTransactionAttributeSource.computeTransactionAttribute() simply never took this scenario into account: someone annotates TransactionalCustomerService as @Transactional but all the methods are declared in a base class. The Method that is analyzed doesn't have the @Transactional annotation and neither does its declaring class, but the bean class does.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This is by design: A class-level transactional demarcation only applies to the methods as per its level in the inheritance hierarchy. Redeclaring methods at that level is exactly what it takes to include them in such a class-level transactional demarcation, or of course locally annotating the redeclared methods themselves. This allows for fine-tuning the scope of the transactional demarcation through putting methods at the appropriate place in the inheritance hierarchy.

In any case, we need to document this properly.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I've added corresponding notes to the annotation javadoc as well as the reference docs, also taking the opportunity to mention @EnableTransactionManagement earlier in the latter.

@spring-projects-issues spring-projects-issues added in: data Issues in data modules (jdbc, orm, oxm, tx) status: backported An issue that has been backported to maintenance branches type: task A general task labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.1.3 milestone Jan 11, 2019
This was referenced Jan 11, 2019
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: task A general task
Projects
None yet
Development

No branches or pull requests

2 participants