1

I am building ReSTful APIs using Spring-boot 1.5.10.RELEASE, Java 8 and MySQL 5.6 and jOOQ(https://www.jooq.org/doc/3.9/manual/). In Service layer, I am using @Transactional annotation offered by Spring framework.

Here is how my UserService class looks like:

@Service
@Slf4j
public class UserService {

    //My repository isn't using @Transactional annotation, only Service layer
    @Autowired
    private UserRepository userRepository;

    @Transactional
    public User createUser(User user){
        ...
    }

    @Transactional
    public User updateUser(User user){
        ...
    }

    @Transactional
    public Boolean deleteUser(Integer userId){
        ...
    }

    @Transactional(readOnly = true)
    public User findUserByUserId(Integer userId){
        ...
    }
}

Shall I use @Transactional(readOnly=true) or not use @Transactional annotation at all for HTTP GET API calls? Are there any side effects I should be aware of when not using @Transactional on all CRUD operations above? Do I have to make jOOQ aware of Transactional capabilities by offering configuration?

I have not used @Transactional from Spring framework in past but do understand what transactions are and why we need them.

I hope I didn't miss any details in question :)

realPK
  • 2,630
  • 29
  • 22
  • Possible duplicate of [Some clarification about Spring @Transactional annotation on a method](https://stackoverflow.com/questions/15300483/some-clarification-about-spring-transactional-annotation-on-a-method) – Hadi J Apr 23 '18 at 06:00
  • @HadiJ: Thanks for sharing URL but question you shared uses Hibernate as ORM and I am using jOOQ. – realPK Apr 23 '18 at 06:26
  • 1
    As a rule, I think it is a good idea to annotate them with `@Transactional(readOnly = true)`. If you perform more than one persistence call in your method, it should guarantee a proper isolation level. That might depend, however, on more transaction parameters: https://stackoverflow.com/a/16151690/1199132 – Aritz Apr 23 '18 at 06:27
  • @XtremeBiker Good suggestion. In that case, do I need master/slave configuration for MySQL hosts? master for reads/writes and slave for reads? – realPK Apr 23 '18 at 06:35
  • I think `@Transactional` is always recommended, but especially necessary if you are performing multiple queries, according to this thread: https://github.com/jOOQ/jOOQ/issues/1836#issuecomment-19468504 (it's quite old though so I don't know if it still applies). – jhyot Apr 23 '18 at 13:03
  • @jhyot Thanks for your comment. I am using jOOQ 3.9 and changes on github thread won't apply to my environment. – realPK Apr 23 '18 at 15:04

0 Answers0