会话工厂处理多个数据库连接
我的任务是为不同的数据源创建多个会话工厂。一个数据源将用于读取请求,另一个数据源将用于读写请求。
我们的数据库服务(DAO 的包装)的每个 API 都作为事务执行。单个 API 可以调用具有不同读/写属性的多个 DAO 函数。
执行此任务的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设不同的数据源是不同的数据库。否则,您可以使用带有 @Transactional 注释的 readOnly 属性来管理只读事务。
您可以在 DAO 层中注入两个独立的会话工厂来管理这些事务。或者,您可以使用 AbstractRoutingDataSource。请参阅此处的文章
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