1

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.

  • 3
    Your framework (probably Spring) will look for all beans that have methods annotated with `@Transactional` (probably because you told it to in your spring configuration using `` or whatever it is). It will then stick the boiler-plate code around these methods (using AOP) to start a transaction and commit it when it has finished. It may do a rollback if the method throws an exception. – BretC Dec 06 '16 at 17:16
  • It may do a rollback ? I thought that it must do a rollback. So it is ok when it comes to transactional ? Doesn matter that called methods arent annotated ? And I don't configure it. I have a spring-boot. –  Dec 06 '16 at 17:20
  • I thought it depends on what you put in as the `rollbackFor` annotation value, but I may be wrong... – BretC Dec 06 '16 at 17:28
  • 1
    Possible duplicate of [Spring - @Transactional - What happens in background?](http://stackoverflow.com/questions/1099025/spring-transactional-what-happens-in-background) – Derrops Dec 08 '16 at 04:25

1 Answers1

4

@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.

Stefan Nuxoll
  • 867
  • 5
  • 15
  • Is it sufficient to append AspectJ in pom.xml ? –  Dec 06 '16 at 17:28
  • 2
    You activate the `@Transactional` feature by either using `@EnableTransactionManagement` or to your spring-configuration. For reference check the official documentation http://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/transaction.html#tx-decl-explained – Alex Ciocan Dec 06 '16 at 17:33
  • `@EnableTransactionManagement` where? –  Dec 06 '16 at 17:38
  • 2
    On a class annotated with `@SpringConfiguration` or `@SpringBootApplication` (if you are using Spring Boot). – Stefan Nuxoll Dec 06 '16 at 17:40