title: Hibernate, Spring and c3p0 connection pooling cause JBoss to opens too many connections to database
body: <p>I have a strange problem with my application, running on JBoss 6.1, which uses Hibernate and Spring. I'm also using c3p0 to have connection pooling.</p>
  
  <p>I have a datasource defined under JBoss. The datasource is a C3P0PooledDataSource. Please note that maxpoolsize is 20 and initial pool size is also 20.</p>
  
  <pre><code>&lt;mbean code="com.mchange.v2.c3p0.jboss.C3P0PooledDataSource"
        name="jboss.jca:service=DataSourceBinding,name=db/MED_POOLED"&gt;
  
    &lt;attribute name="JndiName"&gt;java:db/MED_POOLED&lt;/attribute&gt;
    &lt;attribute name="JdbcUrl"&gt;jdbc:oracle:thin:@dbhost:1521:SID&lt;/attribute&gt;
    &lt;attribute name="DriverClass"&gt;oracle.jdbc.driver.OracleDriver&lt;/attribute&gt;
    &lt;attribute name="User"&gt;xxx&lt;/attribute&gt;
    &lt;attribute name="Password"&gt;xxx&lt;/attribute&gt;
    &lt;attribute name="AutomaticTestTable"&gt;POOL_TEST&lt;/attribute&gt;          
    &lt;attribute name="CheckoutTimeout"&gt;30000&lt;/attribute&gt;      
    &lt;attribute name="Description"&gt;Pooled Datasource for med&lt;/attribute&gt;
    &lt;attribute name="IdleConnectionTestPeriod"&gt;180&lt;/attribute&gt;     
    &lt;attribute name="InitialPoolSize"&gt;20&lt;/attribute&gt;  
    &lt;attribute name="MaxPoolSize"&gt;20&lt;/attribute&gt;   
    &lt;attribute name="MinPoolSize"&gt;20&lt;/attribute&gt;    
    &lt;attribute name="TestConnectionOnCheckin"&gt;true&lt;/attribute&gt;       
    &lt;attribute name="MaxIdleTime"&gt;3600&lt;/attribute&gt;
    &lt;depends&gt;jboss:service=Naming&lt;/depends&gt;
  &lt;/mbean&gt;
  </code></pre>
  
  <p>In my spring configuration file, datasource is retrieved using the jndi name:</p>
  
  <pre><code>&lt;bean id="defaultDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiName"&gt;&lt;value&gt;db/MED_POOLED&lt;/value&gt;&lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>and then used to build a LocalSessionFactoryBean</p>
  
  <pre><code>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="defaultDataSource"/&gt;
      &lt;property name="namingStrategy" ref="namingStrategy"/&gt;
      &lt;property name="configLocations"&gt;
          &lt;list&gt;
          &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
          &lt;value&gt;classpath:jbpm.hibernate.cfg.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="mappingDirectoryLocations"&gt;
          &lt;list&gt;&lt;value&gt;WEB-INF/classes&lt;/value&gt;&lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>In hibernate.cfg.xml no datasource is defined, so the dataSource property of org.springframework.orm.hibernate3.LocalSessionFactoryBean should be used.</p>
  
  <p>When I start my application server, something strange happens (al least, strange to me):</p>
  
  <ul>
  <li><p>JBoss starts, I see in the log this line</p>
  
  <p>[com.mchange.v2.c3p0.jboss.C3P0PooledDataSource] Bound C3P0 PooledDataSource to name'java:db/MED_POOLED'. Starting... </p></li>
  </ul>
  
  <p>and if I check on database, 20 sessions are present.</p>
  
  <ul>
  <li>When the LocalSessionFactoryBean is created, suddendly 40 connections are present on Db, but in my datasource configuration I specified to create at maximum 20 connections.</li>
  </ul>
  
  <p>I think that, in some way, the datasource is reinitialized when the LocalSessionFactoryBean is created, but I don't understand why, and when JBoss is started I have exactly double the connections I want.</p>
  
  <p>Do you know what's happening? Is there something wrong with my configuration?</p>
  
  <p>I'm using JBoss 6.1, the latest available version of c3p0 (0.9.5), Spring 3.0.3.RELEASE and Hibernate 3.5.3-Final</p>