Cheapest way to to determine if a MySQL connection is still alive

前端 未结 3 1533
天命终不由人
天命终不由人 2020-12-14 20:48

I have a pool of MySQL connections for a web-based data service. When it starts to service a request, it takes a connection from the pool to use. The problem is that if ther

3条回答
  •  时光说笑
    2020-12-14 21:34

    I'm not sure what API you are currently using (or what language), but for Java, there is a special trick the JDBC driver can do.

    The standard test query is:

    select 1
    

    as you've indicated. If you modify it to:

    /* ping */ select 1
    

    the JDBC driver will notice this, and send only a single packet to the MySQL server to get a response.

    I learned about this at a Sun 'Deep Dive' episode titled MySQL Tips for Java Developers With Mark Matthews.

    Even if you aren't using Java, maybe this same trick has been implemented in other mysql drivers? I assume the server would need to be aware of this special packet so it can send a response...

提交回复
热议问题