Search in sources :

Example 1 with DbRuntimeException

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;
}
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 DbRuntimeException

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;
}
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 DbRuntimeException

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;
}
Also used : DbRuntimeException(cn.hutool.db.DbRuntimeException) DataSource(javax.sql.DataSource)

Example 4 with DbRuntimeException

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;
}
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 5 with DbRuntimeException

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);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) Setting(cn.hutool.setting.Setting) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) DbRuntimeException(cn.hutool.db.DbRuntimeException) DbRuntimeException(cn.hutool.db.DbRuntimeException) NotInitedException(cn.hutool.core.exceptions.NotInitedException)

Aggregations

DbRuntimeException (cn.hutool.db.DbRuntimeException)23 Setting (cn.hutool.setting.Setting)8 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)4 DatabaseMetaData (java.sql.DatabaseMetaData)4 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 NotInitedException (cn.hutool.core.exceptions.NotInitedException)2 SqlBuilder (cn.hutool.db.sql.SqlBuilder)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)2 MongoClient (com.mongodb.MongoClient)2 MongoCredential (com.mongodb.MongoCredential)2 Blob (java.sql.Blob)2 Properties (java.util.Properties)2 StrUtil (cn.hutool.core.util.StrUtil)1 DataSourceWrapper (cn.hutool.db.ds.DataSourceWrapper)1 SimpleDataSource (cn.hutool.db.ds.simple.SimpleDataSource)1 Props (cn.hutool.setting.dialect.Props)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 ServerAddress (com.mongodb.ServerAddress)1