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

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

问题描述

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

为了简单起见,假设有一个搜索产品的请求。应用程序应该做的是在Live数据库中搜索产品详细信息,如果未找到,它将检查存档数据库。

如果我需要配置这样的设置,我是否仍然需要配置数据源,并且搜索代码是否必须使用第一个数据源来检查实时数据库,以及if没有发现它会运行使用存档数据库的另一个查询?

以上可能是可行的,但我想知道是否有更好的方法来做到这一点。例如,应用程序是否可以在单个数据源上工作,即使在幕后它实际上与两个数据库一起工作?

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

显示如何使用Spring和Jboss配置的示例非常有用。

谢谢 解决方案

code> AbstractRoutingDataSource 。有关如何使用它,请参见此博客文章。在你的情况下,你需要在一个请求期间切换数据源,所以你需要有两个事务,通过改变 ThreadLocal 中的数据源指示符来切换它们之间的数据源。 :


  1. 对于这些DAO,使用不同的包,类名称或方法名称来划分包装Service-layer。

  2. 向Spring表明服务层方法调用应该在它们自己的事务上下文中运行,通过注释 @Transactional(propogation = Propogation.REQUIRES_NEW)

  3. 创建一个AspectJ(使用AspectJ注释 @Aspect )来触发服务层方法调用(使用 @Around )在方法调用之前设置 ThreadLocal 值并在之后取消设置

  4. @Controller ,只需调用服务层方法即可。 Aspect将负责设置值以指示要使用哪个数据源,并且 AbstractRoutingDataSource 将在每个事务的上下文中使用该数据源。


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).

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?

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

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

Thanks

解决方案

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. 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天全站免登陆