Spring 测试代码的写法以及一个c3p0的错误
2009-03-03 21:45
302 查看
错误信息:
An attempt by a client to checkout a Connection has timed out.
java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:65)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:527)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at services.callcenter.common.DBUtilHelper.searchToMap(DBUtilHelper.java:278)
at services.callcenter.test.PersonDaoImplTest.testPersonDaoImpl(PersonDaoImplTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResourcePool@4ac216 -- timeout at awaitAvailable()
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1317)
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)
... 22 more
问题原因:
这个问题我在google上搜索了,没有找到有具体的人给出答案。后经过我自己的测试,发现问题原因是配置出错。
是Spring中配置c3p0的时候,有一个配置属性是checkoutTimeout,把这个配置属性去掉就正常了。
源代码如下:
Spring配置文件:applicationContext.xml
classpath:jdbc.properties
DBUtilHelper中类方法的写法
public class DBUtilHelper {
private DataSource ds;
public void setDs(DataSource ds) {
this.ds = ds;
}
public Map searchToMap(String sql) {
Connection conn = null;
Map result = null;
QueryRunner run = new QueryRunner();
ResultSetHandler h = new MapHandler();
try {
conn = ds.getConnection();
result = (Map) run.query(conn,sql, h);
}
catch (Exception e){
log.warn(sql);
log.warn(e.getMessage(),e);
}
finally {
try{
DbUtils.close(conn);
}
catch(Exception e){
log.warn(e.getMessage(),e);
}
}
return result;
}
}
Junit测试代码如下:
package ***;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonDaoImplTest extends TestCase{
ApplicationContext ac = null;
@Test
public void testPersonDaoImpl() throws Exception {
DBUtilHelper db=(DBUtilHelper)ac.getBean("DBUtilHelper");
String sql = "select user_id from ur_users where user_id = 371 ";
Map m = db.searchToMap(sql);
System.out.println(m.toString());
}
protected void setUp() throws Exception{
System.out.println("DDDDD");
ac=new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
关于Spring代码的测试,因为Spring采用配置文件来注入某些属性,而Junit在默认的测试情况下,是不会读取这些配置文件的。所以测试Spring类的时候,需要手工指定配置文件,然后通过ClassPathXmlApplicationContext类来加载这些配置信息。
要注意把applicationContext.xml文件以及jdbc.properties文件放到src目录下。
当然,这里的测试代码只是用System.out.println直接打印出来信息,没有采用assert来判断信息,大家就别深究了!
An attempt by a client to checkout a Connection has timed out.
java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:65)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:527)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at services.callcenter.common.DBUtilHelper.searchToMap(DBUtilHelper.java:278)
at services.callcenter.test.PersonDaoImplTest.testPersonDaoImpl(PersonDaoImplTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResourcePool@4ac216 -- timeout at awaitAvailable()
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1317)
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)
... 22 more
问题原因:
这个问题我在google上搜索了,没有找到有具体的人给出答案。后经过我自己的测试,发现问题原因是配置出错。
是Spring中配置c3p0的时候,有一个配置属性是checkoutTimeout,把这个配置属性去掉就正常了。
源代码如下:
Spring配置文件:applicationContext.xml
classpath:jdbc.properties
DBUtilHelper中类方法的写法
public class DBUtilHelper {
private DataSource ds;
public void setDs(DataSource ds) {
this.ds = ds;
}
public Map searchToMap(String sql) {
Connection conn = null;
Map result = null;
QueryRunner run = new QueryRunner();
ResultSetHandler h = new MapHandler();
try {
conn = ds.getConnection();
result = (Map) run.query(conn,sql, h);
}
catch (Exception e){
log.warn(sql);
log.warn(e.getMessage(),e);
}
finally {
try{
DbUtils.close(conn);
}
catch(Exception e){
log.warn(e.getMessage(),e);
}
}
return result;
}
}
Junit测试代码如下:
package ***;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonDaoImplTest extends TestCase{
ApplicationContext ac = null;
@Test
public void testPersonDaoImpl() throws Exception {
DBUtilHelper db=(DBUtilHelper)ac.getBean("DBUtilHelper");
String sql = "select user_id from ur_users where user_id = 371 ";
Map m = db.searchToMap(sql);
System.out.println(m.toString());
}
protected void setUp() throws Exception{
System.out.println("DDDDD");
ac=new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
关于Spring代码的测试,因为Spring采用配置文件来注入某些属性,而Junit在默认的测试情况下,是不会读取这些配置文件的。所以测试Spring类的时候,需要手工指定配置文件,然后通过ClassPathXmlApplicationContext类来加载这些配置信息。
要注意把applicationContext.xml文件以及jdbc.properties文件放到src目录下。
当然,这里的测试代码只是用System.out.println直接打印出来信息,没有采用assert来判断信息,大家就别深究了!
相关文章推荐
- 获取与一个磁盘的组织以及剩余空间容量有关的信息以及代码测试
- VS2005发布网站问题及"aspnet_merge.exe”已退出,代码为 1的错误以及所有代码文件生成一个dll
- 实现一个无锁的Stack,并写一段测试代码(多线程访问),证明这个Stack是线程安全的。给出程序以及运行的截图。
- 一种奇怪的错误,执行一个junit测试用例,代码却执行了很多遍
- Spring的AOP配置以及常见错误(内附代码)
- 经常在网上搜怎样获得外网的ip,代码几乎都一样,但测试,都出现错误!最后找了一个既蠢既笨的终极解决方案
- 请大家看一下一个c语言中的链表问题,下面的代码是有错误的!!请大家说出错误的原因,以及修改的方法!!!
- 最近写的一个Spring Web例子测试
- 一个很难发现的错误:Spring对事务进行配置时发生的类消息处理器配置错误
- jsp中上传大文件(200M已测试),存到数据库,易发生连接超时的ie找不到网页的错误,其解决方法。略写代码,参考(.net修改代码应该可以,改天修改)
- Hibernate连接池三种配置实例 以及解决C3P0错误:com.mchange.v2.c3p0.stmt.GooGooStatementCache - Problem with checked-in Statement, discarding.
- 错误代码:WLTC0032W ibatis作为持久层,websphere 报告连接没有提交错误,问题的解决以及产生的原因
- arm wince 有关 Microsoft ActiveSync 4.0 错误代码、 错误消息, 以及如何解决错误代码
- 一个Struts2结合Spring测试的方法
- c3p0参数的说明以及整合spring使用
- Call requires API level 16 (current min is 8 一个正常的代码出现这个错误提示: 解决方法: 在工程上右键 Android Tools | Clear
- 安装部署(六) Kafka集群安装部署以及Producer和Consumer的JAVA代码测试
- 错误代码以及错误消息提示-如何更好地管理与维护消息资源
- 如何在GITHUB上FORK一个项目来贡献代码以及同步原作者的修改
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea