Spring jdbcTemplate 卡在 Fetching JDBC Connection

我遇到了 spring jdbcTemplate 的问题。它曾经可以正常工作,但现在它既不返回任何值也不返回错误消息。我的表变大了,大约有 3500 万条记录,这可能是造成这种情况的原因。

但我主要担心的是 jdbcTemplate 没有抛出任何异常,也没有将控件释放到下一行代码并且我的应用程序中断。如果由于某种原因无法执行查询操作,我希望 jdbcTemplate 释放连接。下面是我在启用调试日志后从 log4j 获得的代码、数据源详细信息和日志语句。获取 JDBC 连接后没有记录任何内容。

SqlRowSet oRs = jdbcTemplate.queryForRowSet(strSql, new Object[] { Integer.valueOf(1) });

数据源配置

<?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-3.0.xsd ">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:4928/dbtest" />
        <property name="username" value="root" />
        <property name="password" value="" />
        <property name="removeAbandoned" value="true" />
        <property name="initialSize" value="3" />
        <property name="maxActive" value="10" />
    </bean>
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="sendMail" class="com.app.SendMails">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>
    <bean id="response" class="com.app.BuildResponses">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>
</beans>

日志:

DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Executing prepared SQL statement 
DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource

任何帮助表示赞赏

stack overflow Spring jdbcTemplate is stuck at Fetching JDBC Connection
原文答案

答案:

作者头像

Could it be that the JDBC connection pool is exhausted and the thread is waiting for a connection ? Do you have other threads or transactions which use the same data source ?

To check if the connection pool is exhausted add

<property name="maxWait" value="1000"/>

To your dataSource bean definition. You will get an exception if a connection can't be allocated within timeout which is 1 second (1000 milliseconds).

BasicDataSource waits forever when borrowing a connection from the pool unless you specify a timeout.