I have been using a repository object like below in a single threaded application till now.
@Service
class MyEntityService {
@Autowired
private MyEntityRepository repository;
}
The repository looks like below -
import com.myorg.mypackage.entities.facebook.MyEntity;
import org.springframework.data.repository.CrudRepository;
public interface MyEntityRepository extends CrudRepository<MyEntity, String> {
MyEntity findByFacebookPageId(String facebookPageId);
}
I plan to start using multiple threads via Executor service and write to database concurrently. Do I need to create multiple instances of the repository or a single one will do?
Generally speaking, spring jpa is thread safe. It all comes from the entity manager which is obtained from the entity manager factories in spring. Spring injects a new entity manager in each thread so it makes the entity manager look thread safe.
check this nice article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With