Example usage for org.apache.commons.dbcp BasicDataSource getUrl

List of usage examples for org.apache.commons.dbcp BasicDataSource getUrl

Introduction

In this page you can find the example usage for org.apache.commons.dbcp BasicDataSource getUrl.

Prototype

public synchronized String getUrl() 

Source Link

Document

Returns the JDBC connection #url property.

Usage

From source file:org.lucane.server.database.HSQLDBLayer.java

public HSQLDBLayer(DataSource dataSource) {
    this.dataSource = dataSource;
    BasicDataSource bds = (BasicDataSource) dataSource;
    bds.setUrl(getAbsoluteUrl(bds.getUrl()));
}

From source file:org.mybatis.guice.datasource.dbcp.BasicDataSourceProviderTest.java

@Test
public void get() throws Throwable {
    final String driver = "org.mybatis.guice.TestDriver";
    final String url = "jdbc:h2:mem:testdb";
    final String username = "test_user";
    final String password = "test_password";
    final boolean autoCommit = true;
    final Properties driverProperties = new Properties();
    driverProperties.put("my_property", "true");
    final boolean accessToUnderlyingConnectionAllowed = true;
    final String defaultCatalog = "test_catalog";
    final boolean defaultReadOnly = true;
    final int defaultTransactionIsolation = 20;
    final int initialSize = 30;
    final int maxActive = 40;
    final int maxIdle = 50;
    final int maxOpenPreparedStatements = 60;
    final long maxWait = 70;
    final int minIdle = 80;
    final int numTestsPerEvictionRun = 90;
    final boolean poolPreparedStatements = true;
    final boolean testOnBorrow = true;
    final boolean testOnReturn = true;
    final boolean testWhileIdle = true;
    final int timeBetweenEvictionRunsMillis = 100;
    final String validationQuery = "SELECT 1";
    Injector injector = Guice.createInjector(new AbstractModule() {
        @Override//from   w w w .j a v  a 2 s  .  com
        protected void configure() {
            bind(ClassLoader.class).annotatedWith(Names.named("JDBC.driverClassLoader"))
                    .toInstance(driverClassLoader);
            bindConstant().annotatedWith(Names.named("JDBC.driver")).to(driver);
            bindConstant().annotatedWith(Names.named("JDBC.url")).to(url);
            bindConstant().annotatedWith(Names.named("JDBC.username")).to(username);
            bindConstant().annotatedWith(Names.named("JDBC.password")).to(password);
            bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit);
            bind(Properties.class).annotatedWith(Names.named("JDBC.driverProperties"))
                    .toInstance(driverProperties);
            bindConstant().annotatedWith(Names.named("DBCP.accessToUnderlyingConnectionAllowed"))
                    .to(accessToUnderlyingConnectionAllowed);
            bindConstant().annotatedWith(Names.named("DBCP.defaultCatalog")).to(defaultCatalog);
            bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly);
            bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation"))
                    .to(defaultTransactionIsolation);
            bindConstant().annotatedWith(Names.named("DBCP.initialSize")).to(initialSize);
            bindConstant().annotatedWith(Names.named("DBCP.maxActive")).to(maxActive);
            bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle);
            bindConstant().annotatedWith(Names.named("DBCP.maxOpenPreparedStatements"))
                    .to(maxOpenPreparedStatements);
            bindConstant().annotatedWith(Names.named("DBCP.maxWait")).to(maxWait);
            bindConstant().annotatedWith(Names.named("DBCP.minIdle")).to(minIdle);
            bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun);
            bindConstant().annotatedWith(Names.named("DBCP.poolPreparedStatements")).to(poolPreparedStatements);
            bindConstant().annotatedWith(Names.named("DBCP.testOnBorrow")).to(testOnBorrow);
            bindConstant().annotatedWith(Names.named("DBCP.testOnReturn")).to(testOnReturn);
            bindConstant().annotatedWith(Names.named("DBCP.testWhileIdle")).to(testWhileIdle);
            bindConstant().annotatedWith(Names.named("DBCP.timeBetweenEvictionRunsMillis"))
                    .to(timeBetweenEvictionRunsMillis);
            bindConstant().annotatedWith(Names.named("DBCP.validationQuery")).to(validationQuery);
        }
    });
    BasicDataSourceProvider provider = injector.getInstance(BasicDataSourceProvider.class);

    BasicDataSource dataSource = (BasicDataSource) provider.get();

    assertEquals(driverClassLoader, dataSource.getDriverClassLoader());
    assertEquals(driver, dataSource.getDriverClassName());
    assertEquals(url, dataSource.getUrl());
    assertEquals(username, dataSource.getUsername());
    assertEquals(password, dataSource.getPassword());
    assertEquals(autoCommit, dataSource.getDefaultAutoCommit());
    // Cannot test driver properties.
    assertEquals(accessToUnderlyingConnectionAllowed, dataSource.isAccessToUnderlyingConnectionAllowed());
    assertEquals(defaultCatalog, dataSource.getDefaultCatalog());
    assertEquals(defaultReadOnly, dataSource.getDefaultReadOnly());
    assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation());
    assertEquals(initialSize, dataSource.getInitialSize());
    assertEquals(maxActive, dataSource.getMaxActive());
    assertEquals(maxIdle, dataSource.getMaxIdle());
    assertEquals(maxOpenPreparedStatements, dataSource.getMaxOpenPreparedStatements());
    assertEquals(maxWait, dataSource.getMaxWait());
    assertEquals(minIdle, dataSource.getMinIdle());
    assertEquals(numTestsPerEvictionRun, dataSource.getNumTestsPerEvictionRun());
    assertEquals(poolPreparedStatements, dataSource.isPoolPreparedStatements());
    assertEquals(testOnBorrow, dataSource.getTestOnBorrow());
    assertEquals(testOnReturn, dataSource.getTestOnReturn());
    assertEquals(testWhileIdle, dataSource.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, dataSource.getTimeBetweenEvictionRunsMillis());
    assertEquals(validationQuery, dataSource.getValidationQuery());
}

