com.alibaba.druid.pool.DruidDataSourceFactory Java Examples

The following examples show how to use com.alibaba.druid.pool.DruidDataSourceFactory. 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: DruidDataSourceUtil.java    From sqlfly with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 获取数据源 
 */
public static synchronized DataSource getDataSource() {
	try {
		if(dataSource == null) {
			// 加载配置文件 
			InputStream is = DruidDataSourceUtil.class.getClassLoader().getResourceAsStream(propertiesPath);
		    Properties properties = new Properties();
		    properties.load(is);
		    // 创建连接池对象
		    DruidDataSourceUtil.dataSource = DruidDataSourceFactory.createDataSource(properties);
		}
		return dataSource;
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #2
Source File: EventProcessor.java    From openmessaging-connect-odar with Apache License 2.0 6 votes vote down vote up
private void initDataSource() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put("driverClassName", "com.mysql.jdbc.Driver");
    map.put("url", "jdbc:mysql://" + config.mysqlAddr + ":" + config.mysqlPort + "?useSSL=true&verifyServerCertificate=false");
    map.put("username", config.mysqlUsername);
    map.put("password", config.mysqlPassword);
    map.put("initialSize", "2");
    map.put("maxActive", "2");
    map.put("maxWait", "60000");
    map.put("timeBetweenEvictionRunsMillis", "60000");
    map.put("minEvictableIdleTimeMillis", "300000");
    map.put("validationQuery", "SELECT 1 FROM DUAL");
    map.put("testWhileIdle", "true");

    dataSource = DruidDataSourceFactory.createDataSource(map);
}
 
Example #3
Source File: MyBatisConfig.java    From cc-s with MIT License 6 votes vote down vote up
private DataSource dataSource() {
    Map<String,Object> properties=new HashMap<>();
    properties.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME,env.getProperty("spring.datasource.driverClassName"));
    properties.put(DruidDataSourceFactory.PROP_URL,env.getProperty("spring.datasource.url"));
    properties.put(DruidDataSourceFactory.PROP_USERNAME,env.getProperty("spring.datasource.username"));
    properties.put(DruidDataSourceFactory.PROP_PASSWORD,env.getProperty("spring.datasource.password"));
    //添加统计、SQL注入、日志过滤器
    properties.put(DruidDataSourceFactory.PROP_FILTERS,env.getProperty("druid.propFilters"));
    //sql合并,慢查询定义为5s
    properties.put(DruidDataSourceFactory.PROP_CONNECTIONPROPERTIES,env.getProperty("druid.propConnectionProperties"));
    try {
        return DruidDataSourceFactory.createDataSource(properties);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #4
Source File: DataSourceContainer.java    From DBus with Apache License 2.0 5 votes vote down vote up
public synchronized boolean register(String key, Properties properties) {
    boolean isOk = true;
    try {
        if (!cmap.containsKey(key))
            cmap.put(key, DruidDataSourceFactory.createDataSource(properties));
    } catch (Exception e) {
        logger.error("db container init key " + key + " datasource error!", e);
        isOk = false;
    }
    return isOk;
}
 
Example #5
Source File: DataSourceContainer.java    From DBus with Apache License 2.0 5 votes vote down vote up
public synchronized boolean register(String key, Properties properties) {
    boolean isOk = true;
    try {
        if (!cmap.containsKey(key))
            cmap.put(key, DruidDataSourceFactory.createDataSource(properties));
    } catch (Exception e) {
        logger.error("db container init key " + key + " datasource error!", e);
        isOk = false;
    }
    return isOk;
}
 
Example #6
Source File: MyBatisConfig.java    From springboot-multiple-dataSources with Apache License 2.0 5 votes vote down vote up
/**
 * 创建数据源(数据源的名称:方法名可以取为XXXDataSource(),XXX为数据库名称,该名称也就是数据源的名称)
 */
@Bean
public DataSource test1DataSource() throws Exception {
    Properties props = new Properties();
    props.put("driverClassName", environment.getProperty("test1-datasource.driverClassName"));
    props.put("url", environment.getProperty("test1-datasource.url"));
    props.put("username", environment.getProperty("test1-datasource.username"));
    props.put("password", environment.getProperty("test1-datasource.password"));
    return DruidDataSourceFactory.createDataSource(props);
}
 
Example #7
Source File: MyBatisConfig.java    From springboot-multiple-dataSources with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource test2DataSource() throws Exception {
    Properties props = new Properties();
    props.put("driverClassName", environment.getProperty("test2-datasource.driverClassName"));
    props.put("url", environment.getProperty("test2-datasource.url"));
    props.put("username", environment.getProperty("test2-datasource.username"));
    props.put("password", environment.getProperty("test2-datasource.password"));
    return DruidDataSourceFactory.createDataSource(props);
}
 
Example #8
Source File: DataBaseMgr.java    From Summer with Apache License 2.0 4 votes vote down vote up
public void loadConfig(InputStream in) throws Exception {
	Properties properties = new Properties();
	properties.load(in);
	dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
}
 
Example #9
Source File: DataBaseMgr.java    From Summer with Apache License 2.0 4 votes vote down vote up
public void loadConfigForOther(String topic, InputStream in) throws Exception {
	Objects.requireNonNull(topic, "topic not null");
	Properties properties = new Properties();
	properties.load(in);
	otherDataSourceMap.put(topic, (DruidDataSource) DruidDataSourceFactory.createDataSource(properties));
}
 
Example #10
Source File: DruidDataSourceProvider.java    From DBus with Apache License 2.0 4 votes vote down vote up
public DruidDataSourceProvider(Properties properties) throws Exception {
    ds = DruidDataSourceFactory.createDataSource(properties);
}
 
Example #11
Source File: DruidDataSourceProvider.java    From DBus with Apache License 2.0 4 votes vote down vote up
public DruidDataSourceProvider(Properties properties) throws Exception {
    ds = DruidDataSourceFactory.createDataSource(properties);
}
 
Example #12
Source File: JdbcConnectionPoll.java    From waterdrop with Apache License 2.0 4 votes vote down vote up
private JdbcConnectionPoll(Properties properties) throws Exception {
    dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
}
 
Example #13
Source File: DruidConnectionPool.java    From ServerCore with Apache License 2.0 4 votes vote down vote up
public DruidConnectionPool(String configFile) throws Exception {
    InputStreamReader in = FileLoaderUtil.findInputStreamByFileName(configFile);
    Properties props = new Properties();
    props.load(in);
    ds = DruidDataSourceFactory.createDataSource(props);
}