0

Can I use @Transactional and @RestController in the same class without any problem?

@RestController
@RequestMapping("/users")
@Transactional
public class UserController  {

    @Autowired
    private UserRepository userRepository;
    @PersistenceContext
    private EntityManager em;

    @PostMapping
    public ResponseEntity<?> create(@RequestBody @Valid User user, Errors errors) {
    }



}
Dave08
  • 1
  • 1
    You can follow the answer to this question on why you shouldn't annotate your controllers as transactional. https://stackoverflow.com/questions/30378332/spring-boot-controller-transactional-doesnt-work – Onome Sotu Dec 15 '19 at 02:05
  • The issue is still unclear? I see that you have an answer, but it hasn't be accepted nor you commented why it does not solve your problem. – Marcin Aug 02 '21 at 01:04

1 Answers1

0

Yes, you can use. A new transaction for that flow shall be created.

However, this approach is not recommended.

Recommended approach is to use at Service level. Reason, for that aService might call another Service.

Prakash Ayappan
  • 455
  • 2
  • 8