Reputation:
Let's consider:
delete()
update()
insert()
@Transactional
void doDBstuff(){
delete()
update()
insert
}
As you can see only doDBstuff
. It calls other method (delete, update, insert
). All of them using mybatis
to work on database.
Tell me please, if this @Transactional
annotation should be working. I tested it manually and it seems be ok, however I want to be sure and understand better how does it work.
So I ask for answers:
1. Is it transactional-safe ?
2. How does it work underhood ? I know that it is complex. I mean only some intuion, rougly view on subject.
Upvotes: 1
Views: 3285
Reputation: 867
@Transactional works like most other magic in Spring, proxies (or not, if you are using AspectJ). If you inject a bean that has any @Transactional annotations Spring Framework automatically wires up a proxy to ensure any calls to @Transactional methods are wrapped in a transaction as requested by the annotation (and rolled back if an exception is thrown).
As to whether your code will actually run in a transaction or not depends. If you are using AspectJ then yes, it will run in a transaction as expected, end of story. If you are not using AspectJ and Spring has to create a proxy then it will work anywhere it is called on an autowired bean for your class - but if you try to call it on an instance you constructed manually or from within the class itself it will silently fail to run inside a transaction.
Upvotes: 4