Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Repository class object across multiple threads

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?

like image 216
comiventor Avatar asked Jul 24 '26 08:07

comiventor


1 Answers

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

like image 185
Amer Qarabsa Avatar answered Jul 27 '26 22:07

Amer Qarabsa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!