Java Hibernate + Spring 使用多个数据源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/860918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate + Spring using multiple datasources?
提问by Boden
I'm working on a web application that uses Spring MVC 2.5 and Hibernate.
我正在开发一个使用 Spring MVC 2.5 和 Hibernate 的 Web 应用程序。
One of the requirements of the application is that it must be able to export some objects to an external database. I figure I might as well use my existing data layer and just save the objects to the external source.
应用程序的要求之一是它必须能够将某些对象导出到外部数据库。我想我还不如使用我现有的数据层并将对象保存到外部源。
I'm new to Spring and Hibernate, and I guess I'm just wondering how I should approach this. Right now everything is automatically wired up through annotations. I'm guessing I'll have to create a new dataSource bean, and a new sessionFactory, and a transactionManager...maybe...but...
我是 Spring 和 Hibernate 的新手,我想我只是想知道我应该如何处理这个问题。现在一切都通过注释自动连接起来。我猜我必须创建一个新的 dataSource bean、一个新的 sessionFactory 和一个 transactionManager ......也许......但是......
I only want the connection to the external data source to be available when the user is specifically "exporting".
Is autowiring going to get in my way? How can I tell Spring to inject the appropriate sessionFactory when I instantiate a DAO for my export process? (I'm autowiring through constructors) Should I programatically create my session factory (etc) and then manually instantiate my DAO? If so, will this "override" the autowire annotation?
我只希望在用户专门“导出”时与外部数据源的连接可用。
自动装配会妨碍我吗?当我为导出过程实例化 DAO 时,如何告诉 Spring 注入适当的 sessionFactory?(我通过构造函数自动装配)我应该以编程方式创建我的会话工厂(等),然后手动实例化我的 DAO?如果是这样,这会“覆盖”自动装配注释吗?
I guess I don't need answers to the above questions specifically if someone can just step me through the basic process of getting something like this to work. Thanks!
我想我不需要特别回答上述问题,如果有人可以引导我完成让这样的事情发挥作用的基本过程。谢谢!
回答by skaffman
Configuring multiple data sources and session factories in your spring context will not itself be a problem, but it does make autowiring less attractive.
在 spring 上下文中配置多个数据源和会话工厂本身不会成为问题,但它确实降低了自动装配的吸引力。
You could use the @Qualifier
annotation to tell the autowiring which one to choose, but I'd suggest not using autowiring, and instead explicitly injecting the correct data source and session factory using <property>
or <constructor-arg>
.
您可以使用@Qualifier
注释来告诉自动装配选择哪个,但我建议不要使用自动装配,而是使用<property>
or显式注入正确的数据源和会话工厂<constructor-arg>
。
The transaction manager could potentially be shared between both data sources, if both data sources are managed by your app server, but it sounds like having transactional integrity across both data sources is not a requirement for you, and that having separate transactions for each data source would be enough.
如果两个数据源都由您的应用程序服务器管理,则事务管理器可能会在两个数据源之间共享,但听起来您不需要跨两个数据源的事务完整性,并且每个数据源都有单独的事务就足够了。
回答by cliff.meyers
Spring fortunately already has a solution for this: AbstractRoutingDataSource. It basically acts as a Facade for multiple DataSources and allows you to subclass it and implement whatever logic you need to decide which DataSource should be used. Some details are here:
幸运的是,Spring 已经为此提供了解决方案:AbstractRoutingDataSource。它基本上充当多个数据源的外观,并允许您对其进行子类化并实现您需要的任何逻辑来决定应该使用哪个数据源。一些细节在这里:
http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/
http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/
This allows your DataSource lookup logic to be handled in exactly one place. Your DAO layer and SessionFactory do not need to be adjusted, except that you need to inject your subclass of AbstractRoutingDataSource into the Hibernate SessionFactory.
这允许您的数据源查找逻辑在一个地方处理。您的 DAO 层和 SessionFactory 不需要调整,只是您需要将您的 AbstractRoutingDataSource 子类注入到 Hibernate SessionFactory 中。