title: Can Spring Boot JPA and JDBC share same DataSource/connections?
body: <p>Spring Boot here. I currently use JPA/Hibernate along with <code>CrudRepository</code> interfaces for all my JPA entities and its been working great for me so far. I have a need to execute a complex query that requires a <code>UNION</code> and from what I can tell, Hibernate doesn't support unions (or at least does not support them easily). I've verified by trying about a <em>dozen</em> different JPQL queries in my <code>CrudRepository</code> and all of them fail because Hibernate (the JPA provider) doesn't like the <code>UNION</code>.</p>
  
  <p>So, I'm totally fine with just using a raw JDBC query in this one case and doing my own quasi-OR Mapping from the resultant <code>ResultSet</code> back into instances of my entities.</p>
  
  <p>What I'm concerned about has to do with managing the JDBC connections. I configure my JPA/Hibernate connection configurations in the application config (YAML) file:</p>
  
  <pre><code>spring:
    cache:
      type: none
    datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/myapp_db?useSSL=false
      username: root
      testWhileIdle: true
      validationQuery: SELECT 1
    jpa:
      show-sql: false
      hibernate:
        ddl-auto: none
        naming:
          physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
          implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
      properties:
        hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        hibernate.cache.use_second_level_cache: false
        hibernate.cache.use_query_cache: false
        hibernate.generate_statistics: false
        hibernate.hbm2ddl.auto: validate
  </code></pre>
  
  <ul>
  <li>Will I need two <code>DataSource</code>s, one for JPA/Hibernate and another for my JDBC (both are connecting to the exact same DB after all)</li>
  <li>Will I have performance or network/socket issues by maintaining these two <code>DataSource</code> instances?
  Anything special I need to do</li>
  <li>Currently I don't <em>believe</em> my JPA/Hibernate configs make use of connection pooling, but ideally I'd like to use <a href="https://github.com/brettwooldridge/HikariCP" rel="nofollow noreferrer">HikariCP</a> for the pooling for both JPA/Hibernate as well as my raw JDBC queries</li>
  <li>Is there a way to make both my <code>CrudRepository</code> instances (JPA/Hibernate) and my raw JDBC connections use the same exact <code>DataSource</code>/config? If so what would this config/code look like?</li>
  </ul>
  
  <p>I know there's several questions in here, but I believe that the right battle-weary JDBC/JPA veteran will be able to address all of them with a quick snippet of config and/or code! Thanks in advance!</p>