What does @Transactional do? [duplicate]
Asked Answered
T

2

16

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

It relieves the programmer of having to use transaction.begin() and commit().
If you have a method that calls two DAO methods which normally would each have a transaction.begin and transaction.commit encompassing the real operations and call them it would result in two transactions ( and there might be rollback issues if the previous DAO method was supposed to be rolled-back too).

But if you use @transactional on your method then all those DAO calls will be wrapped in a single begin()- commit() cycle. Of course, in case you use @Transactional the DAOs must not use the begin() and commit() methods I think.

Tessi answered 10/7, 2013 at 7:21 Comment(2)
One thing want to add is that if you add some code like commit or begin programmaticlly in your method, it will work. The transactional only changes your database connection to not auto commit.Pendent
It is exactly like you say. Good that you catched it well!Taligrade
I
12

You can handle your Transactions in two ways: Programmatically and Declarative.

When you're using transaction.begin and transaction.commit and ..., you are hanling your Transactions programmatically. This way you have more control on your Transaction boundaries but you will end up with lots of similar codes (Cross Cutting Concerns) scattered all over your project.

But in Declarative way, the codes that handle the Transactions are separated from your businesses logic and will not be scattered all over your project. It's one of the main concepts of Aspect Oriented Programming.

Indicant answered 1/8, 2018 at 10:1 Comment(0)
A
0

I suggest you this link that explain everything on Spring Transaction.

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

You should see also the same attribute about Transactional (propagation, rollbackFor, etc), transaction behavior could change if you use those attributes.

Athanasius answered 29/7, 2014 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.