From source file:org.mybatis.guice.datasource.dbcp.BasicDataSourceProviderTest.java

@Test
public void get_OtherValues() throws Throwable {
    final String driver = "org.mybatis.guice.TestDriver2";
    final String url = "jdbc:h2:mem:testdb2";
    final String username = "test_user2";
    final String password = "test_password2";
    final boolean autoCommit = false;
    final Properties driverProperties = new Properties();
    driverProperties.put("my_property", "false");
    final boolean accessToUnderlyingConnectionAllowed = false;
    final String defaultCatalog = "test_catalog2";
    final boolean defaultReadOnly = false;
    final int defaultTransactionIsolation = 21;
    final int initialSize = 31;
    final int maxActive = 41;
    final int maxIdle = 51;
    final int maxOpenPreparedStatements = 61;
    final long maxWait = 71;
    final int minIdle = 81;
    final int numTestsPerEvictionRun = 91;
    final boolean poolPreparedStatements = false;
    final boolean testOnBorrow = false;
    final boolean testOnReturn = false;
    final boolean testWhileIdle = false;
    final int timeBetweenEvictionRunsMillis = 101;
    final String validationQuery = "SELECT 2";
    Injector injector = Guice.createInjector(new AbstractModule() {
        @Override//from w  w  w . j  ava2  s  . c  o  m
        protected void configure() {
            bind(ClassLoader.class).annotatedWith(Names.named("JDBC.driverClassLoader"))
                    .toInstance(driverClassLoader);
            bindConstant().annotatedWith(Names.named("JDBC.driver")).to(driver);
            bindConstant().annotatedWith(Names.named("JDBC.url")).to(url);
            bindConstant().annotatedWith(Names.named("JDBC.username")).to(username);
            bindConstant().annotatedWith(Names.named("JDBC.password")).to(password);
            bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit);
            bind(Properties.class).annotatedWith(Names.named("JDBC.driverProperties"))
                    .toInstance(driverProperties);
            bindConstant().annotatedWith(Names.named("DBCP.accessToUnderlyingConnectionAllowed"))
                    .to(accessToUnderlyingConnectionAllowed);
            bindConstant().annotatedWith(Names.named("DBCP.defaultCatalog")).to(defaultCatalog);
            bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly);
            bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation"))
                    .to(defaultTransactionIsolation);
            bindConstant().annotatedWith(Names.named("DBCP.initialSize")).to(initialSize);
            bindConstant().annotatedWith(Names.named("DBCP.maxActive")).to(maxActive);
            bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle);
            bindConstant().annotatedWith(Names.named("DBCP.maxOpenPreparedStatements"))
                    .to(maxOpenPreparedStatements);
            bindConstant().annotatedWith(Names.named("DBCP.maxWait")).to(maxWait);
            bindConstant().annotatedWith(Names.named("DBCP.minIdle")).to(minIdle);
            bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun);
            bindConstant().annotatedWith(Names.named("DBCP.poolPreparedStatements")).to(poolPreparedStatements);
            bindConstant().annotatedWith(Names.named("DBCP.testOnBorrow")).to(testOnBorrow);
            bindConstant().annotatedWith(Names.named("DBCP.testOnReturn")).to(testOnReturn);
            bindConstant().annotatedWith(Names.named("DBCP.testWhileIdle")).to(testWhileIdle);
            bindConstant().annotatedWith(Names.named("DBCP.timeBetweenEvictionRunsMillis"))
                    .to(timeBetweenEvictionRunsMillis);
            bindConstant().annotatedWith(Names.named("DBCP.validationQuery")).to(validationQuery);
        }
    });
    BasicDataSourceProvider provider = injector.getInstance(BasicDataSourceProvider.class);

    BasicDataSource dataSource = (BasicDataSource) provider.get();

    assertEquals(driverClassLoader, dataSource.getDriverClassLoader());
    assertEquals(driver, dataSource.getDriverClassName());
    assertEquals(url, dataSource.getUrl());
    assertEquals(username, dataSource.getUsername());
    assertEquals(password, dataSource.getPassword());
    assertEquals(autoCommit, dataSource.getDefaultAutoCommit());
    // Cannot test driver properties.
    assertEquals(accessToUnderlyingConnectionAllowed, dataSource.isAccessToUnderlyingConnectionAllowed());
    assertEquals(defaultCatalog, dataSource.getDefaultCatalog());
    assertEquals(defaultReadOnly, dataSource.getDefaultReadOnly());
    assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation());
    assertEquals(initialSize, dataSource.getInitialSize());
    assertEquals(maxActive, dataSource.getMaxActive());
    assertEquals(maxIdle, dataSource.getMaxIdle());
    assertEquals(maxOpenPreparedStatements, dataSource.getMaxOpenPreparedStatements());
    assertEquals(maxWait, dataSource.getMaxWait());
    assertEquals(minIdle, dataSource.getMinIdle());
    assertEquals(numTestsPerEvictionRun, dataSource.getNumTestsPerEvictionRun());
    assertEquals(poolPreparedStatements, dataSource.isPoolPreparedStatements());
    assertEquals(testOnBorrow, dataSource.getTestOnBorrow());
    assertEquals(testOnReturn, dataSource.getTestOnReturn());
    assertEquals(testWhileIdle, dataSource.getTestWhileIdle());
    assertEquals(timeBetweenEvictionRunsMillis, dataSource.getTimeBetweenEvictionRunsMillis());
    assertEquals(validationQuery, dataSource.getValidationQuery());
}

