2

I have a task to create multiple session factories for different Data Sources. One data source will be used for read requests and another for read-write requests.

Each API of our Database Service(a wrapper over DAO) is executed as a transaction. Single API may call multiple DAO functions having different Read/Write property.

What is the best possible way to perform this task?

1

1 Answer 1

1

I am assuming that the different datasources are different DBs . Else you could use the readOnly attribute with the @Transactional annotation to manage the read only transactions.

You could inject two separate session factories in the DAO layer to manage these transactions . Alternatively , you could use AbstractRoutingDataSource . See a write up on this here

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

6 Comments

Yes, different datasources are different DBs.
Then you could inject different sessionfactories . If you have different transaction managers, then you could use @Transactional("TxManagerName") . You could wire this all up with AbstractRoutingDataSource . But is you you plan to work across different transactional datasource within the same method - i.e , if one DB rollback should affect the other , consider using XA datasource .
This is really helpful. Could you please tell me the pros and cons of connecting to multiple DBs from a single API? Apart from this, might be naive but what is the way to use global transaction (XA datasource) in Spring?
@tyro - Please upvote(click on the up arrow)/check the tick mark if you are happy with the answer
Vote Up requires 15 reputation :( Could you please answer the previous question?
|

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.