akrentz6 / Economy

A Minecraft Economy plugin for 1.8-1.16
8 stars 6 forks source link

JDBC Memory Leakage #16

Open r5t0neer opened 9 months ago

r5t0neer commented 9 months ago

Priority: HIGH/EMERGENCY Issue: Memory leakage Problematic functions:

It causes lags on server and server requires restart every 4 hours. I analyzed heap dump and it leads to this plugin.

It's incremental memory leakage due to mishandling correctly JDBC PreparedStatement/Statement and ResultSet. The reason is not closing them and they are left present in com.mysql.cj.jdbc.ConnectionImpl#openStatements (CopyOnWriteArrayList):

/**

  • An array of currently open statements.
  • Copy-on-write used here to avoid ConcurrentModificationException when statements unregister themselves while we iterate over the list. */ private final CopyOnWriteArrayList openStatements = new CopyOnWriteArrayList<>();

Just add these lines before returning anything from function in SQLEconomy.java functions:

result.close(); statement.close();

Also consider surrounding ResultSet with nested try-catch inside already existing try-catch, so statement can be closed if ResultSet fails, and optionally add "&dontTrackOpenResources=true" to jdbc connection parameters (at the end of string), so that statements are not added to openStatements array.

Scheggia0 commented 4 months ago

Same problem, thank you