From source file:org.orbisgis.geoserver.h2gis.datastore.H2DataStoreFactoryTest.java

@Test
public void testCreateDataStoreMVCC() throws Exception {
    Map clonedParams = new HashMap(params);
    clonedParams.put(H2GISDataStoreFactory.MVCC.key, true);
    JDBCDataStore ds = factory.createDataStore(clonedParams);
    assertNotNull(ds);/*from  w  w  w.j a  v a 2s.co m*/
    final DataSource source = ds.getDataSource();
    assertNotNull(source);
    final DataSource wrapped = source.unwrap(DataSource.class);
    assertNotNull(wrapped);
    if (wrapped instanceof BasicDataSource) {
        final BasicDataSource basicSource = (BasicDataSource) wrapped;
        final String url = basicSource.getUrl();
        assertTrue(url.contains("MVCC=true"));
    }
}

From source file:org.sonar.core.persistence.profiling.PersistenceProfilingTest.java

@Test
public void should_enable_profiling_when_profiling_is_full() throws Exception {
    final Logger sqlLogger = (Logger) LoggerFactory.getLogger("sql");
    ListAppender<ILoggingEvent> appender = new ListAppender<ILoggingEvent>();
    appender.setContext(new ContextBase());
    appender.start();/*from w ww .ja v a 2 s.  c o  m*/
    sqlLogger.addAppender(appender);

    BasicDataSource originDataSource = mock(BasicDataSource.class);

    Connection connection = mock(Connection.class);
    when(originDataSource.getConnection()).thenReturn(connection);

    String sql = "select 'polop' from dual;";
    String sqlWithParams = "insert into polop (col1, col2, col3, col4) values (?, ?, ?, ?, ?);";
    int param1 = 42;
    String param2 = "plouf";
    Date param3 = new Date(System.currentTimeMillis());
    Timestamp param4 = new Timestamp(System.currentTimeMillis());
    byte[] param5 = "blob".getBytes("UTF-8");

    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    when(connection.prepareStatement(sqlWithParams)).thenReturn(preparedStatement);
    when(preparedStatement.execute()).thenReturn(true);

    Statement statement = mock(Statement.class);
    when(connection.createStatement()).thenReturn(statement);
    when(statement.execute(sql)).thenReturn(true);

    Settings settings = new Settings();
    settings.setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.FULL.toString());

    BasicDataSource resultDataSource = PersistenceProfiling.addProfilingIfNeeded(originDataSource, settings);

    assertThat(resultDataSource).isInstanceOf(ProfilingDataSource.class);
    assertThat(resultDataSource.getUrl()).isNull();
    assertThat(resultDataSource.getConnection().getClientInfo()).isNull();
    PreparedStatement preparedStatementProxy = resultDataSource.getConnection().prepareStatement(sqlWithParams);
    preparedStatementProxy.setInt(1, param1);
    preparedStatementProxy.setString(2, param2);
    preparedStatementProxy.setDate(3, param3);
    preparedStatementProxy.setTimestamp(4, param4);
    preparedStatementProxy.setBlob(5, new ByteArrayInputStream(param5));
    assertThat(preparedStatementProxy.getConnection()).isNull();
    assertThat(preparedStatementProxy.execute()).isTrue();
    final Statement statementProxy = resultDataSource.getConnection().createStatement();
    assertThat(statementProxy.getConnection()).isNull();
    assertThat(statementProxy.execute(sql)).isTrue();

    assertThat(appender.list).hasSize(2);
    assertThat(appender.list.get(0).getLevel()).isEqualTo(Level.INFO);
    assertThat(appender.list.get(0).getFormattedMessage()).contains(sqlWithParams)
            .contains(" - parameters are: ").contains(Integer.toString(param1)).contains(param2);
    assertThat(appender.list.get(1).getLevel()).isEqualTo(Level.INFO);
    assertThat(appender.list.get(1).getFormattedMessage()).contains(sql);
}

From source file:org.springframework.boot.autoconfigure.jdbc.CommonsDataSourceConfigurationTests.java

@Test
public void testDataSourcePropertiesOverridden() throws Exception {
    this.context.register(CommonsDataSourceConfiguration.class);
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "url:jdbc:foo//bar/spam");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "testWhileIdle:true");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "testOnBorrow:true");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "testOnReturn:true");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "timeBetweenEvictionRunsMillis:10000");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "minEvictableIdleTimeMillis:12345");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "maxWait:1234");
    this.context.refresh();
    BasicDataSource ds = this.context.getBean(BasicDataSource.class);
    assertEquals("jdbc:foo//bar/spam", ds.getUrl());
    assertTrue(ds.getTestWhileIdle());// w ww .  jav  a 2s .  c o  m
    assertTrue(ds.getTestOnBorrow());
    assertTrue(ds.getTestOnReturn());
    assertEquals(10000, ds.getTimeBetweenEvictionRunsMillis());
    assertEquals(12345, ds.getMinEvictableIdleTimeMillis());
    assertEquals(1234, ds.getMaxWait());
}

From source file:org.springframework.boot.autoconfigure.jdbc.CommonsDbcpDataSourceConfigurationTests.java

@Test
public void testDataSourcePropertiesOverridden() throws Exception {
    this.context.register(CommonsDbcpDataSourceConfiguration.class);
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "url:jdbc:foo//bar/spam");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "testWhileIdle:true");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "testOnBorrow:true");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "testOnReturn:true");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "timeBetweenEvictionRunsMillis:10000");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "minEvictableIdleTimeMillis:12345");
    EnvironmentTestUtils.addEnvironment(this.context, PREFIX + "maxWait:1234");
    this.context.refresh();
    BasicDataSource ds = this.context.getBean(BasicDataSource.class);
    assertThat(ds.getUrl()).isEqualTo("jdbc:foo//bar/spam");
    assertThat(ds.getTestWhileIdle()).isTrue();
    assertThat(ds.getTestOnBorrow()).isTrue();
    assertThat(ds.getTestOnReturn()).isTrue();
    assertThat(ds.getTimeBetweenEvictionRunsMillis()).isEqualTo(10000);
    assertThat(ds.getMinEvictableIdleTimeMillis()).isEqualTo(12345);
    assertThat(ds.getMaxWait()).isEqualTo(1234);
}

