春季数据JPA |动态运行时多数据库连接 [英] Spring Data JPA | Dynamic runtime multiple database connection

查看:43
本文介绍了春季数据JPA |动态运行时多数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例:

在 JBoss 服务器启动期间,已经使用 Spring Data JPA 配置(基于 xml 的方法)建立了一个永久数据库连接.

现在当应用程序已经启动并运行时,需要连接到多个数据库并且连接字符串是动态的,在运行时可用.

如何使用 Spring Data JPA 实现这一目标?

解决方案

切换数据源的一种方法是定义一个配置了运行时"数据源的运行时"存储库.但这将使客户端代码知道不同的存储库:

package com...runtime.repository;公共接口 RuntimeRepo 扩展 JpaRepository<OBJECT, ID>{ ... }@配置@EnableJpaRepositories(transactionManagerRef="runtimeTransactionManager",entityManagerFactoryRef="runtimeEmfBean")@EnableTransactionManagement公共类 RuntimeDatabaseConfig {@Bean 公共数据源 runtimeDataSource() {DriverManagerDataSource rds = new DriverManagerDataSource();//设置驱动、用户名、密码、url返回 rds;}@Bean public LocalContainerEntityManagerFactoryBean runtimeEmfBean() {LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();factoryBean.setDataSource(runtimeDataSource());//设置 JpaVendorAdapter, jpaProperties,返回 factoryBean;}@Bean 公共 PlatformTransactionManager runtimeTransactionManager() {JpaTransactionManager jtm = new JpaTransactionManager();jtm.setEntityManagerFactory(runtimeEmfBean());返回 jtm;}}

我已经合并了代码以节省空间;您可以在不同的文件中定义 javaconfig 和 repo 接口,但在同一个包中.

要使客户端代码与存储库类型无关,请实现您自己的存储库工厂,将存储库工厂自动连接到客户端代码中,并在返回特定存储库实现之前让您的存储库工厂检查应用程序状态.>

Use Case:

During JBoss server startup, one permanent database connection is already made using Spring Data JPA configurations(xml based approach).

Now when application is already up and running, requirement is to connect to multiple Database and connection string is dynamic which is available on run-time.

How to achieve this using Spring Data JPA?

解决方案

One way to switch out your data source is to define a "runtime" repository that is configured with the "runtime" data source. But this will make client code aware of the different repos:

package com...runtime.repository;

public interface RuntimeRepo extends JpaRepository<OBJECT, ID> { ... }

@Configuration
@EnableJpaRepositories(
    transactionManagerRef="runtimeTransactionManager", 
    entityManagerFactoryRef="runtimeEmfBean")
@EnableTransactionManagement
public class RuntimeDatabaseConfig {

    @Bean public DataSource runtimeDataSource() {
        DriverManagerDataSource rds = new DriverManagerDataSource();
        // setup driver, username, password, url
        return rds;
    }

    @Bean public LocalContainerEntityManagerFactoryBean runtimeEmfBean() {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(runtimeDataSource());
        // setup JpaVendorAdapter, jpaProperties, 
        return factoryBean;
    }

    @Bean public PlatformTransactionManager runtimeTransactionManager() {
        JpaTransactionManager jtm = new JpaTransactionManager();
        jtm.setEntityManagerFactory(runtimeEmfBean());
        return jtm;
    }
}

I have combined the code to save space; you would define the javaconfig and the repo interface in separate files, but within the same package.

To make client code agnostic of the repo type, implement your own repo factory, autowire the repo factory into client code and have your repo factory check application state before returning the particular repo implementation.

这篇关于春季数据JPA |动态运行时多数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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