Java Code Examples for org.apache.commons.dbcp2.BasicDataSource#setConnectionInitSqls()
The following examples show how to use
org.apache.commons.dbcp2.BasicDataSource#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: DataSourceConfigurationTest.java From shardingsphere with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Test public void assertGetDataSourceConfigurationWithConnectionInitSqls() { BasicDataSource actualDataSource = new BasicDataSource(); actualDataSource.setDriverClassName("org.h2.Driver"); actualDataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"); actualDataSource.setUsername("root"); actualDataSource.setPassword("root"); actualDataSource.setConnectionInitSqls(Arrays.asList("set names utf8mb4;", "set names utf8;")); DataSourceConfiguration actual = DataSourceConfiguration.getDataSourceConfiguration(actualDataSource); assertThat(actual.getDataSourceClassName(), is(BasicDataSource.class.getName())); assertThat(actual.getProps().get("driverClassName").toString(), is("org.h2.Driver")); assertThat(actual.getProps().get("url").toString(), is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL")); assertThat(actual.getProps().get("username").toString(), is("root")); assertThat(actual.getProps().get("password").toString(), is("root")); assertNull(actual.getProps().get("loginTimeout")); assertThat(actual.getProps().get("connectionInitSqls"), instanceOf(List.class)); List<String> actualConnectionInitSql = (List<String>) actual.getProps().get("connectionInitSqls"); assertThat(actualConnectionInitSql, hasItem("set names utf8mb4;")); assertThat(actualConnectionInitSql, hasItem("set names utf8;")); }
Example 2
Source File: ConfigCenterTest.java From shardingsphere with Apache License 2.0 | 5 votes |
private DataSource createDataSourceWithConnectionInitSqls(final String name) { BasicDataSource result = new BasicDataSource(); result.setDriverClassName("com.mysql.jdbc.Driver"); result.setUrl("jdbc:mysql://localhost:3306/" + name); result.setUsername("root"); result.setPassword("root"); result.setConnectionInitSqls(Arrays.asList("set names utf8mb4;", "set names utf8;")); return result; }
Example 3
Source File: DataSourceUtil.java From shardingsphere with Apache License 2.0 | 5 votes |
private static DataSource createDBCP(final DatabaseType databaseType, final String dataSourceName) { BasicDataSource result = new BasicDataSource(); DatabaseEnvironment databaseEnvironment = IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().get(databaseType); result.setDriverClassName(databaseEnvironment.getDriverClassName()); result.setUrl(null == dataSourceName ? databaseEnvironment.getURL() : databaseEnvironment.getURL(dataSourceName)); result.setUsername(databaseEnvironment.getUsername()); result.setPassword(databaseEnvironment.getPassword()); result.setMaxTotal(2); result.setValidationQuery("SELECT 1"); if ("Oracle".equals(databaseType.getName())) { result.setConnectionInitSqls(Collections.singleton("ALTER SESSION SET CURRENT_SCHEMA = " + dataSourceName)); } return result; }
Example 4
Source File: JdbcIO.java From beam with Apache License 2.0 | 5 votes |
DataSource buildDatasource() { if (getDataSource() == null) { BasicDataSource basicDataSource = new BasicDataSource(); if (getDriverClassName() != null) { basicDataSource.setDriverClassName(getDriverClassName().get()); } if (getUrl() != null) { basicDataSource.setUrl(getUrl().get()); } if (getUsername() != null) { basicDataSource.setUsername(getUsername().get()); } if (getPassword() != null) { basicDataSource.setPassword(getPassword().get()); } if (getConnectionProperties() != null && getConnectionProperties().get() != null) { basicDataSource.setConnectionProperties(getConnectionProperties().get()); } if (getConnectionInitSqls() != null && getConnectionInitSqls().get() != null && !getConnectionInitSqls().get().isEmpty()) { basicDataSource.setConnectionInitSqls(getConnectionInitSqls().get()); } return basicDataSource; } return getDataSource(); }
Example 5
Source File: Dbcp2DataSourcePool.java From Zebra with Apache License 2.0 | 4 votes |
@Override public DataSource build(DataSourceConfig config, boolean withDefaultValue) { BasicDataSource dbcp2DataSource = new BasicDataSource(); dbcp2DataSource.setUrl(config.getJdbcUrl()); dbcp2DataSource.setUsername(config.getUsername()); dbcp2DataSource.setPassword(config.getPassword()); dbcp2DataSource.setDriverClassName(StringUtils.isNotBlank(config.getDriverClass()) ? config.getDriverClass() : JdbcDriverClassHelper.getDriverClassNameByJdbcUrl(config.getJdbcUrl())); if (withDefaultValue) { dbcp2DataSource.setInitialSize(getIntProperty(config, "initialPoolSize", 5)); dbcp2DataSource.setMaxTotal(getIntProperty(config, "maxPoolSize", 30)); dbcp2DataSource.setMinIdle(getIntProperty(config, "minPoolSize", 5)); dbcp2DataSource.setMaxIdle(getIntProperty(config, "maxPoolSize", 20)); dbcp2DataSource.setMaxWaitMillis(getIntProperty(config, "checkoutTimeout", 1000)); dbcp2DataSource.setValidationQuery(getStringProperty(config, "preferredTestQuery", "SELECT 1")); dbcp2DataSource.setMinEvictableIdleTimeMillis(getIntProperty(config, "minEvictableIdleTimeMillis", 1800000));// 30min dbcp2DataSource .setTimeBetweenEvictionRunsMillis(getIntProperty(config, "timeBetweenEvictionRunsMillis", 30000)); // 30s dbcp2DataSource.setRemoveAbandonedTimeout(getIntProperty(config, "removeAbandonedTimeout", 300)); // 30s dbcp2DataSource.setNumTestsPerEvictionRun(getIntProperty(config, "numTestsPerEvictionRun", 6)); // 30s dbcp2DataSource.setValidationQueryTimeout(getIntProperty(config, "validationQueryTimeout", 0)); if (StringUtils.isNotBlank(getStringProperty(config, "connectionInitSql", null))) { List<String> initSqls = new ArrayList<String>(); initSqls.add(getStringProperty(config, "connectionInitSql", null)); dbcp2DataSource.setConnectionInitSqls(initSqls); } dbcp2DataSource.setTestWhileIdle(true); dbcp2DataSource.setTestOnBorrow(false); dbcp2DataSource.setTestOnReturn(false); dbcp2DataSource.setRemoveAbandonedOnBorrow(true); dbcp2DataSource.setRemoveAbandonedOnMaintenance(true); } else { try { PropertiesInit<BasicDataSource> propertiesInit = new PropertiesInit<BasicDataSource>(dbcp2DataSource); propertiesInit.initPoolProperties(config); } catch (Exception e) { throw new ZebraConfigException(String.format("dbcp2 dataSource [%s] created error : ", config.getId()), e); } } this.pool = dbcp2DataSource; LOGGER.info(String.format("New dataSource [%s] created.", config.getId())); return this.pool; }