title: Connection issue from Spring Boot projects to PostgreSQL database while connecting
body: <p>I am working on a micro service oriented multi-tenant application using Spring MVC, Spring Boot and PostgreSQL. In my service domain, I have different 30 number of Spring Boot projects connecting with same database.</p>
  
  <p><strong>Issue</strong></p>
  
  <p>When I am starting more than 11 Spring Boot project parallel, I am not able to retrieve data from database. I am only getting blank JSON response. But If I am starting only less than 11 projects, then at that time, I am able to get the micro service result. Problem getting when I am starting from 12th service.</p>
  
  <p><strong>Troubleshooting and my Investigation</strong></p>
  
  <p>According to reading, I made little bit changes as follows,</p>
  
  <p>I set PostgreSQL config file, <code>max_connections = 100</code> and <code>shared_buffers = 128MB</code>. </p>
  
  <p>But that not solved my problem. And also added additional lines in my <code>application.property</code> for <code>hikari</code> connection pool like the following,</p>
  
  <pre><code>spring.datasource.hikari.minimumIdle=3
  spring.datasource.hikari.maximum-pool-size=3
  </code></pre>
  
  <p>Even this also not fixing my issue. Still I am only able to start maximum 12 Spring Boot projects at one time.</p>
  
  <p>On pom.xml added the following,</p>
  
  <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;exclusions&gt;
      &lt;exclusion&gt;
          &lt;groupId&gt;com.zaxxer&lt;/groupId&gt;
          &lt;artifactId&gt;HikariCP&lt;/artifactId&gt;
      &lt;/exclusion&gt;
    &lt;/exclusions&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
    &lt;artifactId&gt;tomcat-jdbc&lt;/artifactId&gt;
    &lt;version&gt;9.0.10&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>application.property updated with the following,</p>
  
  <pre><code>spring.datasource.tomcat.initial-size=15
  spring.datasource.tomcat.max-wait=20000
  spring.datasource.tomcat.max-active=20
  spring.datasource.tomcat.max-idle=20
  spring.datasource.tomcat.min-idle=5
  spring.datasource.tomcat.default-auto-commit=true
  </code></pre>
  
  <p><strong>Updates</strong></p>
  
  <p>As I mentioned above, in total I have 100 max connection. When I am starting first microservice and monitoring the DB connection by querying (below query), I am getting 10 connections.</p>
  
  <pre><code>select max_conn,used,res_for_super,max_conn-used-res_for_super res_for_normal 
  from 
   (select count(*) used from pg_stat_activity) t1,
   (select setting::int res_for_super from pg_settings where 
    name=$$superuser_reserved_connections$$) t2,
   (select setting::int max_conn from pg_settings where 
   name=$$max_connections$$) t3
  </code></pre>
  
  <p>Activity screenshot - When I started only 1 microservice I am getting like these,</p>
  
  <p><a href="https://i.stack.imgur.com/iHtY9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iHtY9.png" alt="enter image description here"></a></p>
  
  <p>Activity screenshot 2:</p>
  
  <p><a href="https://i.stack.imgur.com/AktvZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AktvZ.png" alt="enter image description here"></a></p>
  
  <p>So here, for each micro service starting, 10 Database connections are initiating at the back end database level. So if I am starting more than 30 microservice application, it will go to more than 300 connection.</p>
  
  <p><strong>Confusion</strong></p>
  
  <ol>
  <li>Is this because of any database default settings?</li>
  <li>Or is this because of Tomcat problem when connecting to DB engine?</li>
  </ol>
  
  <p>Even I tried to use connection pool tool <code>pgbouncer</code> in my database node. Then also I am getting the same problems.</p>
  
  <p>If this is due to database default settings or configuration, how can I override this?</p>