title: How to setup multiple connection pools when multiple datasources are used in Spring Boot?
body: <p>I have a Spring Boot application that connects to two separate databases. All works fine (I followed the steps in <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-two-datasources" rel="noreferrer">the docs</a> and <a href="https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7" rel="noreferrer">a tutorial</a>), although in order to customize the Tomcat JDBC connection pool settings, <a href="https://stackoverflow.com/a/42712947/2261315">I had to manually configure it</a> (because by defining multiple data sources, the Boot auto-configuration is ignored, and Spring Boot does not read the tomcat-specific properties anymore from application.properties).</p>
  
  <p>When I use a debugger during the configuration of the two DataSources, I see that both <a href="https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/tomcat/jdbc/pool/DataSource.html" rel="noreferrer">org.apache.tomcat.jdbc.pool.DataSource</a> instances have the same connection pool in the DataSource.PoolProperties["name"] entry.
  See below screenshots in the debugger, each dataSource() method is configured in a separate configuration class. Notice that the same Connection Pool is defined.
  <a href="https://i.stack.imgur.com/Tz8kf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Tz8kf.png" alt="Primary DataSource CP configuration"></a>
  <a href="https://i.stack.imgur.com/Uqxjl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Uqxjl.png" alt="Secondary DataSource CP configuration"></a></p>
  
  <p>However, from what I see using jConsole + tomcat JMX, there is only one connection pool, which has the primary database details configured (URL, credentials, see below).
  <a href="https://i.stack.imgur.com/a6vz8.png" rel="noreferrer"><img src="https://i.stack.imgur.com/a6vz8.png" alt="jConsole inspecting the Tomcat JDBC JMX info about the Connection Pool"></a></p>
  
  <p>Because of the multiple layers of abstraction inside Spring, it is difficult for me to debug this. I have the <a href="https://marketplace.eclipse.org/content/eclipse-class-decompiler" rel="noreferrer">Eclipse Class Decompiler plugin</a>, which I normally use to see the Spring logic, but in this case, the initialization code for the data sources happens when the beans are registered, not when they are actually used by Spring Boot to set the data sources up.</p>
  
  <p>Bottom line, can you help me understand:</p>
  
  <ol>
  <li>why there is only one connection pool</li>
  <li>how can I use two connection pools, one for each data source</li>
  <li>where in the Spring code to look at for more details of how this works</li>
  </ol>
  
  <p>For the 2nd question, there is a somewhat <a href="https://stackoverflow.com/questions/37163160/spring-boot-connection-pool-autoconfiguration-for-more-than-one-datasource">related question</a>, but with no answer. There is <a href="https://stackoverflow.com/questions/43529778/multiple-db-connection-pools-in-spring-boot-app">another question</a> which is a false positive, and <a href="https://stackoverflow.com/questions/29330592/spring-initialize-multiple-connection-pools-at-startup">another one</a> which is related to Spring, not Spring Boot, so please don't report this as dupe.</p>