[C3p0] java. SQL. sqlexception: an attempt by a client to checkout a connection...

Source: Internet
Author: User

Java. SQL. sqlexception: an attempt by a client to checkout a connection has timed out.

Error message:

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

When c3p0 is configured in spring, there is a Configuration Attribute checkouttimeout. It is normal to remove this configuration attribute.

The source code is as follows:

Spring configuration file: applicationcontext. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Beans
Xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<Bean id = "propertyconfigurer"
Class = "org. springframework. Beans. Factory. config. propertyplaceholderconfigurer">
<Property name = "location">
<Value> classpath: JDBC. properties </value>
</Property>
</Bean>
<Bean id = "datasource" class = "com. mchange. v2.c3p0. combopooleddatasource">
<Property name = "driverclass" value = "oracle. JDBC. Driver. oracledriver"/>
<Property name = "jdbcurl" value = "$ {JDBC. url}"/>
<Property name = "user" value = "$ {JDBC. Username}"/>
<Property name = "password" value = "$ {JDBC. Password}"/>

<Property name = "autocommitonclose" value = "true"/>

<Property name = "checkouttimeout" value = "$ {cpool. checkouttimeout}"/>

<Property name = "initialpoolsize" value = "$ {cpool. minpoolsize}"/>
<Property name = "minpoolsize" value = "$ {cpool. minpoolsize}"/>

<Property name = "maxpoolsize" value = "$ {cpool. maxpoolsize}"/>
<Property name = "maxidletime" value = "$ {cpool. maxidletime}"/>
<Property name = "acquireincrement" value = "$ {cpool. acquireincrement}"/>
<Property name = "maxidletimeexcessconnections" value = "$ {cpool. maxidletimeexcessconnections}"/>
</Bean>

<! -- Transaction Manager for a single JDBC datasource -->
<Bean id = "transactionmanager" class = "org. springframework. JDBC. datasource. cetcetransactionmanager">
<Property name = "datasource" ref = "datasource"/>
</Bean>

<! -- ====================================== Dao definitions: dbutils implementations ===========================-->

<Bean id = "dbconn" class = "services. callcenter. Common. dbconnection">
<Property name = "ds" ref = "datasource"/>
</Bean>

<Bean id = "dbutilhelper" class = "services. callcenter. Common. dbutilhelper">
<Property name = "ds" ref = "datasource"> </property>
</Bean>
</Beans> 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;
}

}

The JUnit test code is as follows:

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 ");
}
}

For spring code testing, spring uses configuration files to inject certain attributes, while JUnit does not read these configuration files by default. Therefore, when testing the spring class, you need to manually specify the configuration file and then load the configuration information through the classpathxmlapplicationcontext class.

Put the applicationcontext. xml file and JDBC. properties file in the src directory.

Of course, the test code here only uses system. Out. println to print the information directly, and does not use assert to judge the information, so don't go into it!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.