Search in sources :

Example 6 with Props

use of cn.hutool.setting.dialect.Props in project hutool by looly.

the class DruidDSFactory method createDataSource.

@Override
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
    final DruidDataSource ds = new DruidDataSource();
    // 基本信息
    ds.setUrl(jdbcUrl);
    ds.setDriverClassName(driver);
    ds.setUsername(user);
    ds.setPassword(pass);
    // remarks等特殊配置,since 5.3.8
    String connValue;
    for (String key : KEY_CONN_PROPS) {
        connValue = poolSetting.getAndRemoveStr(key);
        if (StrUtil.isNotBlank(connValue)) {
            ds.addConnectionProperty(key, connValue);
        }
    }
    // Druid连接池配置信息,规范化属性名
    final Props druidProps = new Props();
    poolSetting.forEach((key, value) -> druidProps.put(StrUtil.addPrefixIfNot(key, "druid."), value));
    ds.configFromPropety(druidProps);
    // 检查关联配置,在用户未设置某项配置时,
    if (null == ds.getValidationQuery()) {
        // 在validationQuery未设置的情况下,以下三项设置都将无效
        ds.setTestOnBorrow(false);
        ds.setTestOnReturn(false);
        ds.setTestWhileIdle(false);
    }
    return ds;
}
Also used : Props(cn.hutool.setting.dialect.Props) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 7 with Props

use of cn.hutool.setting.dialect.Props in project hutool by looly.

the class HikariDSFactory method createDataSource.

@Override
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
    // remarks等特殊配置,since 5.3.8
    final Props connProps = new Props();
    String connValue;
    for (String key : KEY_CONN_PROPS) {
        connValue = poolSetting.getAndRemoveStr(key);
        if (StrUtil.isNotBlank(connValue)) {
            connProps.setProperty(key, connValue);
        }
    }
    final Props config = new Props();
    config.putAll(poolSetting);
    config.put("jdbcUrl", jdbcUrl);
    if (null != driver) {
        config.put("driverClassName", driver);
    }
    if (null != user) {
        config.put("username", user);
    }
    if (null != pass) {
        config.put("password", pass);
    }
    final HikariConfig hikariConfig = new HikariConfig(config);
    hikariConfig.setDataSourceProperties(connProps);
    return new HikariDataSource(hikariConfig);
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Props(cn.hutool.setting.dialect.Props) HikariConfig(com.zaxxer.hikari.HikariConfig)

Example 8 with Props

use of cn.hutool.setting.dialect.Props in project hutool by looly.

the class TomcatDSFactory method createDataSource.

@Override
protected javax.sql.DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
    final PoolProperties poolProps = new PoolProperties();
    poolProps.setUrl(jdbcUrl);
    poolProps.setDriverClassName(driver);
    poolProps.setUsername(user);
    poolProps.setPassword(pass);
    // remarks等特殊配置,since 5.3.8
    final Props connProps = new Props();
    String connValue;
    for (String key : KEY_CONN_PROPS) {
        connValue = poolSetting.getAndRemoveStr(key);
        if (StrUtil.isNotBlank(connValue)) {
            connProps.setProperty(key, connValue);
        }
    }
    poolProps.setDbProperties(connProps);
    // 连接池相关参数
    poolSetting.toBean(poolProps);
    return new DataSource(poolProps);
}
Also used : Props(cn.hutool.setting.dialect.Props) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource)

Example 9 with Props

use of cn.hutool.setting.dialect.Props in project hutool by looly.

the class SimpleDataSource method getConnection.

// -------------------------------------------------------------------- Getters and Setters end
@Override
public Connection getConnection() throws SQLException {
    final Props info = new Props();
    if (this.user != null) {
        info.setProperty("user", this.user);
    }
    if (this.pass != null) {
        info.setProperty("password", this.pass);
    }
    // 其它参数
    final Properties connProps = this.connProps;
    if (MapUtil.isNotEmpty(connProps)) {
        info.putAll(connProps);
    }
    return DriverManager.getConnection(this.url, info);
}
Also used : Props(cn.hutool.setting.dialect.Props) Properties(java.util.Properties)

Example 10 with Props

use of cn.hutool.setting.dialect.Props in project hutool by looly.

the class PropsTest method propTestForAbsPAth.

@Test
@Ignore
public void propTestForAbsPAth() {
    // noinspection MismatchedQueryAndUpdateOfCollection
    Props props = new Props("d:/test.properties");
    String user = props.getProperty("user");
    Assert.assertEquals(user, "root");
    String driver = props.getStr("driver");
    Assert.assertEquals(driver, "com.mysql.jdbc.Driver");
}
Also used : Props(cn.hutool.setting.dialect.Props) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Props (cn.hutool.setting.dialect.Props)12 Test (org.junit.Test)6 Ignore (org.junit.Ignore)2 DbRuntimeException (cn.hutool.db.DbRuntimeException)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 PropertyVetoException (java.beans.PropertyVetoException)1 Properties (java.util.Properties)1 DataSource (org.apache.tomcat.jdbc.pool.DataSource)1 PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)1