池连接+验证查询+ SQL_CALC_FOUND_ROWS

时间:2013-07-02 10:16:13

标签: spring jdbc database-connection apache-commons-dbcp sql-calc-found-rows

我遇到的问题是我在tomcat上运行的Web应用程序经常会出现以下异常:

java.sql.SQLException: Connection already closed

所以我想在通过以下配置从池中获取连接之前验证连接:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />        
    <property name="url" value="jdbc:mysql://localhost/database" />
    <property name="username" value="user" />
    <property name="password" value="pass" />
    <property name="validationQuery" value="SELECT 1"/>
    <property name="testOnReturn" value="false"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testWhileIdle" value="false"/>
    <property name="maxActive" value="30" />
    <property name="maxIdle" value="15" />
    <property name="minIdle" value="5"/>
    <property name="poolPreparedStatements" value="true"/>
    <property name="connectionProperties"
        value="useUnicode=true;characterEncoding=UTF-8;profileSQL=true;logger=custom.MysqlLogger;" />
</bean>

使用spring jdbcTemplate发送以下2个查询时:

SELECT SQL_CALC_FOUND_ROWS col1, col2, col3 AS foo FROM table HAVING foo = 'bar' LIMIT 0, 10;
SELECT FOUND_ROWS();

“SELECT FOUND_ROWS()”查询始终返回1,因为验证查询在两者之间执行(“SELECT 1”)。

有人知道如何在第二次查询中不使用COUNT来解决此问题???

1 个答案:

答案 0 :(得分:2)

您的查询是否在同一个交易中?