Proper way to set up transactions in Spring for different data sources?

I have an application which needs to connect to multiple databases. This is an administrative application which is basically used to manage entries in different databases - we do not need to access multiple databases simultaneously nor do we need any sort of distributed transaction management. Basically one area of the application lets you create gadgets in Database A, and another area of the application lets you configure similar gadgets in Database B. We already have transactions set up and working perfectly when using just one datasource. The configuration looks like so: This sets a pointcut on any execution of any methods within `CompanyService` and associates transaction advice with the pointcut which requires transactions for any methods whose name starts with "create". The transaction advice is associated with a TransactionManager which is tied to the dataSource. When adding a second (or more) datasources, how can I apply the same transaction advice to other datasources? Since the AOP advice can only be associated with one transactionManager, which can only be associated with one dataSource, do I need to set up duplicate transaction advice? If I setup duplicate transaction advice to the same pointcut, won't this mean that any invocations of methods in my `CompanyService` interface will require propogation against **all** of my dataSources? To make my last question a little clearer, I will have multiple beans declared which implement the `CompanyService` interface, and each of these beans will have a separate `CompanyDAO` to access their individual DataSource. I fear that this approach will mean that when the `companyService1` bean is invoked, transaction advice will be triggered on `all` companyService beans/dataSources. Am I going about this the wrong way? **Update:** I've actually tested out the configuration I talked about above (attaching two advisors to the same pointcut), and invoking any method on either individual instance of the `CompanyService` implementation does in fact create new transactions on both dataSources, as expected: DEBUG company.serviceDataSourceTransactionManager - Creating new transaction with name [com.company.service.CompanyService.createCompany]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT DEBUG company.serviceDataSourceTransactionManager - Acquired Connection [connection1 string here...] for JDBC transaction ... DEBUG company.serviceDataSourceTransactionManager - Creating new transaction with name [com.company.service.CompanyService.createCompany]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT DEBUG company.serviceDataSourceTransactionManager - Acquired Connection [connection2 string here...] for JDBC transaction ... DEBUG company.serviceDataSourceTransactionManager - Rolling back JDBC transaction on Connection [connection1 string here...] ... DEBUG company.serviceDataSourceTransactionManager - Rolling back JDBC transaction on Connection [connection2 string here...] This seems like it would cause issues down the road, since either `CompanyService` instance is only ever working with a single DataSource. Is there a better way to configure what I seek to accomplish?

以上就是Proper way to set up transactions in Spring for different data sources?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