use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class C3p0DSFactory method createDataSource.
/**
* 创建数据源
* @param group 分组
* @return C3P0数据源 {@link ComboPooledDataSource}
*/
private ComboPooledDataSource createDataSource(String group) {
final Setting config = setting.getSetting(group);
if (CollectionUtil.isEmpty(config)) {
throw new DbRuntimeException("No C3P0 config for group: [{}]", group);
}
final ComboPooledDataSource ds = new ComboPooledDataSource();
// 基本信息
final String url = config.getAndRemoveStr(KEY_ALIAS_URL);
if (StrUtil.isBlank(url)) {
throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
}
ds.setJdbcUrl(url);
ds.setUser(config.getAndRemoveStr(KEY_ALIAS_USER));
ds.setPassword(config.getAndRemoveStr(KEY_ALIAS_PASSWORD));
final String driver = config.getAndRemoveStr(KEY_ALIAS_DRIVER);
try {
if (StrUtil.isNotBlank(driver)) {
ds.setDriverClass(driver);
} else {
ds.setDriverClass(DbUtil.identifyDriver(url));
}
} catch (Exception e) {
throw new DbRuntimeException(e);
}
// 注入属性
config.toBean(ds);
return ds;
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class DruidDSFactory method createDataSource.
/**
* 创建数据源
* @param group 分组
* @return Druid数据源 {@link DruidDataSource}
*/
private DruidDataSource createDataSource(String group) {
final Setting config = setting.getSetting(group);
if (CollectionUtil.isEmpty(config)) {
throw new DbRuntimeException("No Druid config for group: [{}]", group);
}
final DruidDataSource ds = new DruidDataSource();
// 基本信息
final String url = config.getAndRemoveStr(KEY_ALIAS_URL);
if (StrUtil.isBlank(url)) {
throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
}
ds.setUrl(url);
ds.setUsername(config.getAndRemoveStr(KEY_ALIAS_USER));
ds.setPassword(config.getAndRemoveStr(KEY_ALIAS_PASSWORD));
final String driver = config.getAndRemoveStr(KEY_ALIAS_DRIVER);
// 在未提供JDBC驱动的情况下,Druid会自动识别驱动
if (StrUtil.isNotBlank(driver)) {
ds.setDriverClassName(driver);
}
// 规范化属性名
Properties config2 = new Properties();
String keyStr;
for (Entry<Object, Object> entry : config.entrySet()) {
keyStr = StrUtil.addPrefixIfNot(Convert.toStr(entry.getKey()), "druid.");
config2.put(keyStr, entry.getValue());
}
// 连接池信息
ds.configFromPropety(config2);
// 检查关联配置,在用户未设置某项配置时,
if (null == ds.getValidationQuery()) {
// 在validationQuery未设置的情况下,以下三项设置都将无效
ds.setTestOnBorrow(false);
ds.setTestOnReturn(false);
ds.setTestWhileIdle(false);
}
return ds;
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class JndiDSFactory method createDataSource.
/**
* 创建数据源
* @param group JNDI名
* @return 数据源 {@link DataSource}
*/
private DataSource createDataSource(String group) {
if (group == null) {
group = StrUtil.EMPTY;
}
String jndiName = setting.getByGroup("jndi", group);
if (StrUtil.isEmpty(jndiName)) {
throw new DbRuntimeException("No setting name [jndi] for group [{}]", group);
}
DataSource ds = DbUtil.getJndiDs(jndiName);
return ds;
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class TomcatDSFactory method createDataSource.
/**
* 创建数据源
* @param group 分组
* @return Tomcat数据源 {@link DataSource}
*/
private DataSource createDataSource(String group) {
if (group == null) {
group = StrUtil.EMPTY;
}
Setting config = setting.getSetting(group);
if (null == config || config.isEmpty()) {
throw new DbRuntimeException("No Tomcat jdbc pool config for group: [{}]", group);
}
final PoolProperties poolProps = new PoolProperties();
// 基本信息
poolProps.setUrl(getAndRemoveProperty(config, "url", "jdbcUrl"));
poolProps.setUsername(getAndRemoveProperty(config, "username", "user"));
poolProps.setPassword(getAndRemoveProperty(config, "password", "pass"));
final String driver = getAndRemoveProperty(config, "driver", "driverClassName");
if (StrUtil.isNotBlank(driver)) {
poolProps.setDriverClassName(driver);
} else {
poolProps.setDriverClassName(DbUtil.identifyDriver(poolProps.getUrl()));
}
// 扩展属性
config.toBean(poolProps);
final DataSource ds = new DataSource(poolProps);
return ds;
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class MongoDS method initCloud.
/**
* 初始化集群<br>
* 集群的其它客户端设定参数使用全局设定<br>
* 集群中每一个实例成员用一个group表示,例如:
*
* <pre>
* user = test1
* pass = 123456
* database = test
* [db0]
* host = 192.168.1.1:27117
* [db1]
* host = 192.168.1.1:27118
* [db2]
* host = 192.168.1.1:27119
* </pre>
*/
public synchronized void initCloud() {
if (groups == null || groups.length == 0) {
throw new DbRuntimeException("Please give replication set groups!");
}
if (setting == null) {
// 若未指定配置文件,则使用默认配置文件
setting = new Setting(MONGO_CONFIG_PATH, true);
}
final List<ServerAddress> addrList = new ArrayList<>();
for (String group : groups) {
addrList.add(createServerAddress(group));
}
final MongoCredential credentail = createCredentail(StrUtil.EMPTY);
try {
if (null == credentail) {
mongo = new MongoClient(addrList, buildMongoClientOptions(StrUtil.EMPTY));
} else {
mongo = new MongoClient(addrList, credentail, buildMongoClientOptions(StrUtil.EMPTY));
}
} catch (Exception e) {
log.error(e, "Init MongoDB connection error!");
return;
}
log.info("Init MongoDB cloud Set pool with connection to {}", addrList);
}
Aggregations