Search in sources :

Example 21 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class ClusterManagerImpl method getOrCreateCluster.

@Override
public Cluster getOrCreateCluster(String clusterName, DalConfigCustomizedOption customizedOption) {
    if (StringUtils.isEmpty(clusterName))
        throw new DalRuntimeException("cluster name is empty");
    clusterName = StringUtils.toTrimmedLowerCase(clusterName);
    Cluster cluster = clusters.get(clusterName);
    if (cluster == null)
        synchronized (clusters) {
            cluster = clusters.get(clusterName);
            if (cluster == null) {
                cluster = createCluster(clusterName, customizedOption);
                clusters.put(clusterName, cluster);
            }
        }
    return cluster;
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Cluster(com.ctrip.framework.dal.cluster.client.Cluster)

Example 22 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class ClusterConfigAdapter method load.

private void load(DalConnectionStringConfigure configure) {
    ClusterConfig clusterConfig;
    if (configure instanceof InvalidVariableConnectionString) {
        throw new DalRuntimeException("connectionString invalid for db: " + provider.getDbName(), ((InvalidVariableConnectionString) configure).getConnectionStringException());
    }
    if (configure == null)
        throw new RuntimeException("Get null config from mysqlapi for db: " + provider.getDbName());
    if (configure instanceof MultiHostConnectionStringConfigure)
        clusterConfig = buildMultiHostClusterConfig((MultiHostConnectionStringConfigure) configure);
    else
        clusterConfig = buildNormalClusterConfig(configure);
    clusterConfigRef.getAndSet(clusterConfig);
    DalConnectionStringConfigure prev = connStrConfigRef.getAndSet(configure);
    if (prev != null && !equals(prev, configure))
        for (Listener<ClusterConfig> listener : getListeners()) {
            try {
                listener.onChanged(this);
            } catch (Throwable t) {
            // ignore
            }
        }
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Listener(com.ctrip.framework.dal.cluster.client.base.Listener) ClusterConfig(com.ctrip.framework.dal.cluster.client.config.ClusterConfig)

Example 23 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class ClusterDynamicDataSource method getCustomDataSourceFactory.

private CustomDataSourceFactory getCustomDataSourceFactory() {
    DalConfigCustomizedOption customizedOption = cluster.getCustomizedOption();
    String clazz = null;
    if (customizedOption != null) {
        clazz = customizedOption.getDataSourceFactory();
    }
    if (StringUtils.isEmpty(clazz)) {
        Properties properties = cluster.getCustomProperties();
        clazz = properties.getProperty(DATASOURCE_FACTORY);
    }
    try {
        return (CustomDataSourceFactory) Class.forName(clazz).newInstance();
    } catch (Exception e) {
        throw new DalRuntimeException("Construct CustomDataSourceFactory error", e);
    }
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalConfigCustomizedOption(com.ctrip.framework.dal.cluster.client.config.DalConfigCustomizedOption) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) CustomDataSourceFactory(com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory) SQLException(java.sql.SQLException) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) UnsupportedFeatureException(com.ctrip.platform.dal.exceptions.UnsupportedFeatureException)

Example 24 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class ClusterDynamicDataSource method forceSwitch.

@Override
public SwitchableDataSourceStatus forceSwitch(FirstAidKit configure, final String ip, final Integer port) {
    synchronized (lock) {
        SwitchableDataSourceStatus currentStatus = getStatus();
        ForceSwitchedStatus prevStatus = status.getAndSet(ForceSwitchedStatus.ForceSwitching);
        try {
            String logName = String.format(FORCE_SWITCH, clusterInfo.toString());
            LOGGER.logTransaction(DalLogTypes.DAL_CONFIGURE, logName, String.format("newIp: %s, newPort: %s", ip, port), () -> {
                LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("old isForceSwitched before force switch: %s, old poolCreated before force switch: %s", currentStatus.isForceSwitched(), currentStatus.isPoolCreated()));
                getExecutor().submit(() -> {
                    try {
                        DataSource newDataSource;
                        if (DatabaseCategory.CUSTOM == cluster.getDatabaseCategory()) {
                            newDataSource = createCustomDataSource();
                        } else {
                            DataSourceConfigure dataSourceConfig = getSingleDataSource().getDataSourceConfigure().clone();
                            LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("previous host(s): %s:%s", currentStatus.getHostName(), currentStatus.getPort()));
                            dataSourceConfig.replaceURL(ip, port);
                            LOGGER.logEvent(DalLogTypes.DAL_CONFIGURE, logName, String.format("new host(s): %s:%s", ip, port));
                            newDataSource = new RefreshableDataSource(dataSourceId, dataSourceConfig);
                        }
                        switchDataSource(newDataSource);
                        status.set(ForceSwitchedStatus.ForceSwitched);
                        currentHost.set(new HostAndPort(null, ip, port));
                    } catch (Throwable t) {
                        LOGGER.error("DataSource creation failed", t);
                        // TODO: handle pool creation failure
                        status.set(prevStatus);
                    }
                });
            });
            return currentStatus;
        } catch (Throwable t) {
            status.set(prevStatus);
            LOGGER.error("Force switch error", t);
            throw new DalRuntimeException("Force switch error", t);
        }
    }
}
Also used : ForceSwitchedStatus(com.ctrip.platform.dal.common.enums.ForceSwitchedStatus) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString) ClusterDataSource(com.ctrip.platform.dal.dao.datasource.cluster.ClusterDataSource) DataSource(javax.sql.DataSource)

Example 25 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class DefaultClusterRouteStrategyConfig method generate.

@Override
public RouteStrategy generate() {
    String strategyName = routeStrategyName();
    String clazz = RouteStrategyEnum.parse(strategyName);
    try {
        return (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy) Class.forName(clazz).newInstance();
    } catch (Throwable t) {
        String msg = "Error constructing route strategy: " + strategyName;
        throw new DalRuntimeException(msg, t);
    }
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) RouteStrategy(com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)

Aggregations

DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)29 DalContextClient (com.ctrip.platform.dal.dao.DalContextClient)8 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)8 ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 Callback (com.ctrip.platform.dal.dao.log.Callback)3 Map (java.util.Map)3 Cluster (com.ctrip.framework.dal.cluster.client.Cluster)2 UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)2 RouteStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)2 Node (org.w3c.dom.Node)2 HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)1 Listener (com.ctrip.framework.dal.cluster.client.base.Listener)1 ClusterConfig (com.ctrip.framework.dal.cluster.client.config.ClusterConfig)1 DalConfigCustomizedOption (com.ctrip.framework.dal.cluster.client.config.DalConfigCustomizedOption)1 ClusterConfigException (com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException)1 CustomDataSourceFactory (com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)1 DatabaseShard (com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)1 ClusterIdGeneratorConfig (com.ctrip.framework.dal.cluster.client.sharding.idgen.ClusterIdGeneratorConfig)1