Search in sources :

Example 11 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource in project spring-boot-quick by vector4wang.

the class DataSourcePrimaryConfig method testDataSource.

@Bean(name = "primaryDataSource", initMethod = "init", destroyMethod = "close")
@Primary
public DataSource testDataSource() throws SQLException {
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setDriverClassName(druidConfigPrimaryProperties.getDriverClassName());
    druidDataSource.setUrl(druidConfigPrimaryProperties.getUrl());
    druidDataSource.setUsername(druidConfigPrimaryProperties.getUsername());
    druidDataSource.setPassword(druidConfigPrimaryProperties.getPassword());
    druidDataSource.setInitialSize(druidConfigPrimaryProperties.getMinIdle());
    druidDataSource.setMinIdle(druidConfigPrimaryProperties.getMinIdle());
    druidDataSource.setMaxActive(druidConfigPrimaryProperties.getMaxActive());
    druidDataSource.setMaxWait(druidConfigPrimaryProperties.getMaxWait());
    druidDataSource.setTimeBetweenEvictionRunsMillis(druidConfigPrimaryProperties.getTimeBetweenEvictionRunsMillis());
    druidDataSource.setMinEvictableIdleTimeMillis(druidConfigPrimaryProperties.getMinEvictableIdleTimeMillis());
    druidDataSource.setValidationQuery(druidConfigPrimaryProperties.getValidationQuery());
    druidDataSource.setTestWhileIdle(druidConfigPrimaryProperties.getTestWhileIdle());
    druidDataSource.setTestOnBorrow(druidConfigPrimaryProperties.getTestOnBorrow());
    druidDataSource.setTestOnReturn(druidConfigPrimaryProperties.getTestOnReturn());
    druidDataSource.setPoolPreparedStatements(druidConfigPrimaryProperties.getPoolPreparedStatements());
    druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidConfigPrimaryProperties.getMaxPoolPreparedStatementPerConnectionSize());
    druidDataSource.setFilters(druidConfigPrimaryProperties.getFilters());
    return druidDataSource;
}
Also used : DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Primary(org.springframework.context.annotation.Primary) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 12 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource in project spring-boot-quick by vector4wang.

the class DataSourceSecondaryConfig method testDataSource.

@Bean(name = "secondaryDataSource", initMethod = "init", destroyMethod = "close")
public DataSource testDataSource() throws SQLException {
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setDriverClassName(druidConfigSecondaryProperties.getDriverClassName());
    druidDataSource.setUrl(druidConfigSecondaryProperties.getUrl());
    druidDataSource.setUsername(druidConfigSecondaryProperties.getUsername());
    druidDataSource.setPassword(druidConfigSecondaryProperties.getPassword());
    druidDataSource.setInitialSize(druidConfigSecondaryProperties.getMinIdle());
    druidDataSource.setMinIdle(druidConfigSecondaryProperties.getMinIdle());
    druidDataSource.setMaxActive(druidConfigSecondaryProperties.getMaxActive());
    druidDataSource.setMaxWait(druidConfigSecondaryProperties.getMaxWait());
    druidDataSource.setTimeBetweenEvictionRunsMillis(druidConfigSecondaryProperties.getTimeBetweenEvictionRunsMillis());
    druidDataSource.setMinEvictableIdleTimeMillis(druidConfigSecondaryProperties.getMinEvictableIdleTimeMillis());
    druidDataSource.setValidationQuery(druidConfigSecondaryProperties.getValidationQuery());
    druidDataSource.setTestWhileIdle(druidConfigSecondaryProperties.getTestWhileIdle());
    druidDataSource.setTestOnBorrow(druidConfigSecondaryProperties.getTestOnBorrow());
    druidDataSource.setTestOnReturn(druidConfigSecondaryProperties.getTestOnReturn());
    druidDataSource.setPoolPreparedStatements(druidConfigSecondaryProperties.getPoolPreparedStatements());
    druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidConfigSecondaryProperties.getMaxPoolPreparedStatementPerConnectionSize());
    druidDataSource.setFilters(druidConfigSecondaryProperties.getFilters());
    return druidDataSource;
}
Also used : DruidDataSource(com.alibaba.druid.pool.DruidDataSource) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 13 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource in project cloud-sea-towerman by huadahuang1983.

the class DataSourceConfig method druidDataSource.

