使用 Spring、JPA 和 Hibernate 访问 Jboss 中配置的多个数据库/数据源 [英] Using Spring, JPA with Hibernate to access multiple databases/datasources configured in Jboss

查看:23
本文介绍了使用 Spring、JPA 和 Hibernate 访问 Jboss 中配置的多个数据库/数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需求,我需要配置一个基于 Spring 的应用程序来处理两个数据库.我们有两个数据库,一个用于保存实时数据,另一个数据库用作数据仓库并包含存档数据(具有与 Live db 完全相同的结构).

I have a requirement where i need to configure a Spring based application to work with two databases. 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 数据库中搜索产品详细信息,如果没有找到,它将检查存档数据库.

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.

如果我需要配置这样的设置,我是否还需要配置到数据源,搜索代码是否必须使用第一个数据源来检查实时数据库,如果没有找到,它将使用存档数据库运行另一个查询?

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?

该应用基于Spring、JPA/Hibernate、SOAP和Mysql数据库以及Jboss 7作为应用服务器.

The application is based on Spring, JPA/Hibernate, SOAP and Mysql database and Jboss 7 as the application server.

任何显示如何使用 Spring 和 Jboss 进行配置的示例都非常有用.

Any examples showing how this is configured using Spring and Jboss would be very useful.

谢谢

推荐答案

Spring 正是您想要的 - AbstractRoutingDataSource.有关如何使用它的信息,请参阅这篇博文.在您的情况下,您需要在一个请求期间切换数据源,因此您需要有 2 个事务,通过更改 ThreadLocal 上的数据源指示器在它们之间切换数据源:

Spring has exactly what you want - the 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:

  1. 对于这些 DAO,使用不同的包、类名或方法名来划分包装服务层
  2. 通过使用 @Transactional(propogation=Propogation.REQUIRES_NEW)
  3. 进行注释,向 Spring 指示服务层方法调用应该在它们自己的事务上下文中运行
  4. 创建一个方面(使用 AspectJ 注释 @Aspect)来触发服务层方法调用(使用 @Around)来设置 ThreadLocal 方法调用前的值,然后取消设置
  5. @Controller 中,只需调用服务层方法.方面将负责设置值以指示要使用的数据源,AbstractRoutingDataSource 将在每个事务的上下文中使用该数据源.
  1. For these DAOs, demarcate the wrapping Service-layer either with distinct packages, class names, or method names
  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)
  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
  4. In the @Controller, simply call the Service-layer methods. 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.

这篇关于使用 Spring、JPA 和 Hibernate 访问 Jboss 中配置的多个数据库/数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