Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring self injection for transactions

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?

like image 603
gs_vlad Avatar asked Apr 07 '17 14:04

gs_vlad


People also ask

What does @transactional do in Spring?

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.

Why do we use @transactional annotation?

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.

Which class in Spring is used for programmatic transaction management?

The Spring Framework provides two means of programmatic transaction management: Using the TransactionTemplate . Using a PlatformTransactionManager implementation directly.


1 Answers

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.

like image 61
Sergii Bishyr Avatar answered Oct 17 '22 06:10

Sergii Bishyr