Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent further configuration once AbstractJdbcCall is compiled #33729

Closed

Conversation

dssievewright
Copy link

During profiling of our application, we noticed a potential memory leak. We had a SimpleJdbcCall object as a field in a DAO. Typically, we would create and initialize this object during the PostConstruct phase and runtime would just need to set up the inputs and call execute. However the developer accidentally put the declareParameters in the same method as execution so those paremeters kept getting added.

Example DAO:

@Resource
private JdbcTemplate jdbcTemplate;

private SimpleJdbcCall procedure;

@PostConstruct
void buildProcedure() {
  procedure = new SimpleJdbcCall(jdbcTemplate).withProcedureName("some_procedure");
}

void executeProcedure(String input) {
  procedure.declareParameters(new SqlParameter("param", Types.VARCHAR));
  procedure.execute(Collections.singletonMap("param", input));
}

The first time executeProcedure is called, the procedure gets compiled and all the parameters get set in the callMetaDataContext. Subsequent calls to declareParameters do not alter the callMetaDataContext and the procedure executes successfully.

The problem: Each time declareParameters is called, that parameter gets added to declaredParameters in AbstractJdbcCall where it will sit there, never gets used, and never gets released for garbage collection.

This proposed solution here is to check to see if the call has been compiled before adding to the declaredParameters list. If the call is not compiled, we'll proceed as normal. Otherwise, AbstractJdbcCall will ignore the request because the parameter won't be added to the callMetaDataContext.

Check added to addDeclaredParameter and addDeclaredRowMapper
Junits added for verifying existing and new logic
Protects against parameter lists continuously growing
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 17, 2024
@sbrannen sbrannen added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Oct 17, 2024
@snicoll
Copy link
Member

snicoll commented Feb 4, 2025

@dssievewright thanks for the PR.

The problem: Each time declareParameters is called, that parameter gets added to declaredParameters in AbstractJdbcCall where it will sit there, never gets used, and never gets released for garbage collection.

I can see that but the memory leak is in the use of the class, not the class itself. Rather than ignoring the call (which looks odd to me), I'd rather throw an exception since it wouldn't change the state of the object anyway. What do you think @jhoeller?

@snicoll snicoll self-assigned this Feb 4, 2025
@snicoll snicoll added status: waiting-for-feedback We need additional information before we can continue status: waiting-for-internal-feedback An issue that needs input from a member or another Spring Team type: enhancement A general enhancement and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on status: waiting-for-internal-feedback An issue that needs input from a member or another Spring Team labels Feb 4, 2025
@snicoll snicoll added this to the 7.0.0-M2 milestone Feb 4, 2025
snicoll pushed a commit that referenced this pull request Feb 4, 2025
This commit prevents AbstractJdbcCall to be further configured once it
is compiled.

See gh-33729
@snicoll snicoll closed this in 0db2c7d Feb 4, 2025
@snicoll snicoll changed the title Prevent declaredParameter list from Modification after compiling AbstractJdbcCall Prevent further configuration once AbstractJdbcCall is compiled Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants