Search in sources :

Example 6 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException 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 7 with DalRuntimeException

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

the class DataSourceConfigure method valueOf.

public static DataSourceConfigure valueOf(IDataSourceConfigure configure) {
    if (configure instanceof DataSourceConfigure)
        return (DataSourceConfigure) configure;
    else {
        DataSourceConfigure dataSourceConfigure = new DataSourceConfigure();
        Properties properties = new Properties();
        String username = configure.getUserName();
        properties.setProperty(USER_NAME, username != null ? username : "");
        String password = configure.getPassword();
        properties.setProperty(PASSWORD, password != null ? password : "");
        String connectionUrl = configure.getConnectionUrl();
        if (connectionUrl == null)
            throw new DalRuntimeException("connection url cannot be null");
        properties.setProperty(CONNECTION_URL, connectionUrl);
        try {
            HostAndPort hostAndPort = ConnectionStringParser.parseHostPortFromURL(connectionUrl);
            if (StringUtils.isEmpty(hostAndPort.getHost())) {
                properties.setProperty(HOST_NAME, "unknown");
            } else {
                properties.setProperty(HOST_NAME, hostAndPort.getHost());
            }
        } catch (Throwable t) {
        // ignore
        }
        if (configure.getDriverClass() != null)
            properties.setProperty(DRIVER_CLASS_NAME, configure.getDriverClass());
        if (configure.getTestWhileIdle() != null)
            properties.setProperty(TESTWHILEIDLE, String.valueOf(configure.getTestWhileIdle()));
        if (configure.getTestOnBorrow() != null)
            properties.setProperty(TESTONBORROW, String.valueOf(configure.getTestOnBorrow()));
        if (configure.getTestOnReturn() != null)
            properties.setProperty(TESTONRETURN, String.valueOf(configure.getTestOnReturn()));
        if (configure.getValidationQuery() != null)
            properties.setProperty(VALIDATIONQUERY, configure.getValidationQuery());
        if (configure.getValidationQueryTimeout() != null)
            properties.setProperty(VALIDATIONQUERYTIMEOUT, String.valueOf(configure.getValidationQueryTimeout()));
        if (configure.getValidationInterval() != null)
            properties.setProperty(VALIDATIONINTERVAL, String.valueOf(configure.getValidationInterval()));
        if (configure.getTimeBetweenEvictionRunsMillis() != null)
            properties.setProperty(TIMEBETWEENEVICTIONRUNSMILLIS, String.valueOf(configure.getTimeBetweenEvictionRunsMillis()));
        if (configure.getMaxAge() != null)
            properties.setProperty(MAX_AGE, String.valueOf(configure.getMaxAge()));
        if (configure.getMaxActive() != null)
            properties.setProperty(MAXACTIVE, String.valueOf(configure.getMaxActive()));
        if (configure.getMinIdle() != null)
            properties.setProperty(MINIDLE, String.valueOf(configure.getMinIdle()));
        if (configure.getMaxWait() != null)
            properties.setProperty(MAXWAIT, String.valueOf(configure.getMaxWait()));
        if (configure.getInitialSize() != null)
            properties.setProperty(INITIALSIZE, String.valueOf(configure.getInitialSize()));
        if (configure.getRemoveAbandonedTimeout() != null)
            properties.setProperty(REMOVEABANDONEDTIMEOUT, String.valueOf(configure.getRemoveAbandonedTimeout()));
        if (configure.getRemoveAbandoned() != null)
            properties.setProperty(REMOVEABANDONED, String.valueOf(configure.getRemoveAbandoned()));
        if (configure.getLogAbandoned() != null)
            properties.setProperty(LOGABANDONED, String.valueOf(configure.getLogAbandoned()));
        if (configure.getMinEvictableIdleTimeMillis() != null)
            properties.setProperty(MINEVICTABLEIDLETIMEMILLIS, String.valueOf(configure.getMinEvictableIdleTimeMillis()));
        if (configure.getConnectionProperties() != null)
            properties.setProperty(CONNECTIONPROPERTIES, configure.getConnectionProperties());
        if (configure.getInitSQL() != null)
            properties.setProperty(INIT_SQL, configure.getInitSQL());
        if (configure.getValidatorClassName() != null)
            properties.setProperty(VALIDATORCLASSNAME, configure.getValidatorClassName());
        if (configure.getJdbcInterceptors() != null)
            properties.setProperty(JDBC_INTERCEPTORS, configure.getJdbcInterceptors());
        if (configure instanceof AbstractDataSourceConfigure) {
            AbstractDataSourceConfigure configure1 = (AbstractDataSourceConfigure) configure;
            if (configure1.getSessionWaitTimeout() != null)
                properties.setProperty(SESSION_WAIT_TIMEOUT, String.valueOf(configure1.getSessionWaitTimeout()));
        }
        dataSourceConfigure.setProperties(properties);
        return dataSourceConfigure;
    }
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException)

