Search in sources :

Example 1 with DatabaseShard

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));
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Cluster(com.ctrip.framework.dal.cluster.client.Cluster) DatabaseShard(com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)

Example 2 with DatabaseShard

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;
}
Also used : Database(com.ctrip.framework.dal.cluster.client.database.Database) DatabaseShard(com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)

Example 3 with DatabaseShard

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();
}
Also used : DatabaseShard(com.ctrip.framework.dal.cluster.client.shard.DatabaseShard) DataSource(javax.sql.DataSource)

Aggregations

DatabaseShard (com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)3 Cluster (com.ctrip.framework.dal.cluster.client.Cluster)1 Database (com.ctrip.framework.dal.cluster.client.database.Database)1 DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)1 DataSource (javax.sql.DataSource)1