简体   繁体   English

使用Spring,JPA和Hibernate访问Jboss中配置的多个数据库/数据源

[英]Using Spring, JPA with Hibernate to access multiple databases/datasources configured in Jboss

I have a requirement where i need to configure a Spring based application to work with two databases. 我有一个要求,我需要配置一个基于Spring的应用程序来使用两个数据库。 We have two databases, one that we use to keep the live data and the other database is used as a datawarehouse and contains archived data (which has the exact structure as the Live db). 我们有两个数据库,一个用于保存实时数据,另一个数据库用作数据仓库,包含存档数据(其结构与Live db完全相同)。

To keep it simple, assume that there is a request to search for a product. 为简单起见,假设有搜索产品的请求。 What the application should do is to search for the product details in the Live database and if not found it will check the archive database. 应用程序应该做的是在Live数据库中搜索产品详细信息,如果没有找到,它将检查存档数据库。

If i need to configure such a setup, do i still need to configure to datasources and would the search code have to use the first datasource to check the live database and if not found it will run another query using the archive database? 如果我需要配置这样的设置,我是否仍然需要配置数据源,搜索代码是否必须使用第一个数据源来检查实时数据库,如果没有找到它将使用存档数据库运行另一个查询?

The above is probably doable but i am wondering whether there is a better way of doing this. 以上可能是可行的,但我想知道是否有更好的方法来做到这一点。 For example, is it possible for the application to work on a single datasource even though behind the scenes it actually works with two databases? 例如,应用程序是否可以在单个数据源上工作,即使它在幕后实际上与两个数据库一起工作?

The application is based on Spring, JPA/Hibernate, SOAP and Mysql database and Jboss 7 as the application server. 该应用程序基于Spring,JPA / Hibernate,SOAP和Mysql数据库以及Jboss 7作为应用程序服务器。

Any examples showing how this is configured using Spring and Jboss would be very useful. 显示如何使用Spring和Jboss配置它的任何示例都非常有用。

Thanks 谢谢

Spring has exactly what you want - the AbstractRoutingDataSource . Spring正是你想要的 - AbstractRoutingDataSource See this blog post on how to use it. 请参阅此博客文章 ,了解如何使用它。 In your case, you need to switch the datasource during one request, so you'll need to have 2 transactions, switching the datasource between them by changing the datasource indicator on the ThreadLocal : 在您的情况下,您需要在一个请求期间切换数据源,因此您需要有2个事务,通过更改ThreadLocal上的数据源指示符在它们之间切换数据源:

  1. For these DAOs, demarcate the wrapping Service-layer either with distinct packages, class names, or method names 对于这些DAO,使用不同的包,类名或方法名来划分包装Service层
  2. Indicate to Spring that the Service-layer method calls should run in their own transactional contexts by annotating with @Transactional(propogation=Propogation.REQUIRES_NEW) 通过使用@Transactional(propogation=Propogation.REQUIRES_NEW)注释,指示Spring,Service-layer方法调用应该在自己的事务上下文中运行@Transactional(propogation=Propogation.REQUIRES_NEW)
  3. Create an Aspect (using AspectJ annotation @Aspect ) to fire around the service-layer method calls (using @Around ) to set the ThreadLocal value before the method call, and to unset it afterwards 创建一个Aspect(使用AspectJ注释@Aspect )来触发服务层方法调用(使用@Around )在方法调用之前设置ThreadLocal值,然后取消设置它
  4. In the @Controller , simply call the Service-layer methods. @Controller ,只需调用服务层方法即可。 The Aspect will take care of setting the values to indicate which datasource to use, and the AbstractRoutingDataSource will use that datasource in the context of each transaction. Aspect将负责设置值以指示要使用的数据源,而AbstractRoutingDataSource将在每个事务的上下文中使用该数据源。

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

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