What does @Transactional do?

I know that this is probably a duplicate, and ironically, before I started reading about it here and there, I thought, I knew what it was for ( needless to say , but I will still say it, please correct me where I'm wrong):

This eliminates the need for the programmer to use transaction.begin() and commit() .
If you have a method that calls two DAO methods, each of which usually has transaction.begin and transaction.commit , covering real operations, and calls them, this will lead to two transactions (and there may be problems with rollback if the previous method was supposed DAO). roll back too).

But if you use @transactional in your method, then all these DAO calls will be enclosed in one begin() loop - commit() . Of course, if you use @Transactional , the DAO should not use the begin() and commit() methods, as it seems to me.

+8
source share
2 answers

You can handle Transactions two ways: Programmatically and Declarative .

When you use transaction.begin and transaction.commit and ..., you process your Transactions programmatically. Thus, you will have more control over your Transaction boundaries but you will get many similar codes ( Cross Cutting Concerns ) scattered throughout your project.

But in the Declarative form, the codes that handle Transactions are separate from your business logic and will not be scattered throughout your project. This is one of the basic concepts of Aspect Oriented Programming .

+4
source

I offer you this link that explains everything on Spring Transaction.

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/transaction.html

You should also see the same Transactional attribute (distribution, rollbackFor, etc.), the behavior of the transaction may change if you use these attributes.

0
source

Source: https://habr.com/ru/post/949085/


All Articles