Search in sources :

Example 1 with Setting

use of cn.hutool.setting.Setting 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;
}
Also used : ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) Setting(cn.hutool.setting.Setting) DbRuntimeException(cn.hutool.db.DbRuntimeException) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 2 with Setting

use of cn.hutool.setting.Setting 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;
}
Also used : Setting(cn.hutool.setting.Setting) Properties(java.util.Properties) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 3 with Setting

use of cn.hutool.setting.Setting 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;
}
Also used : Setting(cn.hutool.setting.Setting) DbRuntimeException(cn.hutool.db.DbRuntimeException) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) SimpleDataSource(cn.hutool.db.ds.simple.SimpleDataSource) DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 4 with Setting

use of cn.hutool.setting.Setting in project hutool by looly.

the class SettingTest method settingTestForCustom.

@Test
public void settingTestForCustom() {
    Setting setting = new Setting();
    setting.put("group1.user", "root");
    setting.put("group2.user", "root2");
    setting.set("user", "group3", "root3");
    setting.set("user", "root4");
    Assert.assertEquals("root", setting.getByGroup("user", "group1"));
    Assert.assertEquals("root2", setting.getByGroup("user", "group2"));
    Assert.assertEquals("root3", setting.getByGroup("user", "group3"));
    Assert.assertEquals("root4", setting.get("user"));
}
Also used : Setting(cn.hutool.setting.Setting) Test(org.junit.Test)

Example 5 with Setting

use of cn.hutool.setting.Setting in project hutool by looly.

the class SettingTest method settingTest.

@Test
public void settingTest() {
    Setting setting = new Setting("test.setting", true);
    String driver = setting.getByGroup("driver", "demo");
    Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
    String user = setting.getByGroup("user", "demo");
    Assert.assertEquals(user, "rootcom.mysql.jdbc.Driver");
}
Also used : Setting(cn.hutool.setting.Setting) Test(org.junit.Test)

Aggregations

Setting (cn.hutool.setting.Setting)16 DbRuntimeException (cn.hutool.db.DbRuntimeException)8 NotInitedException (cn.hutool.core.exceptions.NotInitedException)3 Test (org.junit.Test)3 MongoClient (com.mongodb.MongoClient)2 MongoCredential (com.mongodb.MongoCredential)2 ServerAddress (com.mongodb.ServerAddress)2 SimpleDataSource (cn.hutool.db.ds.simple.SimpleDataSource)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)1 DataSource (org.apache.tomcat.jdbc.pool.DataSource)1 PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)1 Ignore (org.junit.Ignore)1 JedisPool (redis.clients.jedis.JedisPool)1 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)1