0

I have JpaRepository<Person, UUID> which connects to the database. Is it possible to create a different connection pool to the same repository but from different controllers?

From controller A I want to have a separate connection pool than in controller B.

I think I should create two DataSource which points to same database, but I don't know how to point which controller uses which DataSource. Maybe I should create two Repositories?

8
  • What is your requirement, why can't you simply inject your repository in each controller? Commented Oct 3, 2018 at 10:25
  • I need to separate the connection pool between two controllers to avoid situation when first will lead to starvation of second. Commented Oct 3, 2018 at 10:26
  • 1
    You would need not only duplicate datasources, also entity managers, repositories etc. looks like you are trying to optimize something you shouldn't be optimizing. If something starves how could a second pool to the same database help. Just increase the size of the initial connection pool and write proper logic so that your connection don't starve. If that is the case you are keeping connections open far too long. Commented Oct 3, 2018 at 10:29
  • @M.Deinum Yes, you are right. I will think about not keeping connections too long instead of creating separate pools. Commented Oct 3, 2018 at 10:38
  • 1
    If that is what you want you might want to use an AbstractRoutingDataSource for that. Commented Oct 3, 2018 at 10:49

2 Answers 2

2

Consider going through the blog https://www.baeldung.com/spring-data-jpa-multiple-databases to understand better how you can configure different connection pool and entity manager for different datasources. For your use case, we can point both to the same datasource.

Then, go about creating two different repositories for the same and through @EnableJpaRepositories annotation, point the two different transaction manager to the two different repository class paths.

Say your two different repositories are repositoryA and repositoryB, then in Controller A, inject repositoryA and in Controller B, inject repositoryB.

Sign up to request clarification or add additional context in comments.

Comments

0

To use several connection pools you have to define your own Bean for each pooled database connection by using multiple data sources.

Follow this tutorial to do so https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.