如何使用Spring来管理到多个数据库的连接 [英] How to use Spring to manage connection to multiple databases

查看:113
本文介绍了如何使用Spring来管理到多个数据库的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读其他主题,但没有找到一个好的和明确的答案

I have read other topics but didn't find a good and clear answer

我想要的是开发一个网络应用程序,它能够:

What I'm trying to is to develop a web app which is able to:

1)在单独的UI数据库中记录/跟踪用户事件,我们通过hibernate使用相同的数据库模式连接(也许将connectionString保存到不同的oracle数据库)

1) Log/track user events in a seperate UI database which we connect via hibernate with the same database schema (maybe save connectionString to the different oracle databases)

2)在运行时登录时,可以选择一个环境连接到具有相同模式(但不是相同的数据)的三个不同的oracle数据库之一

2) At runtime when you log in you can choose an environment to connect to one of the three different oracle databases that have the same schema (but not the same data)

3)提供正确的DataSource用户名和密码(在哪里得到这个敏感数据?我不会保存在应用程序中的某处)

3) provide the correct DataSource with username and password (where to get this sensitive data? I would not keep this stored somewhere in the application)

我对Spring框架很新。我发现此链接可能是第一个潜在客户。

I'm fairly new to the Spring framework. I found this link which could be a first lead.

任何建议?

同样使用Spring 3.1或3.2,JDBC查询我的oracle数据库和hibernate映射我的UI数据库。这听起来很混乱,所以我有一个图片:

Also using Spring 3.1 or 3.2, JDBC to query to my oracle database and hibernate mapping to my UI database. This sounds quite confusing so I have a picture:

基础设施

推荐答案

只需创建不同的DAO, 。
在您的persistence.xml中,您可以有多个持久单元,每个连接到不同的数据库。

Just create different DAOs each with a separate persistence-unit attached to them. In your persistence.xml you can have multiple persistence-unit's each connecting to a different database.

示例:

public class Dao1{
@PersistenceContext(unitName="Database1")
protected EntityManager entityManager;

public class Dao2{
@PersistenceContext(unitName="Database2")
protected EntityManager entityManager;


<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="Database1">     
        <exclude-unlisted-classes />                    
        <properties>
        </properties>
    </persistence-unit>

    <persistence-unit name="Database2">     
        <exclude-unlisted-classes />                    
        <properties>
        </properties>
    </persistence-unit>
</persistence>

这篇关于如何使用Spring来管理到多个数据库的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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