@Test
 public void testDataSourceUrlHasEmbeddedDefault() throws Exception {
   this.context.register(
       DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
   this.context.refresh();
   org.apache.tomcat.jdbc.pool.DataSource dataSource =
       this.context.getBean(org.apache.tomcat.jdbc.pool.DataSource.class);
   assertNotNull(dataSource.getUrl());
   assertNotNull(dataSource.getDriverClassName());
 }
 @Test
 public void testEmbeddedTypeDefaultsUsername() throws Exception {
   EnvironmentTestUtils.addEnvironment(
       this.context,
       "spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
       "spring.datasource.url:jdbc:hsqldb:mem:testdb");
   this.context.register(
       DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
   this.context.refresh();
   DataSource bean = this.context.getBean(DataSource.class);
   assertNotNull(bean);
   org.apache.tomcat.jdbc.pool.DataSource pool = (org.apache.tomcat.jdbc.pool.DataSource) bean;
   assertEquals("org.hsqldb.jdbcDriver", pool.getDriverClassName());
   assertEquals("sa", pool.getUsername());
 }
 @Test
 public void testExplicitDriverClassClearsUserName() throws Exception {
   EnvironmentTestUtils.addEnvironment(
       this.context,
       "spring.datasource.driverClassName:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests$DatabaseDriver",
       "spring.datasource.url:jdbc:foo://localhost");
   this.context.register(
       DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
   this.context.refresh();
   DataSource bean = this.context.getBean(DataSource.class);
   assertNotNull(bean);
   org.apache.tomcat.jdbc.pool.DataSource pool = (org.apache.tomcat.jdbc.pool.DataSource) bean;
   assertEquals(
       "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests$DatabaseDriver",
       pool.getDriverClassName());
   assertNull(pool.getUsername());
 }
Пример #4
0
  private static void pullTomcatJdbcDataSourceProperties(String name, DataSource dataSource) {
    // si tomcat-jdbc, alors on récupère des infos
    final org.apache.tomcat.jdbc.pool.DataSource jdbcDataSource =
        (org.apache.tomcat.jdbc.pool.DataSource) dataSource;
    final BasicDataSourcesProperties properties = TOMCAT_JDBC_DATASOURCES_PROPERTIES;
    // basicDataSource.getNumActive() est en théorie égale à USED_CONNECTION_COUNT à un instant t,
    // numIdle + numActive est le nombre de connexions ouvertes dans la bdd pour ce serveur à un
    // instant t

    // les propriétés généralement importantes en premier (se méfier aussi de testOnBorrow)
    properties.put(name, MAX_ACTIVE_PROPERTY_NAME, jdbcDataSource.getMaxActive());

    properties.put(name, "defaultCatalog", jdbcDataSource.getDefaultCatalog());
    properties.put(name, "defaultAutoCommit", jdbcDataSource.getDefaultAutoCommit());
    properties.put(name, "defaultReadOnly", jdbcDataSource.getDefaultReadOnly());
    properties.put(
        name, "defaultTransactionIsolation", jdbcDataSource.getDefaultTransactionIsolation());
    properties.put(name, "driverClassName", jdbcDataSource.getDriverClassName());
    properties.put(name, "connectionProperties", jdbcDataSource.getConnectionProperties());
    properties.put(name, "initSQL", jdbcDataSource.getInitSQL());
    properties.put(name, "initialSize", jdbcDataSource.getInitialSize());
    properties.put(name, "maxIdle", jdbcDataSource.getMaxIdle());
    properties.put(name, "maxWait", jdbcDataSource.getMaxWait());
    properties.put(name, "maxAge", jdbcDataSource.getMaxAge());
    properties.put(name, "faireQueue", jdbcDataSource.isFairQueue());
    properties.put(name, "jmxEnabled", jdbcDataSource.isJmxEnabled());
    properties.put(
        name, "minEvictableIdleTimeMillis", jdbcDataSource.getMinEvictableIdleTimeMillis());
    properties.put(name, "minIdle", jdbcDataSource.getMinIdle());
    properties.put(name, "numTestsPerEvictionRun", jdbcDataSource.getNumTestsPerEvictionRun());
    properties.put(name, "testOnBorrow", jdbcDataSource.isTestOnBorrow());
    properties.put(name, "testOnConnect", jdbcDataSource.isTestOnConnect());
    properties.put(name, "testOnReturn", jdbcDataSource.isTestOnReturn());
    properties.put(name, "testWhileIdle", jdbcDataSource.isTestWhileIdle());
    properties.put(
        name, "timeBetweenEvictionRunsMillis", jdbcDataSource.getTimeBetweenEvictionRunsMillis());
    properties.put(name, "validationInterval", jdbcDataSource.getValidationInterval());
    properties.put(name, "validationQuery", jdbcDataSource.getValidationQuery());
    properties.put(name, "validatorClassName", jdbcDataSource.getValidatorClassName());
  }