title: Spring boot - Configure second datasource along with gloud gcp postgresql primary datasource
body: <p>Setting up our Spring Boot project we have a database hosted on Google Cloud Gcp. We use Spring Data JPA to manipulate our database objects and it works great.</p>
  
  <blockquote>
    <p>application.yaml</p>
  </blockquote>
  
  <pre><code>spring:
    jpa:
  database-platform: org.hibernate.dialect.PostgreSQL95Dialect
  properties:
    hibernate:
      default_schema: {schema}
      dialect : org.hibernate.dialect.PostgreSQL95Dialect
  datasource:
    username: {username}
    password: {pwd}
  cloud:
    gcp:
      sql:
        database-name: {dbname}
        instance-connection-name: {connection-name}
  </code></pre>
  
  <blockquote>
    <p>pom.xml</p>
  </blockquote>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
      &lt;artifactId&gt;spring-cloud-gcp-starter-sql-postgresql&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Using this Spring boot is able to autoconfigure the database connection and we are using Spring Data JPA to manipulate database objects.</p>
  
  <p>This datasource is only used by our project, but our company has an Oracle database used by our ERP that I need to use. I figured I would use JdbcTemplate.</p>
  
  <p>So I setup a Datasource and a JdbcTemplate linked to it :</p>
  
  <blockquote>
    <p>DatasourceConfiguration.java</p>
  </blockquote>
  
  <pre><code>@Bean(name = "dataSourceGenerix")
  public DataSource dataSourceGenerix() {
      final DriverManagerDataSource dataSource = new DriverManagerDataSource();
  
      dataSource.setDriverClassName(generixDatasourceDriver);
      dataSource.setUrl(generixDatasourceUrl);
      dataSource.setUsername(generixDatasourceUsername);
      dataSource.setPassword(generixDatasourcePassword);
      return dataSource;
  }
  
  @Bean
  public NamedParameterJdbcTemplate jdbcTemplateGenerix(@Qualifier("dataSourceGenerix") DataSource dataSourceGenerix) {
      NamedParameterJdbcTemplate jdbcTemplate = null;
  
      try(Connection conn = DataSourceUtils.getConnection(dataSourceGenerix)) {
          jdbcTemplate = new NamedParameterJdbcTemplate(dataSourceGenerix);
      } catch (SQLException | CannotGetJdbcConnectionException e) {
          log.error("{} {} : {}", Constantes.NO_DB_CONNECTION_GENERIX, generixDatasourceUrl, e.getMessage());
      }
      return jdbcTemplate;
  }
  </code></pre>
  
  <p>With that setup, I can perform requests on this datasource.</p>
  
  <p>But since I configures a Datasource explicitely, JPA now executes requests on that one and does not Autoconfigure my CLoud SQL datasource anymore.</p>
  
  <p>I tried setting up a @Primary Datasource and explicitly configure parameters :</p>
  
  <pre><code>@Bean
  @Primary
  DataSource dataSource() {
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
  
      dataSource.setUrl("jdbc:postgresql://google/{dbname}?cloudSqlInstance={instancename}&amp;socketFactory=com.google.cloud.sql.postgres.SocketFactory");
      dataSource.setUsername({username});
      dataSource.setPassword({pwd});
  
      return dataSource;
  }
  </code></pre>
  
  <p>Here's the logs at startup :</p>
  
  <pre><code>c.g.cloud.sql.core.SslSocketFactory : Obtaining ephemeral certificate for Cloud SQL instance [{instancename}].
  o.s.b.a.orm.jpa.DatabaseLookup      : Unable to determine jdbc url from datasource
  org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta-data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException:
  Caused by: java.lang.RuntimeException: Unable to retrieve information about Cloud SQL instance
  Caused by: java.net.SocketTimeoutException: connect timed out
  </code></pre>
  
  <p>So it seems like it cannot get to connect to Cloud SQL instance. I'm using the exact same parameters that I use in the application.yaml.</p>
  
  <p>So how can I keep the autoconfiguration to Cloud GCP for my JPA needs, and add a second datasource for jdbc template ?</p>