use of com.ctrip.framework.dal.cluster.client.shard.DatabaseShard in project dal by ctripcorp.
the class DalConnectionManager method clusterSelect.
protected DataBase clusterSelect(DatabaseSet dbSet, DalHints hints, String shard, boolean isMaster, boolean isSelect) {
Cluster cluster = ((ClusterDatabaseSet) dbSet).getCluster();
logReadStrategy(cluster);
DatabaseShard databaseShard;
if (StringUtils.isEmpty(shard)) {
Set<Integer> shards = cluster.getAllDbShards();
if (shards.size() == 0)
throw new DalRuntimeException("no shards found for this cluster");
if (shards.size() > 1)
throw new DalRuntimeException("multiple shards detected for non sharding cluster");
databaseShard = cluster.getDatabaseShard(shards.iterator().next());
} else
databaseShard = cluster.getDatabaseShard(Integer.valueOf(shard));
if (isMaster || !isSelect) {
return new ClusterDataBase(databaseShard.getMasters().iterator().next());
}
return new ClusterDataBase(databaseShard.selectDatabaseFromReadStrategy(hints));
}
use of com.ctrip.framework.dal.cluster.client.shard.DatabaseShard in project dal by ctripcorp.
the class DefaultCluster method getDatabases.
@Override
public List<Database> getDatabases() {
List<Database> databases = new LinkedList<>();
for (DatabaseShard databaseShard : databaseShards.values()) {
databases.addAll(databaseShard.getMasters());
databases.addAll(databaseShard.getSlaves());
}
return databases;
}
use of com.ctrip.framework.dal.cluster.client.shard.DatabaseShard in project dal by ctripcorp.
the class GroupConnection method pickRead.
protected Connection pickRead(DalHints dalHints) throws SQLException {
DatabaseShard databaseShard = clusterInfo.getCluster().getDatabaseShard(shardIndex);
DataSource dataSource = readDataSource.get(databaseShard.selectDatabaseFromReadStrategy(dalHints));
if (dataSource == null) {
synchronized (groupDataSource) {
dataSource = readDataSource.get(databaseShard.selectDatabaseFromReadStrategy(dalHints));
if (dataSource == null) {
groupDataSource.init();
this.readDataSource = groupDataSource.readDataSource;
dataSource = this.readDataSource.get(databaseShard.selectDatabaseFromReadStrategy(dalHints));
}
}
}
return dataSource.getConnection();
}
Aggregations