/** * Alipay.com Inc. * Copyright (c) 2004-2012 All Rights Reserved. */ package com.alipay.zdal.client.jdbc.dbselector; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.sql.DataSource; import org.apache.log4j.Logger; import com.alipay.zdal.client.ThreadLocalString; import com.alipay.zdal.client.jdbc.ZdalStatement.DB_OPERATION_TYPE; import com.alipay.zdal.client.util.ThreadLocalMap; import com.alipay.zdal.common.OperationDBType; /** * ֻ��һ������Դ��DBSelector * //private String id, ��ΪdbSelector��id��db��dsKey */ public class OneDBSelector extends AbstractDBSelector { private static final Logger logger = Logger.getLogger(OneDBSelector.class); private DataSource db; private static final String indexSuffix = "_0"; public static final int DEFAULT_WEIGHT_INIT = 10; private static volatile int OneDbRunTimeWeight = DEFAULT_WEIGHT_INIT; public OneDBSelector(String id, DataSource dataSource) { super(id); this.db = dataSource; } public DataSource select() { return db; } /*public DBSelector copyAndRemove(DataSource removedDataSource) { return null; }*/ public <T> T tryExecute(Map<DataSource, SQLException> failedDataSources, DataSourceTryer<T> tryer, int times, DB_OPERATION_TYPE operationType, Object... args) throws SQLException { List<SQLException> exceptions; if (failedDataSources != null && failedDataSources.containsKey(db)) { //throw new SQLException("û�п��õ�����Դ��"); exceptions = new ArrayList<SQLException>(failedDataSources.size()); exceptions.addAll(failedDataSources.values()); return tryer.onSQLException(exceptions, this.exceptionSorter, args); } try { if (OneDbRunTimeWeight <= 0) { throw new SQLException("Mark_down or readonly!"); } //��ȡ��db�����֣�Ȼ�󻺴�������ҵ����õ������� Map<String, DataSource> map = new HashMap<String, DataSource>(); //����ǰ׺��by���� 20130903 map.put(getAppDsName() + "." + getId(), null); ThreadLocalMap.put(ThreadLocalString.GET_ID_AND_DATABASE, map); return tryer.tryOnDataSource(db, getId(), args); } catch (SQLException e) { exceptions = new ArrayList<SQLException>(1); exceptions.add(e); } return tryer.onSQLException(exceptions, this.exceptionSorter, args); } public <T> T tryExecute(DataSourceTryer<T> tryer, int times, DB_OPERATION_TYPE operationType, Object... args) throws SQLException { return this.tryExecute(null, tryer, times, null, args); } public void setWeight(Map<String, Integer> weightMap) { // Ϊ�˿��Զ�̬��ͣ��ij���߼��⣬���������´��� //�Ӹ���õ�id String id = getId(); String id_db = id + indexSuffix; Integer weight = weightMap.get(id_db); if (weight != null) { logger.warn("Change OneDbRunTimeWeight from " + OneDbRunTimeWeight + "to " + weight); OneDbRunTimeWeight = weight; logger.warn(" Chage the OneDbRunTimeWeight success!"); } else { logger.warn("We Can't find weight by id :" + id_db); return; } } public boolean isSupportRetry(OperationDBType type) { return false; } public DataSource getDb() { return db; } public void setDb(DataSource db) { this.db = db; } }