title: Configure Hibernate C3P0 Connection Pooling
body: <p>I'm stumbled upon a problem while developing a Web Application based on Struts2 with Spring and Hibernate.</p>
  
  <p>When I refresh the site a few times, for example 2-4 times, Hibernate is showing up an exception about <strong>Too many connections</strong>. I've tried to implement C3P0 Connection pool and have some problems with it</p>
  
  <hr>
  
  <p>The <strong>hibernate.cfg.xml</strong> configuration:</p>
  
  <pre><code>&lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/axelle&lt;/property&gt;
  &lt;property name="hibernate.connection.username"&gt;axelle&lt;/property&gt;
  &lt;property name="hibernate.connection.password"&gt;dbpassword&lt;/property&gt;
  &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;    
  
  &lt;property name="hibernate.c3p0.min_size"&gt;5&lt;/property&gt;
  &lt;property name="hibernate.c3p0.max_size"&gt;20&lt;/property&gt;
  &lt;property name="hibernate.c3p0.timeout"&gt;300&lt;/property&gt;
  &lt;property name="hibernate.c3p0.max_statements"&gt;50&lt;/property&gt;
  &lt;property name="hibernate.c3p0.idle_test_period"&gt;3000&lt;/property&gt;
  </code></pre>
  
  <p><strong>applicationContext.xml</strong></p>
  
  <pre><code>&lt;bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:jdbc.properties"/&gt;
  
  &lt;bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.url}"
        p:username="${jdbc.username}"
        p:password="${jdbc.password}"
        p:connectionProperties="${jdbc.connectionProperties}"/&gt;
  
  &lt;!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) --&gt;
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="configLocation"&gt;
          &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory"&gt;
          &lt;ref local="sessionFactory"/&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The log output is:</p>
  
  <pre><code>org.hibernate.exception.JDBCConnectionException: Cannot open connection
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
  </code></pre>
  
  <p>And this is how PROCESSLIST MySQL window looks: <a href="http://img844.imageshack.us/img844/3959/be69273cc2.png">http://img844.imageshack.us/img844/3959/be69273cc2.png</a></p>
  
  <hr>
  
  <p>I've set <strong>max_size</strong> of connections to 20, but it seems like it doesn't read the C3P0 configuration from file, cause from the screen we can see that number of connections is higher than 20, or maybe I'm doing something wrong, but where? I really need some help guys, I'll appreciate this, and thanks in advance.</p>