写了个爬虫程序,在爬取的过程中,有时会出现下面的异常错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
很显然这是数据库连接异常,我采用的数据源是:org.apache.commons.dbcp.BasicDataSource 如下配置:
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://ip:port/kb?useUnicode=true&characterEncoding=utf8" />
<property name="username" value="userxxx" />
<property name="password" value="passwordxxx" />
<property name="validationQuery" value="select 1" />
</bean>
于是我换了另外一个数据源:com.mchange.v2.c3p0.ComboPooledDataSource 如下配置:
<bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=utf8" />
<property name="user" value="username" />
<property name="password" value="****" />
<property name="preferredTestQuery" value="select 1" />
</bean>
结果问题不再出现,所以说c3p0比dbcp要稳定。
PS:
DBCP数据源需要的jar包下载http://download.csdn.net/detail/llhhyy1989/4496980
C3P0数据源需要的jar包下载http://download.csdn.net/detail/llhhyy1989/4496988