java jdbc mysql connector: how to resolve disconnection after a long idle time

后端 未结 7 2118
不知归路
不知归路 2020-12-11 09:51

I\'m using red5 1.0.0rc1 to create an online game. I\'m connecting to a MySQL database using a jdbc mysql connector v5.1.12

it seems that after several hours of idle

7条回答
  •  一生所求
    2020-12-11 10:01

    The MySQL JDBC driver has an autoreconnect feature that can be helpful on occasion; see "Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J"1, and read the caveats.

    A second option is to use a JDBC connection pool.

    A third option is to perform a query to test that your connection is still alive at the start of each transaction. If the connection is not alive, close it and open a new connection. A common query is SELECT 1. See also:

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

    A simple solution is to change the MySQL configuration properties to set the session idle timeout to a really large number. However:

    • This doesn't help if your application is liable to be idle for a really long time.
    • If your application (or some other application) is leaking connections, increasing the idle timeout could mean that lost connections stay open indefinitely ... which is not good for database memory utilization.

    1 - If the link breaks (again), please Google for the quoted page title then edit the answer to update it with the new URL.

提交回复
热议问题