org.springframework.biz.jdbc.DataSourceRoutingKeyHolder Maven / Gradle / Ivy
/*
* Copyright (c) 2018, hiwepy (https://github.com/hiwepy).
*
* 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
*
* http://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.biz.jdbc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class DataSourceRoutingKeyHolder {
public static final String MASTER_DATASOURCE = "defaultDataSource";
private final static Logger logger = LoggerFactory.getLogger(DataSourceRoutingKeyHolder.class);
/**
* A provider of random values.
*/
private final static Random random = new Random();
/**
* 用于在切换数据源时保证不会被其他线程修改
*/
private static Lock lock = new ReentrantLock();
/**
* Maintain variable for every thread, to avoid effect other thread
*/
private static final ThreadLocal CONTEXT_HOLDER = new ThreadLocal() {
@Override
protected String initialValue() {
return MASTER_DATASOURCE;
}
};
/**
* All DataSource List
*/
public static List