@Bean
@Primary
public DataSource druidDataSource() {
    DruidDataSource datasource = new DruidDataSource();
    datasource.setUrl(this.dbUrl);
    datasource.setUsername(username);
    datasource.setPassword(password);
    datasource.setDriverClassName(driverClassName);
    datasource.setInitialSize(initialSize);
    datasource.setMinIdle(minIdle);
    datasource.setMaxActive(maxActive);
    datasource.setMaxWait(maxWait);
    datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    datasource.setValidationQuery(validationQuery);
    datasource.setTestWhileIdle(testWhileIdle);
    datasource.setTestOnBorrow(testOnBorrow);
    datasource.setTestOnReturn(testOnReturn);
    datasource.setPoolPreparedStatements(poolPreparedStatements);
    try {
        datasource.setFilters(filters);
    } catch (SQLException e) {
        logger.error("druid configuration initialization filter", e);
    }
    return datasource;
}
Also used : SQLException(java.sql.SQLException) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Primary(org.springframework.context.annotation.Primary) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Example 14 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource in project jfinal by jfinal.

the class DruidPlugin method start.

public boolean start() {
    if (isStarted)
        return true;
    ds = new DruidDataSource();
    if (this.name != null) {
        ds.setName(this.name);
    }
    ds.setUrl(url);
    ds.setUsername(username);
    ds.setPassword(password);
    if (driverClass != null)
        ds.setDriverClassName(driverClass);
    ds.setInitialSize(initialSize);
    ds.setMinIdle(minIdle);
    ds.setMaxActive(maxActive);
    ds.setMaxWait(maxWait);
    ds.setTimeBetweenConnectErrorMillis(timeBetweenConnectErrorMillis);
    ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    ds.setValidationQuery(validationQuery);
    if (StrKit.notBlank(connectionInitSql)) {
        List<String> connectionInitSqls = new ArrayList<String>();
        connectionInitSqls.add(this.connectionInitSql);
        ds.setConnectionInitSqls(connectionInitSqls);
    }
    ds.setTestWhileIdle(testWhileIdle);
    ds.setTestOnBorrow(testOnBorrow);
    ds.setTestOnReturn(testOnReturn);
    ds.setRemoveAbandoned(removeAbandoned);
    ds.setRemoveAbandonedTimeoutMillis(removeAbandonedTimeoutMillis);
    ds.setLogAbandoned(logAbandoned);
    // 只要maxPoolPreparedStatementPerConnectionSize>0,poolPreparedStatements就会被自动设定为true,参照druid的源码
    ds.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
    if (defaultTransactionIsolation != null) {
        ds.setDefaultTransactionIsolation(defaultTransactionIsolation);
    }
    if (validationQueryTimeout != null) {
        ds.setValidationQueryTimeout(validationQueryTimeout);
    }
    if (timeBetweenLogStatsMillis != null) {
        ds.setTimeBetweenLogStatsMillis(timeBetweenLogStatsMillis);
    }
    if (keepAlive != null) {
        ds.setKeepAlive(keepAlive);
    }
    boolean hasSetConnectionProperties = false;
    if (StrKit.notBlank(filters)) {
        try {
            ds.setFilters(filters);
            // 支持加解密数据库
            if (filters.contains("config")) {
                // 判断是否设定了公钥
                if (StrKit.isBlank(this.publicKey)) {
                    throw new RuntimeException("Druid连接池的filter设定了config时,必须设定publicKey");
                }
                String decryptStr = "config.decrypt=true;config.decrypt.key=" + this.publicKey;
                String cp = this.connectionProperties;
                if (StrKit.isBlank(cp)) {
                    cp = decryptStr;
                } else {
                    cp = cp + ";" + decryptStr;
                }
                ds.setConnectionProperties(cp);
                hasSetConnectionProperties = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    // 确保setConnectionProperties被调用过一次
    if (!hasSetConnectionProperties && StrKit.notBlank(this.connectionProperties)) {
        ds.setConnectionProperties(this.connectionProperties);
    }
    addFilterList(ds);
    isStarted = true;
    return true;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DruidDataSource(com.alibaba.druid.pool.DruidDataSource)

Example 15 with DruidDataSource

use of com.alibaba.druid.pool.DruidDataSource in project Mybatis-PageHelper by pagehelper.

the class DataSourceNegotiationAutoDialectTest method getDriud.

private DruidDataSource getDriud() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(DRUID);
    dataSource.setUsername("root");
    dataSource.setPassword("password");
    return dataSource;
}
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