title: A ResourcePool could not acquire a resource from its primary factory or source
body: <p>I'm trying to connect to a database in Java, using jdbcTemplate and I'm gettin the error below. I have Googled for a long time and all solutions I found didn't solve my problem. I tried several different DBs (both SQLServer and MySQL) and none worked.</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/promotion-handler-admin] threw exception [Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!] with root cause
  com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
      at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
      at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
      at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
      at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
      at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
      at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:202)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
      ...
  </code></pre>
  
  <p>This is my properties file:</p>
  
  <pre><code>app.driverClassName=net.sourceforge.jtds.jdbc.Driver
  app.url=jdbc:sqlserver://myUrl:port;databaseName=my_database
  app.username=myUsername
  app.password=myPassword
  </code></pre>
  
  <p>webapp/WEB-INF/applicationContext-database.xml:</p>
  
  <pre><code>&lt;beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close"&gt;
      &lt;beans:property name="driverClass" value="${app.driverClassName}" /&gt;
      &lt;beans:property name="jdbcUrl"
          value="${app.url}" /&gt;
      &lt;beans:property name="user" value="${app.username}" /&gt;
      &lt;beans:property name="password" value="${app.password}" /&gt;
      &lt;beans:property name="acquireIncrement" value="5" /&gt;
      &lt;beans:property name="idleConnectionTestPeriod" value="600" /&gt;
      &lt;beans:property name="maxPoolSize" value="10" /&gt;
      &lt;beans:property name="maxStatements" value="5" /&gt;
      &lt;beans:property name="minPoolSize" value="3" /&gt;
      &lt;beans:property name="preferredTestQuery" value="select 1 from DUAL" /&gt;
  &lt;/beans:bean&gt;
  
  &lt;!-- TRANSACTION_MANAGERS --&gt;
  &lt;!-- See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html --&gt;
  
  &lt;!-- Default --&gt;
  &lt;beans:bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
      &lt;beans:property name="dataSource" ref="dataSource" /&gt;
  &lt;/beans:bean&gt;
  </code></pre>
  
  <p>DAO class:</p>
  
  <pre><code>@Repository
  public class CampaignDAO {
      private JdbcTemplate jdbcTemplate;
  
      @Resource(name = "dataSource")
      public void setDataSource(DataSource dataSource) {
          this.jdbcTemplate = new JdbcTemplate(dataSource);
      }
  
  public List&lt;Campaign&gt; getCampaignList() {
      Long start = System.currentTimeMillis();
  
      List&lt;Campaign&gt; queryList;
  
      try {
          queryList = jdbcTemplate.query("SELECT * FROM campaign", new RowMapper&lt;Campaign&gt;() {
              public Campaign mapRow(ResultSet rs, int line) throws SQLException {
                  Campaign campaign = new Campaign();
                  campaign.setId(rs.getLong("id"));
                  campaign.setExtraInfo(rs.getString("extra_info"));
                  campaign.setBeginTime(rs.getDate("begin_time"));
                  campaign.setEndTime(rs.getDate("end_time"));
  
                  return campaign;
  
              }
          });
      } finally {
           ...
      }
      return queryList;
  }
  </code></pre>