Java ResourcePool 无法从其主要工厂或来源获取资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16528004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A ResourcePool could not acquire a resource from its primary factory or source
提问by iomartin
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.
我正在尝试使用 jdbcTemplate 连接到 Java 中的数据库,但出现以下错误。我在谷歌上搜索了很长时间,我找到的所有解决方案都没有解决我的问题。我尝试了几个不同的数据库(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)
...
This is my properties file:
这是我的属性文件:
app.driverClassName=net.sourceforge.jtds.jdbc.Driver
app.url=jdbc:sqlserver://myUrl:port;databaseName=my_database
app.username=myUsername
app.password=myPassword
webapp/WEB-INF/applicationContext-database.xml:
webapp/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 class:
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;
}
采纳答案by iomartin
For anyone that finds this question in the future. What I was doing wrong was that I was using the jtds driver and I forgot to add that in the url. So in my properties file what I should have done was:
对于将来发现这个问题的任何人。我做错的是我使用的是 jtds 驱动程序,但我忘了在 url 中添加它。所以在我的属性文件中我应该做的是:
app.url=jdbc:jtds:sqlserver://myUrl:port;databaseName=my_database
回答by Conan
I got this problem on c3p0 0.9.5-pre6 with mchange-commons-java 0.2.6.3. After downgrading to c3p0 0.9.5-pre5 and mchange-commons-java 0.2.6.2, the problem disappears.
我在使用 mchange-commons-java 0.2.6.3 的 c3p0 0.9.5-pre6 上遇到了这个问题。降级到 c3p0 0.9.5-pre5 和 mchange-commons-java 0.2.6.2 后,问题消失。
回答by OldCurmudgeon
For anyone that finds this question in the future.
对于将来发现这个问题的任何人。
This can also be caused by a missing database driver.
这也可能是由于缺少数据库驱动程序造成的。
In my case I was using the maven-shade-plugin
with the minimizeJar
option set. This - of course - was throwing away the jtds
driver because it is not directly referenced anywhere.
在我的情况下,我使用maven-shade-plugin
了minimizeJar
选项集。这 - 当然 - 丢弃了jtds
驱动程序,因为它没有在任何地方直接引用。
This can be fixed as follows:
这可以修复如下:
<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>
回答by Stephane
This message can also be displayed, if, like me, you run your application with the Maven plugin for Tomcat:
如果像我一样使用 Tomcat 的 Maven 插件运行应用程序,也可以显示此消息:
mvn clean install tomcat7:run
and you have a provided
scope element in your Maven dependency:
并且您provided
的 Maven 依赖项中有一个scope 元素:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
<scope>provided</scope>
</dependency>
The provided
scope will prevent the connector from being part of the war
archive and the Tomcat plugin will find no connector to establish the database connection.
该provided
范围将防止连接器作为的一部分war
存档和Tomcat插件将找不到连接器来建立数据库连接。
Simply removing the provided
scope from the dependency solves the issue.
只需provided
从依赖项中删除范围即可解决问题。
回答by dimson
In my case the problem was related with version mismach between MySQL and mysql-connector-java. After few days of headache I took out my ComboPooledDataSource module in a separate clean project and tried to connect with it to MySQL. However, I got stacktrace (unfortunatly I forgot what exactly were there), with which I understood that the problem is related with versions.
就我而言,问题与 MySQL 和 mysql-connector-java 之间的版本不匹配有关。经过几天的头痛,我在一个单独的干净项目中取出了我的 ComboPooledDataSource 模块,并尝试将其连接到 MySQL。但是,我得到了堆栈跟踪(不幸的是我忘记了那里到底有什么),我知道问题与版本有关。