Search in sources :

Example 1 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource 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 2 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource in project hutool by looly.

the class DruidDSFactory method close.

@Override
public void close(String group) {
    if (group == null) {
        group = StrUtil.EMPTY;
    }
    DruidDataSource dds = dsMap.get(group);
    if (dds != null) {
        IoUtil.close(dds);
        dsMap.remove(group);
    }
}
Also used : DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 3 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource in project weicoder by wdcode.

the class SessionFactorys method getDataSource.

/**
 * 获得数据源
 * @param name 名称
 * @return 数据源
 */
private DataSource getDataSource(String name) {
    // 声明数据源
    DruidDataSource ds = new DruidDataSource();
    System.setProperty("druid.logType", "log4j2");
    ds.setDriverClassName((DaoParams.getDriver(name)));
    ds.setUrl(DaoParams.getUrl(name));
    ds.setUsername(DaoParams.getUser(name));
    ds.setPassword(DaoParams.getPassword(name));
    ds.setMaxActive(DaoParams.getMaxPoolSize(name));
    ds.setMinIdle(DaoParams.getMinPoolSize(name));
    ds.setValidationQueryTimeout(300000);
    ds.setTimeBetweenEvictionRunsMillis(60000);
    ds.setInitialSize(DaoParams.getInitialPoolSize(name));
    ds.setMaxWait(DaoParams.getMaxIdleTime(name));
    ds.setValidationQuery("SELECT 1");
    // 返回数据源
    return ds;
}
Also used : DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 4 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource in project Resource by lovelifeming.

the class Application method dataSource.

// destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用.
@Bean(destroyMethod = "close")
public DataSource dataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(env.getProperty("spring.datasource.url"));
    // 用户名
    dataSource.setUsername(env.getProperty("spring.datasource.username"));
    // 密码
    dataSource.setPassword(env.getProperty("spring.datasource.password"));
    dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
    // 初始化时建立物理连接的个数
    dataSource.setInitialSize(2);
    // 最大连接池数量
    dataSource.setMaxActive(20);
    // 最小连接池数量
    dataSource.setMinIdle(0);
    // 获取连接时最大等待时间,单位毫秒。
    dataSource.setMaxWait(60000);
    // 用来检测连接是否有效的sql
    dataSource.setValidationQuery("SELECT 1");
    // 申请连接时执行validationQuery检测连接是否有效
    dataSource.setTestOnBorrow(false);
    // 建议配置为true,不影响性能,并且保证安全性。
    dataSource.setTestWhileIdle(true);
    // 是否缓存preparedStatement,也就是PSCache
    dataSource.setPoolPreparedStatements(false);
    return dataSource;
}
Also used : DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Bean(org.springframework.context.annotation.Bean)

Example 5 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource in project nuls by nuls-io.

the class MybatisDbModuleBootstrap method shutdown.

@Override
public void shutdown() {
    if (sqlSessionFactory != null) {
        DruidDataSource druidDataSource = (DruidDataSource) sqlSessionFactory.getConfiguration().getEnvironment().getDataSource();
        druidDataSource.close();
        sqlSessionFactory = null;
    }
}
Also used : DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Aggregations

DruidDataSource (com.alibaba.druid.pool.DruidDataSource)474 SQLException (java.sql.SQLException)74 OracleMockDriver (com.alibaba.druid.test.util.OracleMockDriver)56 OracleExceptionSorter (com.alibaba.druid.pool.vendor.OracleExceptionSorter)50 MockDriver (com.alibaba.druid.mock.MockDriver)49 Connection (java.sql.Connection)43 Properties (java.util.Properties)41 FilterAdapter (com.alibaba.druid.filter.FilterAdapter)35 FilterChain (com.alibaba.druid.filter.FilterChain)33 Bean (org.springframework.context.annotation.Bean)21 ResultSet (java.sql.ResultSet)20 Test (org.junit.Test)19 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)18 PreparedStatement (java.sql.PreparedStatement)14 Primary (org.springframework.context.annotation.Primary)14 MockConnection (com.alibaba.druid.mock.MockConnection)13 ResultSetProxy (com.alibaba.druid.proxy.jdbc.ResultSetProxy)13 Method (java.lang.reflect.Method)11 Map (java.util.Map)11 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)10