title: tomcat datasource not checking for valid connection during spring-boot start up?
body: <p>I have two spring-boot application.  Application A is using MySQL database configured using HikariDataSource datasource. Application B is using SQL Server database configured using tomcat datasource. </p>
  
  <p>when I supply wrong username for database parameter, Application A does not start up and errors with following exception. However Application B start up fine, but when I make a service call that uses datasource, error is thrown out. why is datasource connection check ignored in the latter?</p>
  
  <p>Note: I have another spring application (Application C), which connects to SQL SERVER using tomcat datasource and that errors out during container start-up if wrong credentials are supplied. It is not a spring-boot application though</p>
  
  <p>Here is my Application A datasource configuration</p>
  
  <pre><code>   @Bean
     public DataSource dataSource() {
              HikariDataSource dataSource = new HikariDataSource();
  
              dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driver"));
              dataSource.setJdbcUrl(environment.getRequiredProperty("jdbc.url"));
              dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
              dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
  
              return dataSource;
          }
  </code></pre>
  
  <p>Application B datasource configuration</p>
  
  <pre><code>import org.apache.tomcat.jdbc.pool.DataSource;
  
      @Bean
      public DataSource datasource() {
          DataSource ds = new DataSource();
          ds.setDriverClassName(hostdb_driver);
          ds.setUrl(hostdb_url);
          ds.setUsername(hostdb_user);
          ds.setPassword(pass);
          ds.setMaxActive(hostdb_maxActive);
          ds.setMaxIdle(hostdb_maxIdle);
          ds.setInitialSize(hostdb_initialSize);
          ds.setMinIdle(hostdb_minIdle);
          ds.setTestOnBorrow(hostdb_testOnBorrow);
          ds.setTestWhileIdle(hostdb_testWhileIdle);
          ds.setValidationQuery(hostdb_validationQuery);
          ds.setRemoveAbandoned(hostdb_removeAbandoned);
          ds.setRemoveAbandonedTimeout(hostdb_removeAbandonedTimeout);
          ds.setLogAbandoned(hostdb_logAbandoned);
          ds.setValidationInterval(hostdb_validationInterval);
          return ds;
      }
  </code></pre>
  
  <p>Exception message:</p>
  
  <pre><code>dg.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      ... 23 common frames omitted
  Caused by: com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
      at com.zaxxer.hikari.pool.BaseHikariPool.initializeConnections(BaseHikariPool.java:544) ~[HikariCP-2.3.8.jar:na]
      at com.zaxxer.hikari.pool.BaseHikariPool.&lt;init&gt;(BaseHikariPool.java:171) ~[HikariCP-2.3.8.jar:na]
      at com.zaxxer.hikari.pool.HikariPool.&lt;init&gt;(HikariPool.java:60) ~[HikariCP-2.3.8.jar:na]
      at com.zaxxer.hikari.pool.HikariPool.&lt;init&gt;(HikariPool.java:48) ~[HikariCP-2.3.8.jar:na]
      at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:104) ~[HikariCP-2.3.8.jar:na]
      at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:338) ~[spring-jdbc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:139) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate.getDefaultDdlAuto(JpaProperties.java:198) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate.getOrDeduceDdlAuto(JpaProperties.java:186) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate.getAdditionalProperties(JpaProperties.java:165) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate.access$000(JpaProperties.java:126) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.getHibernateProperties(JpaProperties.java:123) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.getVendorProperties(HibernateJpaAutoConfiguration.java:99) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:115) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$425788ff.CGLIB$entityManagerFactory$5(&lt;generated&gt;) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$425788ff$$FastClassBySpringCGLIB$$cee5d89e.invoke(&lt;generated&gt;) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$425788ff.entityManagerFactory(&lt;generated&gt;) ~[spring-boot-autoconfigure-1.3.5.RELEASE.jar:1.3.5.RELEASE]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_102]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_102]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_102]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
      ... 24 common frames omitted
  Caused by: java.sql.SQLException: Access denied for user 'foobar'@'localhost' (using password: YES)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:870) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2239) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:794) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_102]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_102]
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_102]
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_102]
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:389) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325) ~[mysql-connector-java-5.1.35.jar:5.1.35]
      at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:92) ~[HikariCP-2.3.8.jar:na]
      at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:98) ~[HikariCP-2.3.8.jar:na]
      at com.zaxxer.hikari.pool.BaseHikariPool.addConnection(BaseHikariPool.java:438) ~[HikariCP-2.3.8.jar:na]
      at com.zaxxer.hikari.pool.BaseHikariPool.initializeConnections(BaseHikariPool.java:542) ~[HikariCP-2.3.8.jar:na]
      ... 49 common frames omitted
  </code></pre>