Tomcat 连接池问题 - 无法在关闭的连接上调用方法

Tomcat connection pool issue- Cannot call a method on closed connection

我的应用程序正在使用 Spring 2.5.x 并部署在 Tomcat 服务器上。有时,当我的数据库连接空闲时,我会遇到以下错误:

[TeraJDBC 14.00.00.13] [Error 1095] [SQLState HY000] Cannot call a method on closed connection

这里是数据源配置

<bean id="dataSource"   class="org.apache.tomcat.jdbc.pool.DataSource"
          destroy-method="close">
    <property name="driverClassName" value="com.teradata.jdbc.TeraDriver"/>
    <property name="url" >
        <util:constant static-field="_DB_HOST"/>
    </property>
    <property name="username">
        <util:constant static-field="_DB_USER"/>
    </property>
    <property name="password">
        <util:constant static-field="_DB_PWD"/>
    </property>
    <property name="initialSize" value="1" />
    <property name="maxActive" value="50" />
</bean>

这里是否缺少任何配置?

虽然 Spring 的 JdbcTemplate 使用的所有连接在每个事务结束时关闭,但 Tomcat 的 JDBC Connection Pool 实际上从未 returns驱动程序获得的真实ConnectionDataSource#getConnection 总是 returns 一个代理,这样 Connection#close returns 到池的连接而不是物理关闭它。

因此,如 this answer, the connections are probably closed by the server. Therefore you need to configure the pool to validate the connections, as in the answer cited by Kayaman 中所述。

我怀疑你的问题不是连接问题,而是服务器策略导致的,所以我会设置:

<property name="validationQuery" value="SELECT 66353343" /> 
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />

为了每 60 秒检查一次物理连接是否正常。