When @Transactional of Spring Boot does not work

I am using Spring Boot and need to manage Transaction when registering to DB.

It seems that if you add @Transactional annotation to a public method and raise an Exception, it will automatically roll back.

That's why I tried it, but it didn't work at all. I tried changing the settings given to the annotations, but they didn't roll back at all.

After struggling for two or three hours and crawling the ocean on the Web, I saw a divine lighthouse.

https://ameblo.jp/kochablo/entry-11578714824.html

God

It worked with a single blow. Thanks to the pioneer brother. Thank you very much.

However, I wondered what it was like to call it directly. I would like to help someone who is in trouble like a pioneer brother, and I will explain in detail below.

Bad example: no_good:

SampleController.java


class SampleController{
  @Autowired
  private SampleService sampleService;

  public String sample(){
    sampleService.transactionalInsert();
    return "sampleView";
  }
}

SampleService.java


class SampleService{

  public void transactionalInsert(){
    doInsert();
  }

  @Transactional
  public void doInsert(){
    //Somehow, processing such as Insert to DB
    //Processing to throw RuntimeExeption if it doesn't work
  }
}

This shouldn't be the case.

Good example: ok_woman:

SampleController.java


class SampleController{
  @Autowired
  private SampleService sampleService;

  public String sample(){
    sampleService.doInsert();
    return "sampleView";
  }
}

SampleService.java


class SampleService{

  public void transactionalInsert(){
    doInsert();
  }

  @Transactional
  public void doInsert(){
    //Somehow, processing such as Insert to DB
    //Processing to throw RuntimeExeption if it doesn't work
  }
}

Did you understand? Anyway, if you call the @Transactional method from the @Autowired class, it's crazy.

Recommended Posts

When @Transactional of Spring Boot does not work
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
@BeforeStep does not work in Tasklet of spring-batch
Sidekiq-limit_fetch does not work
When the form such as Select of Materialize does not work properly
[Ruby on Rails] When parameter id acquisition does not work
Docker does not work when DOCKER_HOST is specified in WSL2
Going out of message (Spring boot)
[Spring Boot] Role of each class
Spring should be suspected when Rails commands do not work properly
Unknown error in line 1 of pom.xml when using Spring Boot in Eclipse
If you get tired of "Spring Boot", why not try "jooby"?
Java Integer comparison (==) does not work correctly
eclipse tab width change does not work.
Story when moving from Spring Boot 1.5 to 2.1
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Simulator does not work: Unable to boot device due to insufficient system resources
WebMvcConfigurer Memorandum of Understanding for Spring Boot 2.0 (Spring 5)
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
How to not start Flyway when running unit tests in Spring Boot
Javaw.exe error when starting Spring Boot (STS)
[NetBeans] Story when it does not start
How to make @Transactional work that does not work if you use it incorrectly
Resource handler settings when delivering SPA with the static resource function of Spring Boot
UriComponentsBuilder does not encode "+" depending on spring version
Summary of what I learned about Spring Boot
[FCM] Implementation of message transmission using FCM + Spring boot
The story of raising Spring Boot 1.5 series to 2.1 series
Let's check the feel of Spring Boot + Swagger 2.0
Various correspondence table of Spring Framework and Spring Boot
[Java / Spring Boot] Spring security ④ --Implementation of login process
Final confirmation when ruby version does not switch
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
Annotation notes when writing tests for Spring Boot
Does not recognize the library when implementing jcaptcha
[Verification] Comparison of Spring Boot vs Micronaut boot speed
[Railways] Railways does not work when the RubyMine project root is other than the Rails root directory.
Challenge Spring Boot
[Spring Boot] The story that the bean of the class with ConfigurationProperties annotation was not found
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
"Do not show again" check does not work in the warning dialog when starting applet
Customize the display when an error such as 404 Not Found occurs in Spring Boot
Specify the encoding of static resources in Spring Boot
I checked asynchronous execution of queries in Spring Boot 1.5.9
A memorandum of addiction to Spring Boot2 x Doma2
Access the built-in h2db of spring boot with jdbcTemplate
05. I tried to stub the source of Spring Boot
Note: Memory Analyzer does not work on Mac OS
I tried to reduce the capacity of Spring Boot
When Docker for Mac 2.4.0.0 does not reflect file changes
How to use CommandLineRunner in Spring Batch of Spring Boot
[Note] When the page access speed does not increase
A memorandum when creating a REST service with Spring Boot
Create Restapi with Spring Boot ((1) Until Run of App)
Correspondence when Ruby version does not switch in rbenv
How to boot by environment with Spring Boot of Maven
Specify spring.profiles.active via context-param of web.xml in Spring Boot
Hivernate Validator does not work in WAS Liberty environment
File upload with Spring Boot (do not use Multipart File)