Skip to content

Commit 0db2c7d

Browse files
committed
Merge pull request #33729 from dssievewright
* pr/33729: Polish "Prevent further configuration once SqlCall is compiled" Prevent further configuration once SqlCall is compiled Closes gh-33729
2 parents 83ab717 + 35dea59 commit 0db2c7d

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java

+8
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ protected CallableStatementCreatorFactory getCallableStatementFactory() {
248248
* @param parameter the {@link SqlParameter} to add
249249
*/
250250
public void addDeclaredParameter(SqlParameter parameter) {
251+
if (isCompiled()) {
252+
throw new IllegalStateException("SqlCall for " + (isFunction() ? "function" : "procedure") +
253+
" is already compiled");
254+
}
251255
Assert.notNull(parameter, "The supplied parameter must not be null");
252256
if (!StringUtils.hasText(parameter.getName())) {
253257
throw new InvalidDataAccessApiUsageException(
@@ -265,6 +269,10 @@ public void addDeclaredParameter(SqlParameter parameter) {
265269
* @param rowMapper the RowMapper implementation to use
266270
*/
267271
public void addDeclaredRowMapper(String parameterName, RowMapper<?> rowMapper) {
272+
if (isCompiled()) {
273+
throw new IllegalStateException("SqlCall for " + (isFunction() ? "function" : "procedure") +
274+
" is already compiled");
275+
}
268276
this.declaredRowMappers.put(parameterName, rowMapper);
269277
if (logger.isDebugEnabled()) {
270278
logger.debug("Added row mapper for [" + getProcedureName() + "]: " + parameterName);

spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/SimpleJdbcCallTests.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,17 +25,20 @@
2525

2626
import javax.sql.DataSource;
2727

28+
import org.assertj.core.api.InstanceOfAssertFactories;
2829
import org.junit.jupiter.api.BeforeEach;
2930
import org.junit.jupiter.api.Test;
3031

3132
import org.springframework.dao.InvalidDataAccessApiUsageException;
3233
import org.springframework.jdbc.BadSqlGrammarException;
34+
import org.springframework.jdbc.core.RowMapper;
3335
import org.springframework.jdbc.core.SqlOutParameter;
3436
import org.springframework.jdbc.core.SqlParameter;
3537
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
3638

3739
import static org.assertj.core.api.Assertions.assertThat;
3840
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
41+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3942
import static org.mockito.BDDMockito.given;
4043
import static org.mockito.Mockito.atLeastOnce;
4144
import static org.mockito.Mockito.mock;
@@ -360,4 +363,37 @@ void correctSybaseFunctionStatementNamed() throws Exception {
360363
verifyStatement(adder, "{call ADD_INVOICE(@AMOUNT = ?, @CUSTID = ?)}");
361364
}
362365

366+
@Test
367+
void declareParametersCannotBeInvokedWhenCompiled() {
368+
SimpleJdbcCall call = new SimpleJdbcCall(dataSource)
369+
.withProcedureName("procedure_name")
370+
.declareParameters(new SqlParameter("PARAM", Types.VARCHAR));
371+
call.compile();
372+
assertThatIllegalStateException()
373+
.isThrownBy(() -> call.declareParameters(new SqlParameter("Ignored Param", Types.VARCHAR)))
374+
.withMessage("SqlCall for procedure is already compiled");
375+
}
376+
377+
@Test
378+
void addDeclaredRowMapperCanBeConfigured() {
379+
SimpleJdbcCall call = new SimpleJdbcCall(dataSource)
380+
.withProcedureName("procedure_name")
381+
.returningResultSet("result_set", (rs, i) -> new Object());
382+
383+
assertThat(call).extracting("declaredRowMappers")
384+
.asInstanceOf(InstanceOfAssertFactories.map(String.class, RowMapper.class))
385+
.containsOnlyKeys("result_set");
386+
}
387+
388+
@Test
389+
void addDeclaredRowMapperWhenCompiled() {
390+
SimpleJdbcCall call = new SimpleJdbcCall(dataSource)
391+
.withProcedureName("procedure_name")
392+
.returningResultSet("result_set", (rs, i) -> new Object());
393+
call.compile();
394+
assertThatIllegalStateException()
395+
.isThrownBy(() -> call.returningResultSet("not added", (rs, i) -> new Object()))
396+
.withMessage("SqlCall for procedure is already compiled");
397+
}
398+
363399
}

0 commit comments

Comments
 (0)