What I got into @Transactional in Spring

Nice to meet you

This is the first post. I think most of the posts are memorandums about what I stumbled upon at work. Be gentle. .. ..

environment

Java 8 Spring Boot 1.5.14

What I wanted to do

Manage transactions with @Transactional and let DB (MySQL) perform update processing

Stumble point

The Service class is annotated with @Transational (readonly = true) In the method of that class

'''hogeRepository.save()

I was doing a save process like

error contents

Caused by: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

Conclusion

The transaction that throws the update process @Transational(readonly=false) You have to do it.

Other

If @Transactional is given to both the class and the method, it will be overwritten with the one defined in the method.

Impressions

Because transactions are not placed in unexpected places (such as adding methods) I thought it was not so good to give @Transactional to a class

Note

https://dev.classmethod.jp/server-side/java/spring-nested-declarative-transaction/

Postscript (2019/1/22)

In the method that I forgot to give @Transactional (readOnly = xxxx), After accessing the repository from the service and getting the data, If you get nested data with LazyLoad of JPA outside the method of the Service class A LazyInitializationException has occurred. Please note that LazyLoad, which loads late, will not work if it is outside the method.

Recommended Posts

What I got into @Transactional in Spring
Summary of what I learned in Spring Batch
I got stuck in File
[Java] What got caught in encapsulation
What is @Autowired in Spring boot?
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
What I investigated in Wagby development Note 1
[* Java *] I participated in JJUG CCC 2019 Spring
I got stuck using snake case for variable name in Spring Boot
Where I got stuck in today's "rails tutorial" (2020/10/08)
Summary of what I learned about Spring Boot
Where I got stuck in today's "rails tutorial" (2020/10/05)
Where I got stuck in today's "rails tutorial" (2020/10/06)
I tried Spring.
What i learned
Where I got stuck in today's "rails tutorial" (2020/10/04)
Where I got stuck in today's "rails tutorial" (2020/10/07)
I checked asynchronous execution of queries in Spring Boot 1.5.9
I got a cannot resolve symbol in Android Studio
What I learned in Java (Part 3) Instruction execution statement
I got an error "undefined method` create'" in RSpec
I made a Restful server and client in Spring.
What I got from continuing to develop open source
What I learned when building a server in Java
[Rilas] What I learned in implementing the pagination function.
I got stuck in a clone of a two-dimensional array
What I stumbled upon in the ActiveModel :: Serializer test
I tried to find out what changed in Java 9
Inject Logger in Spring
[I tried] Spring tutorial
[Personal memo] Looking back on what I got stuck in Docker ... including knowledge of Linux
What I learned ② ~ Mock ~
What is Spring Tools 4
Use Interceptor in Spring
I tried Spring Batch
Microservices in Spring Cloud
Get cookies in Spring
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
I tried to verify this and that of Spring @ Transactional
What I learned in Java (Part 4) Conditional branching and repetition
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
What happens when I do new => build => save! In ActiveRecord
How to control transactions in Spring Boot without using @Transactional