Java Code Examples for com.alibaba.druid.pool.DruidDataSource#setConnectionInitSqls()

The following examples show how to use com.alibaba.druid.pool.DruidDataSource#setConnectionInitSqls() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DbNodeServiceImpl.java    From pmq with Apache License 2.0 5 votes vote down vote up
public void initDataSource(DbNodeEntity t1, String key, Map<String, DataSource> dataMap, boolean isMaster,
		Transaction transaction) {
	try {
		// if (soaConfig.isUseDruid())
		{
			DruidDataSource dataSource = dataSourceFactory.createDataSource();
			dataSource.setDriverClassName("com.mysql.jdbc.Driver");
			if (isMaster) {
				dataSource.setUsername(t1.getDbUserName());
				dataSource.setPassword(t1.getDbPass());
			} else {
				dataSource.setUsername(t1.getDbUserNameBak());
				dataSource.setPassword(t1.getDbPassBak());
			}
			// dataSource.setUrl(t1.getConStr());
			dataSource.setUrl(getCon(t1, isMaster));
			dataSource.setInitialSize(soaConfig.getInitDbCount());
			dataSource.setMinIdle(soaConfig.getInitDbCount());
			dataSource.setMaxActive(soaConfig.getMaxDbCount());
			dataSource.setConnectionInitSqls(Arrays.asList("set names utf8mb4;"));
			dataSource.init();
			dbCreated.put(key, true);
			log.info(dataSource.getUrl() + "数据源创建成功!dataSource_created");
			dataMap.put(key, dataSource);
			transaction.setStatus(Transaction.SUCCESS);
		}
	} catch (Exception e) {
		transaction.setStatus(e);
		log.error("initDataSource_error", e);
	}
}
 
Example 2
Source File: DruidDataSourceFactory.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource createDataSource(DataSourceConfig config) {

    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setUrl(config.getUrl());
    druidDataSource.setUsername(config.getUser());
    druidDataSource.setPassword(config.getPassword());
    druidDataSource.setDriverClassName(config.getDriverClassName());
    druidDataSource.setMaxActive(config.getMaximumPoolSize());
    druidDataSource.setMaxWait(config.getMaxWait());
    druidDataSource.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis());
    druidDataSource.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
    druidDataSource.setTimeBetweenConnectErrorMillis(config.getTimeBetweenConnectErrorMillis());
    druidDataSource.setValidationQuery(config.getValidationQuery());
    druidDataSource.setTestWhileIdle(config.isTestWhileIdle());
    druidDataSource.setTestOnBorrow(config.isTestOnBorrow());
    druidDataSource.setTestOnReturn(config.isTestOnReturn());
    if (config.getMinimumIdle() != null) {
        druidDataSource.setMinIdle(config.getMinimumIdle());
    }

    if (config.getConnectionInitSql() != null) {
        druidDataSource.setConnectionInitSqls(Sets.newHashSet(config.getConnectionInitSql()));
    }

    try {
        druidDataSource.setFilters("stat");
    } catch (SQLException e) {
        log.error("DruidDataSourceFactory is error", e);
    }


    return druidDataSource;
}
 
Example 3
Source File: DruidDatasourceProvider.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JdbcDataSource createDataSource(DatasourceRootConfig.DatasourceConfig config) {
  String username = config.getUser();
  String password = config.getPassword();
  String url = config.getUrl();
  String dbType = config.getDbType();
  int maxRetryCount = config.getMaxRetryCount();
  List<String> initSQLs = config.getInitSqls();

  int maxCon = config.getMaxCon();
  int minCon = config.getMinCon();

  DruidDataSource datasource = new DruidDataSource();
  datasource.setPassword(password);
  datasource.setUsername(username);
  datasource.setUrl(url);
  datasource.setMaxWait(TimeUnit.SECONDS.toMillis(1));
  datasource.setMaxActive(maxCon);
  datasource.setMinIdle(minCon);

  if (maxRetryCount > 0) {
    datasource.setConnectionErrorRetryAttempts(maxRetryCount);
  }
  if (dbType != null) {
    datasource.setDbType(dbType);
  }
  if (initSQLs != null) {
    datasource.setConnectionInitSqls(initSQLs);
  }

  return new JdbcDataSource(config,datasource);
}
 
Example 4
Source File: DruidDataSourcePool.java    From Zebra with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public DataSource build(DataSourceConfig config, boolean withDefaultValue) {
	DruidDataSource druidDataSource = new DruidDataSource();

	druidDataSource.setName(buildDsName(config));
	druidDataSource.setUrl(config.getJdbcUrl());
	druidDataSource.setUsername(config.getUsername());
	druidDataSource.setPassword(config.getPassword());
	druidDataSource.setDriverClassName(StringUtils.isNotBlank(config.getDriverClass()) ? config.getDriverClass()
	      : JdbcDriverClassHelper.getDriverClassNameByJdbcUrl(config.getJdbcUrl()));

	if (withDefaultValue) {
		druidDataSource.setInitialSize(getIntProperty(config, "initialPoolSize", 5));
		druidDataSource.setMaxActive(getIntProperty(config, "maxPoolSize", 30));
		druidDataSource.setMinIdle(getIntProperty(config, "minPoolSize", 5));
		druidDataSource.setMaxWait(getIntProperty(config, "checkoutTimeout", 1000));
		druidDataSource.setValidationQuery(getStringProperty(config, "preferredTestQuery", "SELECT 1"));
		druidDataSource.setMinEvictableIdleTimeMillis(getIntProperty(config, "minEvictableIdleTimeMillis", 1800000));// 30min
		druidDataSource
		      .setTimeBetweenEvictionRunsMillis(getIntProperty(config, "timeBetweenEvictionRunsMillis", 30000)); // 30s
		druidDataSource.setNumTestsPerEvictionRun(getIntProperty(config, "numTestsPerEvictionRun", 6));
		druidDataSource.setValidationQueryTimeout(getIntProperty(config, "validationQueryTimeout", 0));
		druidDataSource.setRemoveAbandonedTimeout(getIntProperty(config, "removeAbandonedTimeout", 300));
		if (StringUtils.isNotBlank(getStringProperty(config, "connectionInitSql", null))) {
			List<String> initSqls = new ArrayList<String>();
			initSqls.add(getStringProperty(config, "connectionInitSql", null));
			druidDataSource.setConnectionInitSqls(initSqls);
		}

		druidDataSource.setTestWhileIdle(true);
		druidDataSource.setTestOnBorrow(false);
		druidDataSource.setTestOnReturn(false);
		druidDataSource.setRemoveAbandoned(true);
		druidDataSource.setUseUnfairLock(true);
		druidDataSource.setNotFullTimeoutRetryCount(-1);
	} else {
		try {
			PropertiesInit<DruidDataSource> propertiesInit = new PropertiesInit<DruidDataSource>(druidDataSource);
			propertiesInit.initPoolProperties(config);
		} catch (Exception e) {
			throw new ZebraConfigException(String.format("druid dataSource [%s] created error : ", config.getId()), e);
		}
	}

	this.pool = druidDataSource;
	LOGGER.info(String.format("New dataSource [%s] created.", config.getId()));

	return this.pool;
}