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

后端 未结 7 2129
不知归路
不知归路 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 09:53

    I saw that ?autoReconnect=true wasn't working for me.

    What I did, is simply creating a function called: executeQuery with:

    private ResultSet executeQuery(String sql, boolean retry) {
        ResultSet resultSet = null;
        try {
            resultSet = getConnection().createStatement().executeQuery(sql);
        } catch (Exception e) {
                // disconnection or timeout error
            if (retry && e instanceof CommunicationsException || e instanceof MySQLNonTransientConnectionException
                            || (e instanceof SQLException && e.toString().contains("Could not retrieve transation read-only status server"))) {
                // connect again        
                connect();
                // recursive, retry=false to avoid infinite loop
                return executeQuery(sql,false);
            }else{
                throw e;
            }
        }
        return resultSet;
    }
    

    I know, I'm using string to get the error.. need to do it better.. but it's a good start, and WORKS :-)

    This will almost all reasons from a disconnect.

提交回复
热议问题