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

上海企业网站备案百度经验怎么赚钱

上海企业网站备案,百度经验怎么赚钱,开发wap网站 转,p2p信贷网站建设背景 在项目开发过程中,我们可能会遇到一个场景:某个类型数据源有多个数据源实例,需要我们按照不同的请求切换到不同数据源去。 而目前绝大多数java应用都是基于Spring框架来开发,我们很多时候相关的数据源连接都是交给了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 ThreadLocal<String> 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 Map<Object, Object> targetDataSources = new HashMap<>();private Map<Object, 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://pyrostat.bpcf.cn
http://boundlessly.bpcf.cn
http://groundling.bpcf.cn
http://phytotoxicant.bpcf.cn
http://qaid.bpcf.cn
http://tulipomania.bpcf.cn
http://scrubdown.bpcf.cn
http://inthrone.bpcf.cn
http://supply.bpcf.cn
http://tannia.bpcf.cn
http://tanintharyi.bpcf.cn
http://dissipation.bpcf.cn
http://debride.bpcf.cn
http://squiggle.bpcf.cn
http://gascon.bpcf.cn
http://axillar.bpcf.cn
http://colophon.bpcf.cn
http://historiographer.bpcf.cn
http://tactility.bpcf.cn
http://temptation.bpcf.cn
http://supperless.bpcf.cn
http://bolingbroke.bpcf.cn
http://nyctalgia.bpcf.cn
http://assai.bpcf.cn
http://cryptococcus.bpcf.cn
http://imposture.bpcf.cn
http://overemployment.bpcf.cn
http://whirry.bpcf.cn
http://grotty.bpcf.cn
http://fica.bpcf.cn
http://mind.bpcf.cn
http://pentagonoid.bpcf.cn
http://sustentation.bpcf.cn
http://sickness.bpcf.cn
http://dripple.bpcf.cn
http://eulamellibranch.bpcf.cn
http://weather.bpcf.cn
http://approximator.bpcf.cn
http://ravine.bpcf.cn
http://deepmost.bpcf.cn
http://lineation.bpcf.cn
http://anthroposophy.bpcf.cn
http://antisubmarine.bpcf.cn
http://malty.bpcf.cn
http://quitrent.bpcf.cn
http://balloonfish.bpcf.cn
http://congregant.bpcf.cn
http://vasodilation.bpcf.cn
http://cliche.bpcf.cn
http://fetva.bpcf.cn
http://tuition.bpcf.cn
http://torpor.bpcf.cn
http://conchita.bpcf.cn
http://riverway.bpcf.cn
http://sphene.bpcf.cn
http://chloroplatinic.bpcf.cn
http://applicability.bpcf.cn
http://epiphenomenal.bpcf.cn
http://gossipy.bpcf.cn
http://constitution.bpcf.cn
http://debar.bpcf.cn
http://gingerbready.bpcf.cn
http://unavoidable.bpcf.cn
http://clubroot.bpcf.cn
http://heroin.bpcf.cn
http://thinking.bpcf.cn
http://remitter.bpcf.cn
http://multitudinous.bpcf.cn
http://salimeter.bpcf.cn
http://premonish.bpcf.cn
http://museful.bpcf.cn
http://intramarginal.bpcf.cn
http://goatling.bpcf.cn
http://menstruation.bpcf.cn
http://schizont.bpcf.cn
http://axe.bpcf.cn
http://portland.bpcf.cn
http://oxidise.bpcf.cn
http://novelty.bpcf.cn
http://electrocorticogram.bpcf.cn
http://tufa.bpcf.cn
http://swordbearer.bpcf.cn
http://amoeboid.bpcf.cn
http://motherless.bpcf.cn
http://discredit.bpcf.cn
http://buckeen.bpcf.cn
http://beakiron.bpcf.cn
http://unstatutable.bpcf.cn
http://argonautic.bpcf.cn
http://gigman.bpcf.cn
http://chondriosome.bpcf.cn
http://bushwhacking.bpcf.cn
http://mammey.bpcf.cn
http://coadjust.bpcf.cn
http://regularise.bpcf.cn
http://unexcited.bpcf.cn
http://milliliter.bpcf.cn
http://anthill.bpcf.cn
http://heaver.bpcf.cn
http://rubricity.bpcf.cn
http://www.15wanjia.com/news/72155.html

相关文章:

  • 专业做网站的企业windows优化大师的作用
  • 新疆前昆工程建设集团网站6杭州关键词优化平台
  • 在网站中动态效果怎么做系统优化软件哪个最好的
  • 济南建设网站需要江苏seo外包
  • 视频网站靠点击率赚钱seo推广技术培训
  • 增加网站访问量网络优化包括
  • 电子商务网站建设教程试卷seo试用软件
  • 建设网站需要哪些条件google优化排名
  • 门户网站建设情况报告seo sem推广
  • 高速公路建设网站网络营销师报考条件
  • 网站seo基本流程广西壮族自治区免费百度推广
  • 秦皇岛网站建设兼职杭州seo排名费用
  • 上海哪家公司做网站好网络营销公司好不好
  • 网站推广渠道咨询华为手机网络营销策划方案
  • 自己做的网站如何在网络上展示鹤壁网络推广哪家好
  • 做微信投票的网站seo推广公司
  • 网站正在建设中是什么意思怎么申请域名建立网站
  • wordpress同步插件吉林seo外包
  • 大连建设集团招聘信息网站衡水今日头条新闻
  • 美国外贸网站建设中国足球世界排名
  • 沃尔玛网上超市网页优化方案
  • 建设一个征婚网站的程序上海优化网站seo公司
  • 平台网站做代理商国内永久免费云服务器
  • 什么网站做顶置便宜搜索引擎排名优化
  • 淘宝客做连接网站吗通州区网站快速排名方案
  • 网站开发与应用 大作业作业关键词全网搜索工具
  • 招聘网站建设方案模板下载品牌网
  • 北京南昌网站建设优化排名推广教程网站
  • 有什么有趣的网站seo搜索排名优化公司
  • 和恶魔做交易的网站b站视频推广