简体   繁体   English

Hibernate + Spring使用多个数据源?

[英]Hibernate + Spring using multiple datasources?

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. 我是Spring和Hibernate的新手,我想我只是想知道如何处理这个问题。 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... 我猜我将不得不创建一个新的dataSource bean,一个新的sessionFactory和一个transactionManager ......也许......但......

  1. I only want the connection to the external data source to be available when the user is specifically "exporting". 当用户专门“导出”时,我只希望连接到外部数据源。

  2. 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? 当我为导出过程实例化DAO时,如何告诉Spring注入适当的sessionFactory? (I'm autowiring through constructors) Should I programatically create my session factory (etc) and then manually instantiate my DAO? (我通过构造函数自动装配)我应该以编程方式创建会话工厂(等),然后手动实例化我的DAO吗? If so, will this "override" the autowire annotation? 如果是这样,这将“覆盖”autowire注释吗?

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! 谢谢!

Spring fortunately already has a solution for this: AbstractRoutingDataSource. Spring幸运的是已经有了解决方案: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. 它基本上充当多个DataSource的Facade,并允许您对其进行子类化并实现您需要的任何逻辑来决定应该使用哪个DataSource。 Some details are here: 一些细节在这里:

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. 这允许您的DataSource查找逻辑只在一个地方处理。 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. 除了需要将AbstractRoutingDataSource的子类注入Hibernate SessionFactory之外,不需要调整DAO层和SessionFactory。

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><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. 如果两个数据源都由您的应用服务器管理,则事务管理器可能在两个数据源之间共享,但听起来两个数据源之间的事务完整性不是您的要求,并且每个数据源都有单独的事务就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM