解决方案 »

  1.   

    DBCP 的配置,MySQL 8小时问题已经解决,亲测
    <?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.xsd">    <!-- 1. Data source config -->
        <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
            <!--<property name="driverClassName" value="com.mysql.jdbc.Driver" />-->
            <!--<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />-->
            <!--<property name="username" value="root" />-->
            <!--<property name="password" value="root" />-->
        <!--</bean>-->    <!-- 1. Data Source using DBCP. -->
        <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:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
            <property name="username" value="root" />
            <property name="password" value="root" />        <!-- 连接池启动时的初始值 -->
            <property name="initialSize" value="10" />
            <!-- 连接池的最大值 -->
            <property name="maxActive" value="100" />
            <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
            <property name="maxIdle" value="50" />
            <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
            <property name="minIdle" value="5" />
            <!--#给出一条简单的sql语句进行验证-->
            <property name="validationQuery" value="select NOW()" />
            <!--#在取出连接时进行有效验证-->
            <property name="testOnBorrow" value="false" />
            <property name="testWhileIdle" value="true" />
            <property name="logAbandoned" value="true" />
            <property name="removeAbandoned" value="true" />
            <property name="removeAbandonedTimeout" value="120" />
            <!-- #运行'判断连接超时任务'的时间间隔,单位为毫秒,默认为-1,即不执行任务。 -->
            <property name="timeBetweenEvictionRunsMillis" value="3600000" />
            <!-- #连接的超时时间,默认为半小时。 -->
            <property name="minEvictableIdleTimeMillis" value="3600000" />
        </bean>    <!-- 2. SQL session factory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mapperLocations" value="classpath:mapper/**/*.xml" /> <!-- Mapper xml -->
        </bean>    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
            <property name="basePackage" value="mapper" />
        </bean>    <!-- Enable Transaction -->
        <!--<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
            <!--<property name="dataSource" ref="dataSource" />-->
        <!--</bean>-->
        <!--<tx:annotation-driven transaction-manager="transactionManager"/>-->
    </beans>
      

  2.   

    但是我用的不是DBCP,我用的c3p0,而且没用spring 等框架,纯jdbc
      

  3.   

    Quote: 引用 4 楼 Inhibitory 的回复:

    DBCP 的配置,MySQL 8小时问题已经解决,亲测似乎在某些时候还是会出现此类问题
      

  4.   

    mysql 连接数超限 查看代码 使用完之后将statment rs  conn全部关闭掉。
    检查mysql连接数目方式:利用xshell 等连接工具,连接mysql ,使用以下命令
    show status like '%Threads_connected%';  当前连接数
    show status like '%Connections%';     试图连接数量,不管是否成功
    show status like '%Max_used_connections%'; 服务器启动后已同时使用的最大连接数