From source file:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests.java

@Test
public void commonsDbcpIsFallback() throws Exception {
    BasicDataSource dataSource = testDataSourceFallback(BasicDataSource.class, "org.apache.tomcat",
            "com.zaxxer.hikari");
    assertEquals("jdbc:hsqldb:mem:testdb", dataSource.getUrl());
}

From source file:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests.java

@Test
public void commonsDbcp2IsFallback() throws Exception {
    org.apache.commons.dbcp2.BasicDataSource dataSource = testDataSourceFallback(
            org.apache.commons.dbcp2.BasicDataSource.class, "org.apache.tomcat", "com.zaxxer.hikari",
            "org.apache.commons.dbcp.");
    assertEquals("jdbc:hsqldb:mem:testdb", dataSource.getUrl());
}

From source file:org.unitils.database.core.DataSourceWrapperFactoryCreateTest.java

@Test
public void create() throws Exception {
    DataSourceWrapper result = dataSourceWrapperFactory.create(databaseConfiguration);

    assertSame(databaseConfiguration, result.getDatabaseConfiguration());
    DataSource dataSource = result.getDataSource(false);
    BasicDataSource basicDataSource = (BasicDataSource) dataSource;
    assertTrue(basicDataSource.isAccessToUnderlyingConnectionAllowed());
    assertEquals("driver", basicDataSource.getDriverClassName());
    assertEquals("user", basicDataSource.getUsername());
    assertEquals("pass", basicDataSource.getPassword());
    assertEquals("url", basicDataSource.getUrl());
}