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

The following examples show how to use com.alibaba.druid.pool.DruidDataSource#setName() . 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: DynamicDataSource.java    From spring-boot-starter-dao with Apache License 2.0 6 votes vote down vote up
public DynamicDataSource(MybatisNodeProperties druidNode, DruidProperties defaultDruidProperties, String dataSourceName) throws SQLException {
	this.dataSourceName=dataSourceName;
	DruidProperties master = druidNode.getMaster();
	if (master == null)
		master = new DruidProperties();
	master.merge(defaultDruidProperties).defaultEmpty().setDefaultReadOnly(false);
	this.masterDataSource = master.createDataSource();
	this.masterDataSource.setName(dataSourceName + "-Master");
	List<DruidProperties> slaves = druidNode.getSlaves();
	if (slaves != null && !slaves.isEmpty()) {
		for (int i = 0; i < slaves.size(); i++) {
			DruidProperties slave = slaves.get(i);
			if (slave == null)
				continue;
			slave.merge(defaultDruidProperties).defaultEmpty().setDefaultReadOnly(true);
			String slaveDatasourceName = dataSourceName + "-Slave-" + i;
			this.slavesDataSourceNames.add(slaveDatasourceName);
			DruidDataSource datasourc = slave.createDataSource();
			datasourc.setName(slaveDatasourceName);
			this.slaveDataSources.put(slaveDatasourceName, datasourc);
		}
	}
	String rawUrl=master.getUrl();
	String dbType = JdbcUtils.getDbType(rawUrl,null);
	this.dialect=Dialect.valoueOfName(dbType);
}
 
Example 2
Source File: DruidHealthTracker.java    From pepper-metrics with Apache License 2.0 5 votes vote down vote up
/**
 * 添加要监控的Druid数据源
 * @param namespace         区别数据源的命名空间
 * @param druidDataSource   Druid数据源实例(需要在用户应用中创建)
 */
public static void addDataSource(String namespace, DruidDataSource druidDataSource) {
    Assert.assertNotNull(namespace);
    Assert.assertFalse("Duplicate datasource name error.", UNIQUE_NAME.contains(namespace));
    UNIQUE_NAME.add(namespace);
    druidDataSource.setName(namespace);
    DruidHealthStats stats = new DruidHealthStats(MetricsRegistry.getREGISTRY(), namespace, druidDataSource);
    HealthTracker.addStats(stats);
}
 
Example 3
Source File: DruidProvider.java    From tastjava with MIT License 5 votes vote down vote up
@Override
public boolean register() {
    if (IsRegistered) {
        return HAS_REGISTERED;
    }

    DruidDataSource ds = new DruidDataSource();

    if (!StrUtil.isBlank(name)) {
        ds.setName(this.name);
    }

    ds.setUrl(jdbcURL);
    ds.setDriverClassName(jdbcDriver);
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    ds.setMinIdle(minIdle);
    ds.setMaxActive(maxActive);
    ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    ds.setValidationQuery(validationQuery);

    ds.setTestWhileIdle(testWhileIdle);
    ds.setTestOnBorrow(testOnBorrow);
    ds.setTestOnReturn(testOnReturn);

    setDataSource(ds);
    setIsRegistered(HAS_REGISTERED);
    return HAS_REGISTERED;
}
 