Example 8 with DalRuntimeException

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

the class DefaultIdGeneratorConfigXMLParser method parse.

private ClusterIdGeneratorConfig parse(String clusterName, Document doc) {
    Element root = doc.getDocumentElement();
    if (root == null || !ClusterConfigXMLConstants.ID_GENERATORS.equalsIgnoreCase(root.getTagName()))
        throw new ClusterConfigException("root element should be <IdGenerators>");
    List<Node> idGeneratorNodes = getChildNodes(root, ID_GENERATOR);
    if (idGeneratorNodes.size() > 1)
        throw new DalRuntimeException("the count of <IdGenerator> nodes should be only one");
    if (idGeneratorNodes.size() == 0)
        throw new DalRuntimeException("<IdGenerator> node does not exist");
    return parse(clusterName, idGeneratorNodes.get(0));
}
Also used : ClusterConfigException(com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node)

Example 9 with DalRuntimeException

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

the class DefaultIdGeneratorConfigXMLParser method parse.

@Override
public ClusterIdGeneratorConfig parse(String clusterName, String content) {
    StringReader reader = new StringReader(content);
    InputSource source = new InputSource(reader);
    try {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source);
        return parse(clusterName, doc);
    } catch (Throwable t) {
        throw new DalRuntimeException("parse cluster IdGenerator config error", t);
    }
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 10 with DalRuntimeException

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

the class DefaultIdGeneratorConfigXMLParser method getIdGenConfig.

private IIdGeneratorConfig getIdGenConfig(String logicDbName, Node idGeneratorNode) {
    if (null == idGeneratorNode) {
        return null;
    }
    Node includesNode = getChildNode(idGeneratorNode, INCLUDES);
    Node excludesNode = getChildNode(idGeneratorNode, EXCLUDES);
    if (includesNode != null && excludesNode != null) {
        throw new DalRuntimeException("<includes> and <excludes> nodes cannot be configured together within <IdGenerator> node");
    }
    IIdGeneratorFactory dbDefaultFactory = getIdGenFactoryForNode(idGeneratorNode);
    Map<String, IIdGeneratorFactory> tableFactoryMap = null;
    if (includesNode != null) {
        tableFactoryMap = getIdGenFactoriesForNode(includesNode, INCLUDE, dbDefaultFactory);
        if (dbDefaultFactory instanceof NullIdGeneratorFactory) {
            dbDefaultFactory = idGenFactoryManager.getOrCreateDefaultFactory();
        } else {
            dbDefaultFactory = idGenFactoryManager.getOrCreateNullFactory();
        }
    } else if (excludesNode != null) {
        if (dbDefaultFactory instanceof NullIdGeneratorFactory) {
            tableFactoryMap = getIdGenFactoriesForNode(excludesNode, EXCLUDE, idGenFactoryManager.getOrCreateDefaultFactory());
        } else {
            tableFactoryMap = getIdGenFactoriesForNode(excludesNode, EXCLUDE, idGenFactoryManager.getOrCreateNullFactory());
        }
    }
    String sequenceDbName = getAttribute(idGeneratorNode, SEQUENCE_DATABASE_NAME, logicDbName);
    String entityDbName = getAttribute(idGeneratorNode, ENTITY_DATABASE_NAME, logicDbName);
    String entityPackage = getAttribute(idGeneratorNode, ENTITY_PACKAGE, null);
    return new IdGeneratorConfig(sequenceDbName, entityDbName, entityPackage, dbDefaultFactory, tableFactoryMap);
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Node(org.w3c.dom.Node) ClusterIdGeneratorConfig(com.ctrip.framework.dal.cluster.client.sharding.idgen.ClusterIdGeneratorConfig)

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