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));
}
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;
}
}
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));
}
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);
}
}
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);
}
Aggregations