一个ResourcePool无法从它的主要工厂或源获取资源(A ResourcePool could

2019-09-02 23:05发布

我试图连接到数据库在Java中使用的JdbcTemplate,我想趁自己下面的错误。 我用Google搜索了很久,所有的解决方案,我发现并没有解决我的问题。 我尝试了几种不同的数据块(包括SQLServer的和MySQL),没有工作。

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)
    ...

这是我的属性文件:

app.driverClassName=net.sourceforge.jtds.jdbc.Driver
app.url=jdbc:sqlserver://myUrl:port;databaseName=my_database
app.username=myUsername
app.password=myPassword

web应用/ WEB-INF /的applicationContext-database.xml:

<beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <beans:property name="driverClass" value="${app.driverClassName}" />
    <beans:property name="jdbcUrl"
        value="${app.url}" />
    <beans:property name="user" value="${app.username}" />
    <beans:property name="password" value="${app.password}" />
    <beans:property name="acquireIncrement" value="5" />
    <beans:property name="idleConnectionTestPeriod" value="600" />
    <beans:property name="maxPoolSize" value="10" />
    <beans:property name="maxStatements" value="5" />
    <beans:property name="minPoolSize" value="3" />
    <beans:property name="preferredTestQuery" value="select 1 from DUAL" />
</beans:bean>

<!-- TRANSACTION_MANAGERS -->
<!-- See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html -->

<!-- Default -->
<beans:bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <beans:property name="dataSource" ref="dataSource" />
</beans:bean>

DAO类:

@Repository
public class CampaignDAO {
    private JdbcTemplate jdbcTemplate;

    @Resource(name = "dataSource")
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

public List<Campaign> getCampaignList() {
    Long start = System.currentTimeMillis();

    List<Campaign> queryList;

    try {
        queryList = jdbcTemplate.query("SELECT * FROM campaign", new RowMapper<Campaign>() {
            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;
}

Answer 1:

对于任何人发现这个问题在未来。 我在做什么错了,我是用JTDS司机和我忘了补充一点,在URL中。 所以在我的属性文件,是我应该做的是:

app.url=jdbc:jtds:sqlserver://myUrl:port;databaseName=my_database


Answer 2:

对于任何人发现这个问题在未来。

这也可以通过一个丢失的数据库驱动程序引起的。

在我来说,我用的是maven-shade-pluginminimizeJar选项集。 本-当然-被扔掉jtds驱动程序,因为它不直接随地引用。

这可以被确定如下:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.6</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <minimizeJar>true</minimizeJar>
          <filters>
            <filter>
              <!-- Make sure jtds is included. -->
               <artifact>net.sourceforge.jtds:jtds</artifact>
               <includes>
                   <include>**</include>
               </includes>
            </filter>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
                <exclude>META-INF/*.sf</exclude>
                <exclude>META-INF/*.dsa</exclude>
                <exclude>META-INF/*.rsa</exclude>
              </excludes>
            </filter>
          </filters>
        </configuration>
      </execution>
    </executions>
  </plugin>


Answer 3:

还可以显示该消息,如果像我一样,你运行与Maven插件为Tomcat应用程序:

mvn clean install tomcat7:run

和你有一个provided在你的Maven的依赖范围元素:

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.36</version>
  <scope>provided</scope>
</dependency>

provided范围将防止连接器作为的一部分war归档和Tomcat的插件会发现没有连接建立数据库连接。

简单地拔下provided从依赖关系解决问题的范围。



Answer 4:

在我的情况下,问题与MySQL和MySQL的连接器的Java之间的版本不对齐相关。 后头痛的几天里,我在一个单独的干净项目拿出我ComboPooledDataSource模块,并试图用它连接到MySQL。 但是,我得到堆栈跟踪(不幸的是我忘了究竟是什么在那里),与我的理解,这个问题与版本有关。



Answer 5:

我得到了C3P0 0.9.5-PRE6与mchange公地的Java 0.2.6.3这个问题。 降级到0.9.5 C3P0,pre5和mchange公地的Java 0.2.6.2后,问题消失。



文章来源: A ResourcePool could not acquire a resource from its primary factory or source