title: DBCP Pool does not work in Servlet but works in standalone app
body: <p>Pool doesn't in <strong>servlet</strong> but works in <strong>standalone</strong> appication.</p>
  
  <p><strong>Pool</strong> class is common in two cases:</p>
  
  <pre><code>public class Pool {
  private static DataSource ds;
  
  static {
      DriverAdapterCPDS cpds = new DriverAdapterCPDS();
      try {
          cpds.setDriver("com.mysql.jdbc.Driver");
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
      cpds.setUrl("jdbc:mysql://localhost:3306/collage");
      cpds.setUser("root");
      cpds.setPassword("r00t");
  
      SharedPoolDataSource tds = new SharedPoolDataSource();
      tds.setConnectionPoolDataSource(cpds);
      tds.setMaxTotal(10);
      /*tds.setMaxWait(50);
      tds.setMaxTotal();*/
  
      tds.setMaxTotal(50);
  
      ds = tds;
  }
  
  public static Connection getConnection() throws SQLException {
      return ds.getConnection();
  }
  </code></pre>
  
  <p>for <strong>standalone</strong> class it works as following </p>
  
  <pre><code>public class MainClass {
  public static void main(String[] args) {
      Connection connection = null;
      Statement statement = null;
      ResultSet resultSet = null;
  
      try {
          connection = Pool.getConnection();
          // Do work with connection
          statement = connection.createStatement();
          String selectEmployeesSQL = "select * from students";
          resultSet = statement.executeQuery(selectEmployeesSQL);
  
          while (resultSet.next()) {
              printTestTable(resultSet);
          }
      } catch (Exception e) {
          e.printStackTrace();
      } finnaly {
          ...
      }
  }
  </code></pre>
  
  <p>And from part from <strong>servlet</strong>  is following:</p>
  
  <pre><code>@WebServlet("/Demo.do")
  public class DemoController extends HttpServlet {
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          ResultSet rsObj = null;
          Connection connObj = null;
          PreparedStatement pstmtObj = null;
  
          try {
              connObj = Pool.getConnection();
          ...
  </code></pre>
  
  <p>And exception is here" </p>
  
  <pre><code>    HTTP Status 500 – Internal Server Error
  
  Type Exception Report
  
  Message Servlet execution threw an exception
  
  Description The server encountered an unexpected condition that prevented it from fulfilling the request.
  
  Exception
  
  javax.servlet.ServletException: Servlet execution threw an exception
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  
  Root Cause
  
  java.lang.NoClassDefFoundError: Could not initialize class am.ak.dao.jdbc.connections.Pool
      test.DemoController.doGet(DemoController.java:27)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  
  Note The full stack trace of the root cause is available in the server logs.  
  </code></pre>
  
  <p>Thanks.</p>
  
  <p><strong>ADDED:</strong></p>
  
  <p>Artifacts in Project settings:<a href="https://i.stack.imgur.com/R8sjc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R8sjc.png" alt="enter image description here"></a></p>
  
  <p><strong>RESOLVED:</strong></p>
  
  <p>So this two lib were missed )</p>
  
  <p><a href="https://i.stack.imgur.com/9Iepu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9Iepu.png" alt="enter image description here"></a></p>
  
  <p><strong>Interesting:</strong>
  In POM I added only one  dependency bot it shoes two :</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
      &lt;artifactId&gt;commons-dbcp2&lt;/artifactId&gt;
      &lt;version&gt;2.5.0&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>