在是用BoneCP的时候,由于MySQL的wait_timeout配置是8小时,当8小时候,BoneCP连接池中的空闲连接会被MySQL服务器清除掉,当时以为是没有配置BoneCP的idleMaxAge和idleConnectionTestPeriod参数导致的,但是应该有默认值啊,idleConnectionTestPeriod的默认值是240分钟(即4个小时),那么每4个小时向MySQL发送一个测试SQL,应该不会被MySQL认为是空闲连接啊,那么应该是测试发送没有进行了,为了证明这点,测试如下:
把MySQL的wait_timeout改为5分钟,BoneCP的idleMaxAge设为4分钟,idleConnectionTestPeriod设为3分钟,结果到了5分钟后查看mysql的连接情况,发现一个连接也没有了,这是为什么呢?研究一天了,兄弟姐们,救救我!
补充下BoneCP的配置:
partitionCount=1
maxConnectionsPerPartition=20
minConnectionsPerPartition=10

解决方案 »

  1.   

    哎,原来是0.6.5的bug,使用0.7.0就解决此问题了。太信任他们了,以为是我配置或者数据库环境的问题呢。
      

  2.   

    兄弟,你确定0.7.0版本可以吗?我最近用了0.7.1release版本,仍然是不检查,我改用0.7.0还是如此。是否我做错了什么?请赐教,谢谢!我的参数配置如下:
    0.7.0:
    <property name="bonecp.idleMaxAge">50</property>
    <property name="bonecp.idleConnectionTestPeriod">50</property>
    0.7.1release:
    <property name="bonecp.idleMaxAgeInSeconds">50</property>
    <property name="bonecp.idleConnectionTestPeriodInSeconds">50</property>
    然后我设置mysql的wait_timeout=60,
    如果执行了检查,在mysql断掉连接之前bonecp就已经对空闲连接进行处理了,那么是不会存在无效连接的。
    但是我通过两个线程执行数据库查询监测,发现bonecp并没有检查。
    每个线程每隔80秒执行一次查询,两次查询的间隔大于连接空闲时间。以下是我的检查代码片段:
    public static void main(String[] args) throws InterruptedException {
    Timer t1 = new ResourceServiceManagerTester().new Timer("A");
    Timer t2 = new ResourceServiceManagerTester().new Timer("B");
    t1.start();
    t2.start();
    }
    class Timer extends Thread {
    private String name = "";
    public Timer(String name) {
    this.name = name;
    }
    public void run() {
    while (true) {
    String hql = "select r.rsid, r.lon, r.lat from ResourceService r ";
    ResourceServiceDAO dao = new ResourceServiceDAO();
    List<Object[]> result = dao.findResources(hql);
    System.out.println(this.name + ":rowcount=" + result.size());
    try {
    Thread.sleep(80000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    以下是执行错误:
    警告: SQL Error: 0, SQLState: 08S01
    2012-11-23 17:54:24 org.hibernate.util.JDBCExceptionReporter logExceptions
    严重: Communications link failureLast packet sent to the server was 1 ms ago.
    Exception in thread "Thread-0" org.hibernate.exception.JDBCConnectionException: could not execute query using iterate
    ......