Example 4
Source File: DataDruidFactory.java    From DataSphereStudio with Apache License 2.0 4 votes vote down vote up
private static DruidDataSource createDataSource(Properties props, Logger log, String type) {
		String name = null;
		String url = null;
		String username = null;
		String password = null;
		
		if (type.equals("Job")) {
			name = props.getProperty("job.datachecker.jdo.option.name");
			url = props.getProperty("job.datachecker.jdo.option.url");
			username = props.getProperty("job.datachecker.jdo.option.username");
			password = props.getProperty("job.datachecker.jdo.option.password");
			try {
//				password = new String(Base64.getDecoder().decode(props.getProperty("job.datachecker.jdo.option.password").getBytes()),"UTF-8");
				password = props.getProperty("job.datachecker.jdo.option.password");
			} catch (Exception e){
				log.error("password decore failed" + e);
			}
		}
		int initialSize = Integer.valueOf(props.getProperty("datachecker.jdo.option.initial.size", "1"));
		int maxActive = Integer.valueOf(props.getProperty("datachecker.jdo.option.max.active", "100"));
		int minIdle = Integer.valueOf(props.getProperty("datachecker.jdo.option.min.idle", "1"));
		long maxWait = Long.valueOf(props.getProperty("datachecker.jdo.option.max.wait", "60000"));
		String validationQuery = props.getProperty("datachecker.jdo.option.validation.quert", "SELECT 'x'");
		long timeBetweenEvictionRunsMillis = Long.valueOf(props.getProperty("datachecker.jdo.option.time.between.eviction.runs.millis", "6000"));
		long minEvictableIdleTimeMillis = Long.valueOf(props.getProperty("datachecker.jdo.option.evictable.idle,time.millis", "300000"));
		boolean testOnBorrow = Boolean.valueOf(props.getProperty("datachecker.jdo.option.test.on.borrow", "true"));
		int maxOpenPreparedStatements = Integer.valueOf(props.getProperty("datachecker.jdo.option.max.open.prepared.statements", "-1"));
		
		
		if (timeBetweenEvictionRunsMillis > minEvictableIdleTimeMillis) {
			timeBetweenEvictionRunsMillis = minEvictableIdleTimeMillis;
		}
		
		DruidDataSource ds = new DruidDataSource();
		
		if (StringUtils.isNotBlank(name)) {
			ds.setName(name);
		}
		
		ds.setUrl(url);
		ds.setDriverClassName("com.mysql.jdbc.Driver");
	    ds.setUsername(username);
	    ds.setPassword(password);
	    ds.setInitialSize(initialSize);
	    ds.setMinIdle(minIdle);
	    ds.setMaxActive(maxActive);
	    ds.setMaxWait(maxWait);
	    ds.setTestOnBorrow(testOnBorrow);
	    ds.setValidationQuery(validationQuery);
	    ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
	    ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
	    if (maxOpenPreparedStatements > 0) {
	      ds.setPoolPreparedStatements(true);
	      ds.setMaxPoolPreparedStatementPerConnectionSize(
	          maxOpenPreparedStatements);
	    } else {
	      ds.setPoolPreparedStatements(false);
	    }
	    log.info("Druid data source initialed!");
	    return ds;
	}
 
Example 5
Source File: EventDruidFactory.java    From DataSphereStudio with Apache License 2.0 4 votes vote down vote up
private static DruidDataSource createDataSource(Properties props, Logger log, String type) {
		String name = null;
		String url = null;
		String username = null;
		String password = null;
		
		if(type.equals("Msg")){
			name = props.getProperty("msg.eventchecker.jdo.option.name");
			url = props.getProperty("msg.eventchecker.jdo.option.url");
			username = props.getProperty("msg.eventchecker.jdo.option.username");
			try {
//				password = new String(Base64.getDecoder().decode(props.getProperty("msg.eventchecker.jdo.option.password").getBytes()),"UTF-8");
				password = props.getProperty("msg.eventchecker.jdo.option.password");
			} catch (Exception e){
				log.error("password decore failed" + e);
			}
		}
		
		int initialSize = Integer.valueOf(props.getProperty("option.initial.size", "1"));
		int maxActive = Integer.valueOf(props.getProperty("option.max.active", "100"));
		int minIdle = Integer.valueOf(props.getProperty("option.min.idle", "1"));
		long maxWait = Long.valueOf(props.getProperty("option.max.wait", "60000"));
		String validationQuery = props.getProperty("option.validation.quert", "SELECT 'x'");
		long timeBetweenEvictionRunsMillis = Long.valueOf(props.getProperty("option.time.between.eviction.runs.millis", "6000"));
		long minEvictableIdleTimeMillis = Long.valueOf(props.getProperty("option.evictable.idle,time.millis", "300000"));
		boolean testOnBorrow = Boolean.valueOf(props.getProperty("option.test.on.borrow", "true"));
		int maxOpenPreparedStatements = Integer.valueOf(props.getProperty("option.max.open.prepared.statements", "-1"));

		if (timeBetweenEvictionRunsMillis > minEvictableIdleTimeMillis) {
			timeBetweenEvictionRunsMillis = minEvictableIdleTimeMillis;
		}
		
		DruidDataSource ds = new DruidDataSource();
		
		if (StringUtils.isNotBlank(name)) {
			ds.setName(name);
		}
		
		ds.setUrl(url);
		ds.setDriverClassName("com.mysql.jdbc.Driver");
	    ds.setUsername(username);
	    ds.setPassword(password);
	    ds.setInitialSize(initialSize);
	    ds.setMinIdle(minIdle);
	    ds.setMaxActive(maxActive);
	    ds.setMaxWait(maxWait);
	    ds.setTestOnBorrow(testOnBorrow);
	    ds.setValidationQuery(validationQuery);
	    ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
	    ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
	    if (maxOpenPreparedStatements > 0) {
	      ds.setPoolPreparedStatements(true);
	      ds.setMaxPoolPreparedStatementPerConnectionSize(
	          maxOpenPreparedStatements);
	    } else {
	      ds.setPoolPreparedStatements(false);
	    }
	    log.info("Druid data source initialed!");
	    return ds;
	}
 
Example 6
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;
}