In Spring you can't simply call @Transactional
method from the same instance, because of AOP-proxy thing. Would it be nice idea to make a self injection and call that method from self proxy instance? Do you see any drawbacks?
At a high level, when a class declares @Transactional on itself or its members, Spring creates a proxy that implements the same interface(s) as the class you're annotating. In other words, Spring wraps the bean in the proxy and the bean itself has no knowledge of it.
The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.
The Spring Framework provides two means of programmatic transaction management: Using the TransactionTemplate . Using a PlatformTransactionManager implementation directly.
It is totally ok.
Moreover there was a Jira ticket for supporting this feature using @Autowired
annotations. It's fixed in Spring 4.3+
versions. However for xml-based configuration or using @Resource
annotation it's working in the earlier versions.
You can see the discussion bellow this ticket. @Transactional
is one of the use case for this:
Particularly interested in @Async and @Transactional use cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With