title: Too many connections Hibernate and mysql
body: <p>I'm at a complete loss, I'm running a batch job using both hibernate and mysql and after a few hours I get an exception saying I'm using to many connections. I've read all the articles on SO, but none seem to relate to me. I'm using Tapestry-hibernate with a very simple configuration, <a href="http://tapestry.apache.org/using-tapestry-with-hibernate.html" rel="nofollow noreferrer">http://tapestry.apache.org/using-tapestry-with-hibernate.html</a>. No where's am I creating a new SessionFactory, once the application starts up, I just inject the hibernate Session into my class. </p>
  
  <p>This is my current connection view with mysql. <img src="https://i.stack.imgur.com/g61sy.png" alt="enter image description here"></p>
  
  <p>My batch job is threaded and everytime a new thread fires off, the threads_connected seems to increment. </p>
  
  <p>my cfg.xml file. </p>
  
  <pre><code>&lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
      &lt;property name="hibernate.connection.datasource"&gt;jdbc/company&lt;/property&gt;
      &lt;property name="hbm2ddl.auto"&gt;validate&lt;/property&gt;
      &lt;property name="hibernate.show_sql"&gt;false&lt;/property&gt;
  
      &lt;property name="hibernate.search.default.directory_provider"&gt;filesystem&lt;/property&gt;
      &lt;property name="hibernate.search.default.indexBase"&gt;/users/george/Documents/indexes &lt;/property&gt;
  
      &lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.cache.use_query_cache"&gt;false&lt;/property&gt;
      &lt;property name="hibernate.cache.region.factory_class"&gt;org.hibernate.cache.ehcache.EhCacheRegionFactory&lt;/property&gt;
  &lt;/session-factory&gt;
  </code></pre>
  
  <p></p>
  
  <p>Sample of basic session usage in class - "please note below code is not production code, just used to illustrate session usage. </p>
  
  <pre><code>private final Session session;
  
  public LineReaderParserImpl(Session session) {
      this.session = session;
  }
  
  public void parse() {
      exec.submit(new Runnable() {
          public void run() {
            for (int i = 0; i &lt; 10000; i++) {
              Object object = session.createCriteria()...
  
              session.save(object);
              session.getTransaction().commit();
  
              if (currentRow % 250 == 0 || currentRow == totalRows) {
                  try {
                      session.getTransaction().commit();
                  } catch (RuntimeException ex) {
                      try {
                          session.getTransaction().rollback();
                      } catch (RuntimeException rbe) {
                          throw ex;
                      } finally {
                          session.clear();
                          session.beginTransaction();
                      }
                  }
              }  
           }              
      }
  }
  </code></pre>