多数据源(sql server 2008,二个数据库不ip,)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自动扫描 ,多个包以 逗号分隔-->
<context:component-scan base-package="com.yunzhijia.cloudflow" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 数据库测试 -->
<bean id="dataSource_cs" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.cs.driver}" />
<property name="url" value="${dataSource.cs.url}" />
<property name="username" value="${dataSource.cs.username}" />
<property name="password" value="${dataSource.cs.password}" />
<property name="initialSize" value="${dataSource.cs.initialSize}"></property>
<property name="maxActive" value="${dataSource.cs.maxActive}"></property>
<property name="maxIdle" value="${dataSource.cs.maxIdle}"></property>
<property name="minIdle" value="${dataSource.cs.minIdle}"></property>
<property name="maxWait" value="${dataSource.cs.maxWait}"></property>
</bean>
<!-- 数据库福清 -->
<bean id="dataSource_fq" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.fq.driver}" />
<property name="url" value="${dataSource.fq.url}" />
<property name="username" value="${dataSource.fq.username}" />
<property name="password" value="${dataSource.fq.password}" />
<property name="initialSize" value="${dataSource.fq.initialSize}"></property>
<property name="maxActive" value="${dataSource.fq.maxActive}"></property>
<property name="maxIdle" value="${dataSource.fq.maxIdle}"></property>
<property name="minIdle" value="${dataSource.fq.minIdle}"></property>
<property name="maxWait" value="${dataSource.fq.maxWait}"></property>
</bean>
<!-- 数据库河南 -->
<bean id="dataSource_hn" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.hn.driver}" />
<property name="url" value="${dataSource.hn.url}" />
<property name="username" value="${dataSource.hn.username}" />
<property name="password" value="${dataSource.hn.password}" />
<property name="initialSize" value="${dataSource.hn.initialSize}"></property>
<property name="maxActive" value="${dataSource.hn.maxActive}"></property>
<property name="maxIdle" value="${dataSource.hn.maxIdle}"></property>
<property name="minIdle" value="${dataSource.hn.minIdle}"></property>
<property name="maxWait" value="${dataSource.hn.maxWait}"></property>
</bean>
<bean id="dataSource" class="com.yunzhijia.cloudflow.common.utils.ThreadLocalRountingDataSource">
<property name="defaultTargetDataSource" ref="dataSource_cs"/>
<property name="targetDataSources">
<map key-type="com.yunzhijia.cloudflow.common.utils.DataSources">
<entry key="DATASOURCE_CS" value-ref="dataSource_cs"/>
<entry key="DATASOURCE_FQ" value-ref="dataSource_fq"/>
<entry key="DATASOURCE_HN" value-ref="dataSource_hn"/>
</map>
</property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/yunzhijia/cloudflow/mapping/**/*.xml"></property>
<!-- 指向mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yunzhijia.cloudflow.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务控制的注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
========================================
package com.yunzhijia.cloudflow.common.utils;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 数据源切换
* 新建ThreadLocalRountingDataSource继承AbstractRoutingDataSource,实现切换
* @author gzh
*
*/
public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceTypeManager.get();
}
}
========================================
package com.yunzhijia.cloudflow.common.utils;
import org.apache.log4j.Logger;
/**
* 数据源切换
* @author
*
*/
public class DataSourceTypeManager {
private static final Logger log = Logger.getLogger(DataSourceTypeManager.class);
private static final ThreadLocal<DataSources> dataSourceTypes = new ThreadLocal<DataSources>(){
@Override
protected DataSources initialValue(){
log.info("==>初始化数据源:"+DataSources.DATASOURCE_CS);
return DataSources.DATASOURCE_CS;
}
};
/**
* 取DataSources
* @return DataSources
*/
public static DataSources get(){
log.info("==>取数据源:"+dataSourceTypes.get());
return dataSourceTypes.get();
}
/**
* 设置DataSources源
* @param dataSourceType
*/
public static void set(DataSources dataSourceType){
log.info("==>设置数据源:"+dataSourceType);
dataSourceTypes.set(dataSourceType);
}
/**
* 重置数据库源
*/
public static void reset(){
log.info("==>重置数据源:"+DataSources.DATASOURCE_CS);
dataSourceTypes.set(DataSources.DATASOURCE_CS);
}
}
================================
package com.yunzhijia.cloudflow.common.utils;
/**
* 配置数据库切换
* @author
* 注:注意此枚举值必须对应spring配置文件各数据源的别名
*/
public enum DataSources {
DATASOURCE_CS,DATASOURCE_FQ,DATASOURCE_HN
}
多数据源(sql server 2008,二个数据库不ip,)的更多相关文章
- 基于Sql Server 2008的分布式数据库的实践(二)
原文 基于Sql Server 2008的分布式数据库的实践(二) 从Win7连接Win2003的Sql Server 2008 1.新建链接服务器链接到Win2003的Sql Server 2008 ...
- SQL Server 2008 R2 主从数据库同步
一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2 DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...
- SQL Server 2008 R2 主从数据库同步设置
一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2 DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...
- 基于Sql Server 2008的分布式数据库的实践(五)
原文 基于Sql Server 2008的分布式数据库的实践(五) 程序设计 ------------------------------------------------------------- ...
- SQL Server 2008 维护计划实现数据库备份
SQL Server 2008 维护计划实现数据库备份(最佳实践) 2013-08-29 09:08 by 听风吹雨, 173 阅读, 2 评论, 收藏, 编辑 一.背景 之前写过一篇关于备份的文章: ...
- SQL SERVER 2008 R2 还原数据库3154错误
1.SQL SERVER 2008 在还原数据库时,会报错. 提示错误:"备份集中的数据库备份与现有的 '***' 数据库不同.RESTORE DATABASE 正在异常终止. (Micro ...
- 基于Sql Server 2008的分布式数据库的实践(四)
原文 基于Sql Server 2008的分布式数据库的实践(四) 数据库设计 1.E-R图 2.数据库创建 Win 7 1 create database V3 Win 2003 1 create ...
- 基于Sql Server 2008的分布式数据库的实践(三)
原文 基于Sql Server 2008的分布式数据库的实践(三) 配置PHP 1.打开PHP配置文件,找到extension=php_mssql.dll,将前面的注释符号去掉 2.找到mssql.s ...
- 基于Sql Server 2008的分布式数据库的实践(一)
原文 基于Sql Server 2008的分布式数据库的实践(一) 配置Sql Server 2008(Win7) 1.打开SQL server2012,使用windows身份登录 2.登录后,右键选 ...
- SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件
原文:SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件 从2005开始,就引入了"数据库邮件"功能.并且取代SQLMail.原有SQLMail ...
随机推荐
- FreeMarker开发-数据模型
FreeMarker用于处理模板的数据模型是哈希表,也就是一个树状结构的name-value对.如下: (root)|+- string="string"| +- map| || ...
- ugui点击穿透判断
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Eve ...
- python数据分析入门(一)----安装pandas
打算入坑, python数据分析 , 所以下载了 <利用python数据分析>的电子书, 影印版 , 14年出版的 , 现在有很多工具对不上号, 但是整体思想还是不变的 , 所以准备工作要 ...
- 20191106 Spring Boot官方文档学习(1-2)
学习内容相关信息 最新版本:2.2.0 CURRENT GA 官网地址 官方文档地址 单页版文档地址 代码生成网址 2.入门 Spring Boot的主要目标是: 为所有Spring开发提供更快且入门 ...
- Spark集成的包与引入包冲突
今天在编写Spark应用的时候,想把处理结果输出为JSON字符串,查到Java比较常用的JSON处理包gson,按照其API编写代码后运行程序,总是出现"NoSuchMethodExcept ...
- 面试题:线程A打印1-10数字,打印到第5个数字时,通知线程B
此题考查的是线程间的通信方式. 可以利用park/unpark实现 可以利用volatile关键字实现 可以利用synchronized结合wait notify实现 可以利用JUC中的CountDo ...
- sql limit order by and where
1 sql limit limit size,返回前size行. limit offset , size,返回offset开始的size行,offset从0行开始. 2 sql limit with ...
- linux修改用户最大线程数
linux下普通用户最大允许使用线程数为1024: 但是并发量大时,该1024配置项远远不够满足我们的需要,我们可以修改/etc/security/limits.d/90-nproc.conf配置设置 ...
- Mybatis-学习笔记(3)mapper配置文件
1.mapper配置文件常用的元素 parameterMap已经废弃,老式风格的参数映射. 2.select元素 映射查询语句.#{...}用于预处理语句参数,通过JDBC,这样一个参数在SQL中会由 ...
- TestCase维护和思维导图
在软件整个生命周期中,测试应该尽早地开始,因为测试对象不只是程序,还有文档和数据,所以针对需求分析说明书.概要设计和详细设计说明书,测试如何快速理解项目需求,进行下一步的工作呢? 本人觉得,如果只是看 ...