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?

instanceOfObject
  • 2,936
  • 5
  • 49
  • 85
  • I think this topic will give some help to you. http://stackoverflow.com/questions/2017949/when-to-use-global-transaction-or-use-spring-aop-for-transaction – donnior Jan 04 '12 at 07:07

1 Answers1

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

Aravind A
  • 9,507
  • 4
  • 36
  • 45
  • Yes, different datasources are different DBs. – instanceOfObject Jan 04 '12 at 07:44
  • 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 . – Aravind A Jan 04 '12 at 07:51
  • 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? – instanceOfObject Jan 04 '12 at 10:34
  • @tyro - Please upvote(click on the up arrow)/check the tick mark if you are happy with the answer – Aravind A Jan 04 '12 at 10:38
  • Vote Up requires 15 reputation :( Could you please answer the previous question? – instanceOfObject Jan 04 '12 at 10:50
  • @tyro For XA datasource , you could use JTA transaction manager which supports both XA and non XA transactions .Check out [this](http://www.javaworld.com/javaworld/jw-04-2007/jw-04-xa.html) link for JTA configurations . – Aravind A Jan 04 '12 at 10:52