How to use JDBC in Java to implement continuous updates of the database server without increasing RAM consumption
P粉323224129
P粉323224129 2023-09-07 20:10:29
0
1
400

I wrote a fighting game in JavaFX, using JDBC to update information into a MySQL database. Most tasks that get basic information from the database server work fine if they are not repeated continuously. The problem arises when I need the program to constantly update the opponent's information, such as position, level, damage, etc. in the game. I have to constantly send select commands to the server, which increases the RAM used by the thread running these commands.

void startUpdatingOpponentInfo() {
    Thread thread = new Thread(() -> {
        while (matchID != -1) {
            String query = String.format("select * from matchDetails where matchID = %d and userID = %d", matchID, opponentID);
            try {
                ResultSet resultSet = sqlConnection.getDataQuery(query);
                while (resultSet.next()) {
                    double opponentX = resultSet.getDouble("xPos");
                    double opponentY = resultSet.getDouble("YPos");
                    if (opponentX != -1 && opponentY != -1)
                        opponent.move(canvas, new Position(opponentX, opponentY));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
}

  public ResultSet getDataQuery(String query) {
    Statement statement;
    ResultSet resultSet = null;
    try {
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);
    } catch (SQLException e) {
        System.out.println(query);
        e.printStackTrace();
    }
    return resultSet;
}

I tried searching for some methods and found a very useful method. After each select, I let the thread sleep for a while before making another select. This approach significantly improves performance and RAM consumption remains stable. However, this approach is not suitable for query information that needs to be refreshed all the time, because the short sleep of the "select" thread will still cause high RAM consumption. Is there any useful way to solve this problem and let my game connect directly to the database server without going through the intermediate game server.

P粉323224129
P粉323224129

reply all(1)
P粉136356287

You should try to optimize your variables first.

Your thread keeps using the new String when querying, but actually it only needs to be constructed once. Likewise, for your ResultSet, each time you enter a new loop, you use a new location in RAM to store a new object.

You can check this by adding a console output outside of the Java ID object:

System.out.println(resultSet);

Then observe whether the output results are different each time.

like this:

Thread thread = new Thread(() -> {

        ResultSet resultSet;
        final String query = String.format("select * from matchDetails where matchID = %d and userID = %d", matchID, opponentID);

        while (matchID != -1) {
            try {
                resultSet = sqlConnection.getDataQuery(query);

Then I asked you to consider changing your query to only select values ​​where xPos/yPos is not equal to "-1", and only get the results of real opponent position changes, and finally, maybe not send a "new Position" instead Only sending the two variables oppositionX and oppositionY avoids storing this object in RAM.

Don't forget, Java only cleans up your RAM objects after code execution is complete (in short), so having a single thread that never ends won't trigger this.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!