I have heard many times that not closing database connections might lead to a memory leak.
(For example in this article)
I tried reproducing the same issue by getting a connection from org.apache.commons.dbcp2.BasicDataSource
and not closing it.
This is my code:
//Repo class
private final BasicDataSource ds;
public Repo() {
ds = new BasicDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUrl("jdbc:postgresql://localhost/postgres");
ds.setUsername("postgres");
ds.setPassword("postgres");
ds.setMaxOpenPreparedStatements(10000);
ds.setMaxTotal(10000);
}
public PreparedStatement prepStatement(String sql) throws SQLException {
return this.ds.getConnection().prepareStatement(sql);
}
//Logic class
public JsonNode logic(String name) {
PreparedStatement ps = this.repo.prepStatement("select data from public.users where name = ? ");
ps.setString(1, name);
//The rest of the logic - no close method or try with resource
}
I have repeated the same process almost 400 times and also decreased the initial and max heap sizes.
Still, there was no sign of a memory leak. Even in the VisualVM monitoring, the heap graph seems pretty normal:
Any ideas on how to reproduce this issue?
