Spring Boot:如何使用多种模式并动态选择在运行时使用哪种模式 [英] Spring Boot: How to use multiple schemas and dynamically choose which one to use at runtime

查看:70
本文介绍了Spring Boot:如何使用多种模式并动态选择在运行时使用哪种模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下相同的问题,但我想知道答案. Spring Boot:如何使用多种模式并在运行时动态选择用于每个请求的模式

I have the same question as below, but I want to know the answer. Spring Boot: How to use multiple schemas and dynamically choose which one to use for every request at runtime

请帮助我寻找答案

如何建立一个数据库连接并为每个请求指定不同的架构?

How can I have one database connection and specify a different schema for every request?

谢谢.

推荐答案

定义多个数据源是否可行,并根据您的请求更改为具有正确架构的数据源?

Wouldn't it work to have multiple data sources defined, and depending on your request, change to the one with the correct schema?

spring.datasource.url = jdbc:oracle:thin:@//maui:1521/xe
spring.datasource.username = schema1
spring.datasource.password = ...

spring.datasource2.url = jdbc:oracle:thin:@//maui:1521/xe
spring.datasource2.username = schema2
spring.datasource2.password = ..

@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource schema1() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="spring.datasource2")
public DataSource schema2() {
    return DataSourceBuilder.create().build();
}

否则,您需要杀死&重新创建连接以继续使用单个数据源,但这对于您的应用程序来说确实很慢,因为它需要一次又一次地重新连接.最好使用一些NoSQL数据库来实现这种动态数据存储.

Otherwise you'd need to kill & re-create the connection to keep using the singular data source, but that would be really slow for your application since it would need reconnecting again and again. It would be better for you to use some NoSQL database to achieve this sorta dynamic data storage.

这篇关于Spring Boot:如何使用多种模式并动态选择在运行时使用哪种模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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