數據源在配置文件中的配置html
請點擊---》 java架構師項目實戰,高併發集羣分佈式,大數據高可用,視頻教程 java
請點擊---》 java架構師項目實戰,高併發集羣分佈式,大數據高可用,視頻教程 spring
DynamicDataSource.classsql
- package com.core;
-
- import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
-
- public class DynamicDataSource extends AbstractRoutingDataSource{
-
- @Override
- protected Object determineCurrentLookupKey() {
- return DatabaseContextHolder.getCustomerType();
- }
-
- }
DatabaseContextHolder.class設置數據源的類數據庫
- package com.core;
-
- public class DatabaseContextHolder {
-
- private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
- <span style="white-space:pre"> </span>
- public static void setCustomerType(String customerType) {
- contextHolder.set(customerType);
- }
- <span style="white-space:pre"> </span>
- public static String getCustomerType() {
- return contextHolder.get();
- }
- <span style="white-space:pre"> </span>
- public static void clearCustomerType() {
- contextHolder.remove();
- }
- }
DataSourceInterceptor.classexpress
- package com.core;
-
- import org.aspectj.lang.JoinPoint;
- import org.springframework.stereotype.Component;
-
- @Component
- public class DataSourceInterceptor {
- <span style="white-space:pre;"> </span>
- public static final String SOURCE_PLAN = "dataSourceOne;
-
- public static final String SOURCE_FUND = dataSourceTwo;
}spring-mvc
請點擊---》 java架構師項目實戰,高併發集羣分佈式,大數據高可用,視頻教程 session
springMVC數據源架構
- jdbc_driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
- dataSourceOne=jdbc:sqlserver://115.29.***.**;DatabaseName=DB_GuiHua
jdbc_username=**jdbc_password=**併發
- dataSourceTwo=jdbc:sqlserver:
Spring MVC會默認有一個數據源,當須要更換數據源時,要在調用事務以前配置
- DataSourceContextHolder.setDbType(DataSourceType.SOURCE_FUND);
- public class DataSourceContextHolder {
- private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
-
- public static void setDbType(String dbType) {
- contextHolder.set(dbType);
- }
-
- public static String getDbType() {
- return ((String) contextHolder.get());
- }
-
- public static void clearDbType() {
- contextHolder.remove();
- }
- }
請點擊---》 java架構師項目實戰,高併發集羣分佈式,大數據高可用,視頻教程