Closed Marco-Sulla closed 5 years ago
Well, the solution was very simple, and I'm a bit embarassed to didn't think before:
@Override
public Groups getByGroupid(Integer groupid, Connection con) {
logger.debug("GroupsRepository.getByGroupid(): groupid: " + groupid);
final String sql = (
selectBase + "from GROUPS g " +
"where " +
"GROUPID = :groupid "
);
Groups res;
try (Query query = con.createQuery(sql)) {
query.addParameter("groupid", groupid);
res = query.executeAndFetchFirst(Groups.class);
}
return res;
}
I close the issue.
Our company uses Spring Boot 2 with sq2o. We also created a code generator for sql2o that creates Spring model, repository and service. We need to close the query because Oracle reach the max connection open. So currently we have to create a Repository like this:
and the method with connection is called by Service this way:
This way the query autoclose with try-with-resources. BUT we have to pass to the service the sql. The service should NOT know the sql, or be able to modify it. But currently this is the only way I found to do a try-with-resources with both
Connection
andQuery
.I propose to create a constructor of
Query
without the sql parameter and add a setter for the sql. If you add it, I can write the repository this way:and the service:
Much cleaner and robust.