How to have thread safe controller in spring boot How to have thread safe controller in spring boot multithreading multithreading

How to have thread safe controller in spring boot


Your controller is a singleton by default and your service is singleton by default too.

Therefore in order to make them thread safe you have to make sure that the operations that take place inside the service must be thread safe, in case of changing the state of an object inside the service ie. a list.

In case of using a rdbms then you have a transaction related problem.

If you use spring and Jpa, the transaction manager will take care for your updates provided that you use @Transactional. In case of plain jdbc method then you can either use pure jdbc and do the transaction handling on your own or use spring-jdbc that comes with a transaction manager.

If you want the database rows not to be changed in case of a write in progress then you have to take into consideration row-locking related mechanisms. – gkatzioura Feb 7 at 15:23

In case of JPA using @Transactional will do the work. However depending on your application you might have to consider locking. Check this article on locking with jpa.


Controllers are singletons, therefore they should be implemented in a thread safe manner.

Design your application in a way that controllers are stateless. Add transactional support in your @Repository layer.

Example:

public class GenericRepository<T, Serializable> { @Transactional public void save(T object) {  // save user }}

You could use Spring declarative transaction management mechanism. The @Transactional annotation itself defines the scope of a single database transaction.


Your controller looks thread safe. As there is no instance variable storing the state. User object will be different for each request and will be resolved by the MVC framework.