当前位置: 首页 > news >正文

常州网站设计公司动漫设计专业怎么样

常州网站设计公司,动漫设计专业怎么样,怎么查询企业电话,大连电商平台有哪些背景 在项目开发过程中#xff0c;我们可能会遇到一个场景#xff1a;某个类型数据源有多个数据源实例#xff0c;需要我们按照不同的请求切换到不同数据源去。 而目前绝大多数java应用都是基于Spring框架来开发#xff0c;我们很多时候相关的数据源连接都是交给了Spring框…背景 在项目开发过程中我们可能会遇到一个场景某个类型数据源有多个数据源实例需要我们按照不同的请求切换到不同数据源去。 而目前绝大多数java应用都是基于Spring框架来开发我们很多时候相关的数据源连接都是交给了Spring框架去管理这就需要Spring能够支持动态数据源切换。 方案 Spring中预留了这个接口通过AbstractRoutingDataSource能够动态切换数据源。 public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {这是一个抽象类预留了一个抽象方法 protected abstract Object determineCurrentLookupKey();我们知道数据源一般都会提供一个getConnection方法来获取一个连接在AbstractRoutingDataSource 实现如下 Overridepublic Connection getConnection() throws SQLException {return determineTargetDataSource().getConnection();}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;}可以看到AbstractRoutingDataSource 获取连接的主要逻辑就是通过determineCurrentLookupKey获取到一个数据源的关联key然后从resolvedDataSources中去获取。 而resolvedDataSources的初始化则放在afterPropertiesSet中 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);}}这里起始就是通过targetDataSources中指定的数据源复制到resolvedDataSources 中去。因此如果多数源是固定的那么只需要实现determineCurrentLookupKey方法即可。但是如果多数据源不固定比如可能会有数据源的变更那么这种实现是不能够支持因为这种实现从服务启动的视乎后续数据源就不能发生变更这需要我们自己实现determineTargetDataSource. 下面是一个参考实现 public class DataSourceContextHolder {private static final ThreadLocalString DATASOURCE_CONTEXT_KEY_HOLDER new ThreadLocal();public static void switchDataSource(String key){log.info(Switch to data source: key);DATASOURCE_CONTEXT_KEY_HOLDER.set(key);}public static String getDataSourceKey(){return DATASOURCE_CONTEXT_KEY_HOLDER.get() ;}}public class DynamicDataSource extends AbstractRoutingDataSource {private MapObject, Object targetDataSources new HashMap();private MapObject, DataSource dataSources new HashMap();public DynamicDataSource (){super.setDefaultTargetDataSource(null);super.setTargetDataSources(targetDataSources);super.afterPropertiesSet();}Overrideprotected DataSource determineTargetDataSource() {Object dataSourceKey determineCurrentLookupKey();return dataSources.get(dataSourceKey);}Overrideprotected Object determineCurrentLookupKey() {return DataSourceContextHolder .getDataSourceKey();}public synchronized void addDataSource(String key, DataSource dataSource){targetDataSources.put(key,dataSource);dataSources.put(key,dataSource);log.info(add tenant dynamic dataSource for tenantId {} ,key);} }这样我们通过DataSourceContextHolder 来调整当前线程关联的数据源。
http://www.sczhlp.com/news/197587/

相关文章:

  • 扶风网站开发电商网站开发报价
  • 高德开放平台wordpress seo自定义
  • 网络系统架构图seo服务商找行者seo
  • 各大网站网络推广的收费网站怎样制作
  • 自适应网站做网站的时候字体应该多大
  • 网路营销网站策划书搜索引擎推广简称
  • 网站建设前的规划wordpress文章参数
  • 电子商务平台网站建设方式手机网站建设价格低
  • 大数据分析之MySQL学习1
  • 2025年GEO(AI搜索优化)源头厂家终极口碑推荐榜
  • 2025年GEO(AI搜索优化)源头厂家Top10权威推荐榜
  • 10.15
  • 网站设计机构图汕头企业模板建站
  • 中国建设银行网站首页u盾登入做p2p网站 人员配置
  • 代理登录网站免费制作一个企业网站
  • 网页设计与网站制作三个页面的网页作品模板
  • 深圳网站制作wordpress默认链接
  • 表白网站制作在线首京建设投资引导基金网站
  • 建筑工程网络计划图怎么编制网站框架优化
  • ipv6跟做网站有关吗wordpress加悬浮
  • 广东官网网站建设企业seo实战密码第三版pdf
  • 适合设计师看的设计网站0经验自己做网站
  • 百度网站的结构项目宣传网站模板
  • 一家专门做房产特卖的网站网站描述
  • 淮安哪个做网站好点上海ktv最新通知
  • 网站建设及推广外包网站开发常用开发语言
  • 上海网站备案注销公司网站用什么系统
  • 广州设计网站培训班移动互联网开发实践
  • 百度上面做企业网站怎么做济南网站关键词优化公司
  • 宁波网站制作哪家全面南京专门做网站