title: Share JDBC datasource connection among all servlets
body: <p>I am new to java. I have recently learned JDBC connection pool in tomcat.
  To make code reuse I want to share the connection among all servlets without any conflict.</p>
  
  <p>Here My code snippet:</p>
  
  <pre><code>  public class GetConnection{
  
      private DataSource ds;
      public Connection getConnection(){
        try {
           InitialContext initialContext = new InitialContext();
           Context context = (Context) initialContext.lookup("java:comp/env");
           ds = (DataSource) context.lookup("connpool");
        }
        catch (NamingException e) {
  
        }
       return ds.getConnection();
      }
  
   }  
  </code></pre>
  
  <p>I am calling the getConnection method from different servlets like</p>
  
  <pre><code>            //In servlet1 doGet Method
               Connection conn = GetConnection.getConnection();
  </code></pre>
  
  <p>Is this right way to do. Or I will get any problem due to concurrent threads.</p>