title: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections
body: <p>My simple web-app sometimes crashes when navigating around the site, the error message I've gotten from the logs are found below. I have absolutely no idea of what is causing this and would be very thankful for any tips that'll lead me in the right direction.</p>
  
  <pre><code>HTTP Status 500 - org.hibernate.exception.JDBCConnectionException: Cannot open connection
  </code></pre>
  
  <p>type Exception report</p>
  
  <pre><code>message org.hibernate.exception.JDBCConnectionException: Cannot open connection
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  org.apache.jasper.JasperException: org.hibernate.exception.JDBCConnectionException: Cannot open connection
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p>root cause</p>
  
  <pre><code>org.hibernate.exception.JDBCConnectionException: Cannot open connection
      org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)
      org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
      org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
      org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
      org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
      org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
      org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1573)
      org.hibernate.loader.Loader.doQuery(Loader.java:696)
      org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
      org.hibernate.loader.Loader.doList(Loader.java:2228)
      org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
      org.hibernate.loader.Loader.list(Loader.java:2120)
      org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
      org.hibernate.impl.SessionImpl.list(SessionImpl.java:1596)
      org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
      com.html.XmlSetting.getListOfhtml_connect(XmlSetting.java:55)
      com.html.XmlSetting.removeXml(XmlSetting.java:121)
      com.html.MarginSetting.removeXml(MarginSetting.java:291)
      org.apache.jsp.check_jsp._jspService(check_jsp.java:183)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p>root cause</p>
  
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections
      sun.reflect.GeneratedConstructorAccessor153.newInstance(Unknown Source)
      sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      java.lang.reflect.Constructor.newInstance(Constructor.java:532)
      com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
      com.mysql.jdbc.Util.getInstance(Util.java:381)
      com.mysql.jdbc.SQLError.createSQLException(SQLError.java:985)
      com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
      com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
      com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
      com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:894)
      com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3808)
      com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1256)
      com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2032)
      com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:729)
      com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:46)
      sun.reflect.GeneratedConstructorAccessor145.newInstance(Unknown Source)
      sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      java.lang.reflect.Constructor.newInstance(Constructor.java:532)
      com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
      com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
      com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
      java.sql.DriverManager.getConnection(DriverManager.java:620)
      java.sql.DriverManager.getConnection(DriverManager.java:169)
      org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
      org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
      org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
      org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
      org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1573)
      org.hibernate.loader.Loader.doQuery(Loader.java:696)
      org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
      org.hibernate.loader.Loader.doList(Loader.java:2228)
      org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
      org.hibernate.loader.Loader.list(Loader.java:2120)
      org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
      org.hibernate.impl.SessionImpl.list(SessionImpl.java:1596)
      org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
      com.html.XmlSetting.getListOfhtml_connect(XmlSetting.java:55)
      com.html.XmlSetting.removeXml(XmlSetting.java:121)
      com.html.MarginSetting.removeXml(MarginSetting.java:291)
      org.apache.jsp.check_jsp._jspService(check_jsp.java:183)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p>I m using  Apache Tomcat 7</p>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
  
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/htmlcleaner&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;root&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
  
          &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
          &lt;property name="hibernate.connection.zeroDateTimeBehavior"&gt;convertToNull&lt;/property&gt;
  
          &lt;property name="connection.pool_size"&gt;1000&lt;/property&gt;
  
         &lt;!--  &lt;property name="show_sql"&gt;true&lt;/property&gt; --&gt;
          &lt;mapping class="com.model.html_connect"/&gt;
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>