问题描述
对于每个客户端,我有单独的数据库,但每个客户端的业务逻辑和表都是相同的.我想要每个客户端的公共服务和 dao 层.在 dao 中,我根据登录的用户客户端选择数据源.在 @Transactional
中,我必须传递事务管理器的 bean id.如何使用@Transactional
注解制作公共服务层.
For each client, I have separate databases but business logic and tables are same for each client. I want common service and dao layer for each client. In dao, I select datasource based on logged user client. In @Transactional
, I have to pass bean id of transaction manager. How to make common service layer with @Transactional
annotation.
同样的问题在这里
但没人回复
推荐答案
您不需要在多个事务管理器之间进行配置和切换来实现您的最终目标.而是使用 Spring 提供的 org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
机制.
You do not need to configure and switch between multiple transaction managers to accomplish your end goal. Instead use the Spring provided org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
mechanism.
可以在此处找到详细示例:
Detailed examples can be found here :
- https://spring.io/blog/2007/01/23/动态数据源路由/
- http://howtodoinjava.com/spring/spring-orm/spring-3-2-5-abstractroutingdatasource-example/
这篇关于spring 多个事务管理器,运行时选择一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!