title: How the JDBC Connection Pooling helps here
body: <p>I have a web application. I have implemented REST Web services using Jersey into it. So my controller is Jersey ServletContainer servlet, and that is packaged inside the jar. 
  Now let suppose i have n number of customers connecting to my app and fetching data from DB through API endpoints. 
  I want to have a global connection pool from where my every customer can get a connection and i would be able to reduce my connection creation overhead. 
  So, i thought of putting it into the <strong>init()</strong> or <strong>contextInitialized()</strong> method of controller servlet, but i can't do that as the jersey controller is already packaged inside the jersey jar as a class file. So i have implemeneted it by code by creating a static block inside a GenericConnection class that takes care of creating the context and data source so that i don't create the context also everytime.</p>
  
  <pre><code>public class GenericConnection {
      static DataSource dataSource;
      Connection con;
  
      static {
          try{
              System.out.println("1 and thats all");
          Context initContext = new InitialContext();
          Context envContext = (Context)initContext.lookup("java:/comp/env");
          dataSource = (DataSource)envContext.lookup("jdbc/moodoff_notificationRead_DB");
          }
          catch(Exception ee){
              ee.printStackTrace();
          }
      }
      // Get a connection from the connection pool
      public Connection getNewConnection(){
          try {
              con = dataSource.getConnection();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return con;
      }
  }
  </code></pre>
  
  <p>But i am not sure of the fact that whether for every user request it would create a global pool and can share connection from there.
  I intentionally created a static block and put everything inside that so that it gets initialized only once.
  Will it really help me achieve what i want to? And if there is any other tuning that i can do into it.
  Thanks in advance!!</p>