org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource

DynamicDataSource-CSDN博客

/** Copyright 2002-2020 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.jdbc.datasource.lookup;import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;import javax.sql.DataSource;import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;/*** Abstract {@link javax.sql.DataSource} implementation that routes {@link #getConnection()}* calls to one of various target DataSources based on a lookup key. The latter is usually* (but not necessarily) determined through some thread-bound transaction context.** @author Juergen Hoeller* @since 2.0.1* @see #setTargetDataSources* @see #setDefaultTargetDataSource* @see #determineCurrentLookupKey()*/
public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {@Nullableprivate Map<Object, Object> targetDataSources;@Nullableprivate Object defaultTargetDataSource;private boolean lenientFallback = true;private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();@Nullableprivate Map<Object, DataSource> resolvedDataSources;@Nullableprivate DataSource resolvedDefaultDataSource;/*** Specify the map of target DataSources, with the lookup key as key.* The mapped value can either be a corresponding {@link javax.sql.DataSource}* instance or a data source name String (to be resolved via a* {@link #setDataSourceLookup DataSourceLookup}).* <p>The key can be of arbitrary type; this class implements the* generic lookup process only. The concrete key representation will* be handled by {@link #resolveSpecifiedLookupKey(Object)} and* {@link #determineCurrentLookupKey()}.*/public void setTargetDataSources(Map<Object, Object> targetDataSources) {this.targetDataSources = targetDataSources;}/*** Specify the default target DataSource, if any.* <p>The mapped value can either be a corresponding {@link javax.sql.DataSource}* instance or a data source name String (to be resolved via a* {@link #setDataSourceLookup DataSourceLookup}).* <p>This DataSource will be used as target if none of the keyed* {@link #setTargetDataSources targetDataSources} match the* {@link #determineCurrentLookupKey()} current lookup key.*/public void setDefaultTargetDataSource(Object defaultTargetDataSource) {this.defaultTargetDataSource = defaultTargetDataSource;}/*** Specify whether to apply a lenient fallback to the default DataSource* if no specific DataSource could be found for the current lookup key.* <p>Default is "true", accepting lookup keys without a corresponding entry* in the target DataSource map - simply falling back to the default DataSource* in that case.* <p>Switch this flag to "false" if you would prefer the fallback to only apply* if the lookup key was {@code null}. Lookup keys without a DataSource* entry will then lead to an IllegalStateException.* @see #setTargetDataSources* @see #setDefaultTargetDataSource* @see #determineCurrentLookupKey()*/public void setLenientFallback(boolean lenientFallback) {this.lenientFallback = lenientFallback;}/*** Set the DataSourceLookup implementation to use for resolving data source* name Strings in the {@link #setTargetDataSources targetDataSources} map.* <p>Default is a {@link JndiDataSourceLookup}, allowing the JNDI names* of application server DataSources to be specified directly.*/public void setDataSourceLookup(@Nullable DataSourceLookup dataSourceLookup) {this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());}@Overridepublic void afterPropertiesSet() {if (this.targetDataSources == null) {throw new IllegalArgumentException("Property 'targetDataSources' is required");}this.resolvedDataSources = CollectionUtils.newHashMap(this.targetDataSources.size());this.targetDataSources.forEach((key, value) -> {Object lookupKey = resolveSpecifiedLookupKey(key);DataSource dataSource = resolveSpecifiedDataSource(value);this.resolvedDataSources.put(lookupKey, dataSource);});if (this.defaultTargetDataSource != null) {this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);}}/*** Resolve the given lookup key object, as specified in the* {@link #setTargetDataSources targetDataSources} map, into* the actual lookup key to be used for matching with the* {@link #determineCurrentLookupKey() current lookup key}.* <p>The default implementation simply returns the given key as-is.* @param lookupKey the lookup key object as specified by the user* @return the lookup key as needed for matching*/protected Object resolveSpecifiedLookupKey(Object lookupKey) {return lookupKey;}/*** Resolve the specified data source object into a DataSource instance.* <p>The default implementation handles DataSource instances and data source* names (to be resolved via a {@link #setDataSourceLookup DataSourceLookup}).* @param dataSource the data source value object as specified in the* {@link #setTargetDataSources targetDataSources} map* @return the resolved DataSource (never {@code null})* @throws IllegalArgumentException in case of an unsupported value type*/protected DataSource resolveSpecifiedDataSource(Object dataSource) throws IllegalArgumentException {if (dataSource instanceof DataSource) {return (DataSource) dataSource;}else if (dataSource instanceof String) {return this.dataSourceLookup.getDataSource((String) dataSource);}else {throw new IllegalArgumentException("Illegal data source value - only [javax.sql.DataSource] and String supported: " + dataSource);}}/*** Return the resolved target DataSources that this router manages.* @return an unmodifiable map of resolved lookup keys and DataSources* @throws IllegalStateException if the target DataSources are not resolved yet* @since 5.2.9* @see #setTargetDataSources*/public Map<Object, DataSource> getResolvedDataSources() {Assert.state(this.resolvedDataSources != null, "DataSources not resolved yet - call afterPropertiesSet");return Collections.unmodifiableMap(this.resolvedDataSources);}/*** Return the resolved default target DataSource, if any.* @return the default DataSource, or {@code null} if none or not resolved yet* @since 5.2.9* @see #setDefaultTargetDataSource*/@Nullablepublic DataSource getResolvedDefaultDataSource() {return this.resolvedDefaultDataSource;}@Overridepublic Connection getConnection() throws SQLException {return determineTargetDataSource().getConnection();}@Overridepublic Connection getConnection(String username, String password) throws SQLException {return determineTargetDataSource().getConnection(username, password);}@Override@SuppressWarnings("unchecked")public <T> T unwrap(Class<T> iface) throws SQLException {if (iface.isInstance(this)) {return (T) this;}return determineTargetDataSource().unwrap(iface);}@Overridepublic boolean isWrapperFor(Class<?> iface) throws SQLException {return (iface.isInstance(this) || determineTargetDataSource().isWrapperFor(iface));}/*** Retrieve the current target DataSource. Determines the* {@link #determineCurrentLookupKey() current lookup key}, performs* a lookup in the {@link #setTargetDataSources targetDataSources} map,* falls back to the specified* {@link #setDefaultTargetDataSource default target DataSource} if necessary.* @see #determineCurrentLookupKey()*/protected DataSource determineTargetDataSource() {Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");Object lookupKey = determineCurrentLookupKey();DataSource dataSource = this.resolvedDataSources.get(lookupKey);if (dataSource == null && (this.lenientFallback || lookupKey == null)) {dataSource = this.resolvedDefaultDataSource;}if (dataSource == null) {throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");}return dataSource;}/*** Determine the current lookup key. This will typically be* implemented to check a thread-bound transaction context.* <p>Allows for arbitrary keys. The returned key needs* to match the stored lookup key type, as resolved by the* {@link #resolveSpecifiedLookupKey} method.*/@Nullableprotected abstract Object determineCurrentLookupKey();}

==分析,启动==

==分析,网页请求==

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/257709.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

样本数量对问卷信度效度分析的影响及应对策略

问卷调研是一种常见的数据收集方法。明确问卷的真实性和效率是保证其靠谱性和有效性的重要一步。但问卷的真实性和品质会受到样本数量的影响吗&#xff1f; 一、问卷信度的认识 1、信度的概念和重要性:在问卷实验中&#xff0c;信度是指问卷测量值的稳定性和一致性。高信度代…

教育心得整理

压抑使人反抗&#xff0c;反抗就是报复&#xff0c;报复就会引起犯罪。要消灭犯罪&#xff0c;我们必须杜绝引起孩子报复心理的行为&#xff0c;更重要的是&#xff0c;我们一定要对孩子表现出来爱与尊重 限制批评的次数限制每次批评的范围限制每次批评的强度 当彼此的信任和…

Ubuntu 安装 CUDA 和 cuDNN 详细步骤

我的Linux系统背景&#xff1a; 系统和驱动都已安装。 系统是centos 8。查看自己操作系统的版本信息&#xff1a;cat /etc/issue或者是 cat /etc/lsb-release 用nvidia-smi可以看到显卡驱动和可支持的最高cuda版本&#xff0c;我的是12.2。驱动版本是535.129.03 首先&#…

Linux学习笔记3 xshell(lnmp)

xshell能连接虚拟机的前提是真机能够ping通虚拟机网址 装OpenSSL依赖文件 [rootlocalhost nginx-1.12.2]# yum -y install openssl pcre-devel 依赖检测[rootlocalhost nginx-1.12.2]# ./configure [rootlocalhost nginx-1.12.2]# yum -y install zlib [rootlocalhost n…

Linus:我休假的时候也会带着电脑,否则会感觉很无聊

目录 Linux 内核最新版本动态 关于成为内核维护者 代码好写&#xff0c;人际关系难处理 内核维护者老龄化 内核中 Rust 的使用 关于 AI 的看法 参考 12.5-12.6 日&#xff0c;Linux 基金会组织的开源峰会&#xff08;OSS&#xff0c;Open Source Summit&#xff09;在日…

Java实现TCP一对一通信,实现UDP群聊通信

TCP一对一通信: 实现服务端对话框&#xff1a; 其中可自由更改对话框的样式 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class QqMain extends JFrame implements ActionListener{public static void …

C++_命名空间(namespace)

目录 1、namespace的重要性 2、 namespace的定义及作用 2.1 作用域限定符 3、命名空间域与全局域的关系 4、命名空间的嵌套 5、展开命名空间的方法 5.1 特定展开 5.1 部分展开 5.2 全部展开 结语&#xff1a; 前言&#xff1a; C作为c语言的“升级版”&#xff0c;其在…

Constraining Async Clock Domain Crossing

Constraining Async Clock Domain Crossing 我们在normal STA中只会去check 同步clock之间的timing,但是design中往往会存在很多CDC paths,这些paths需要被正确约束才能保证design function正确,那么怎么去约束这些CDC paths呢? 以下面的design为例,如下图所示 这里clk…

Linux查看openSSL版本

命令&#xff1a;openssl version

RHEL8_Linux计划任务

本章主要介绍如何创建计划任务 使用 at 创建计划任务使用 crontab 创建计划任务 有时需要在某个指定的时间执行一个操作&#xff0c;此时就要使用计划任务了。计划任务有两种&#xff1a;一个是at计划任务&#xff0c;另一个是 crontab计划任务。 下面我们分别来看这两种计划任…

经纬恒润以太网网关,智能时代网络通关

汽车产业新四化步伐持续加速&#xff0c;智能网联逐渐成为整车标配&#xff0c;随着近年来相关政策频出以及对网联需求和功能的深度挖掘与发展&#xff0c;中国本土市场及本土供应商在这场新浪潮中逐渐走向C位。经纬恒润深耕智能网联领域多年&#xff0c;先后推出四代网关产品&…

用Rust刷LeetCode之66 加一

66. 加一[1] 难度: 简单 func plusOne(digits []int) []int { length : len(digits) // 从最低位开始遍历&#xff0c;逐位加一 for i : length - 1; i > 0; i-- { if digits[i] < 9 { digits[i] return digits } d…