Skip to content

Commit 3b75ffa

Browse files
committed
Using non-blocking optimization to compile function.
1 parent cd76835 commit 3b75ffa

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Set;
24+
import java.util.concurrent.atomic.AtomicBoolean;
2425

2526
import javax.sql.DataSource;
2627

@@ -70,7 +71,7 @@ public abstract class AbstractJdbcCall {
7071
* Has this operation been compiled? Compilation means at least checking
7172
* that a DataSource or JdbcTemplate has been provided.
7273
*/
73-
private volatile boolean compiled;
74+
private final AtomicBoolean compiled = new AtomicBoolean(false);
7475

7576
/** The generated string used for call statement. */
7677
private @Nullable String callString;
@@ -278,19 +279,18 @@ public void addDeclaredRowMapper(String parameterName, RowMapper<?> rowMapper) {
278279
* @throws org.springframework.dao.InvalidDataAccessApiUsageException if the object hasn't
279280
* been correctly initialized, for example if no DataSource has been provided
280281
*/
281-
public final synchronized void compile() throws InvalidDataAccessApiUsageException {
282-
if (!isCompiled()) {
283-
if (getProcedureName() == null) {
284-
throw new InvalidDataAccessApiUsageException("Procedure or Function name is required");
285-
}
282+
public final void compile() throws InvalidDataAccessApiUsageException {
283+
if (getProcedureName() == null) {
284+
throw new InvalidDataAccessApiUsageException("Procedure or Function name is required");
285+
}
286+
if (this.compiled.compareAndSet(false, true)) {
286287
try {
287288
this.jdbcTemplate.afterPropertiesSet();
288289
}
289290
catch (IllegalArgumentException ex) {
290291
throw new InvalidDataAccessApiUsageException(ex.getMessage());
291292
}
292293
compileInternal();
293-
this.compiled = true;
294294
if (logger.isDebugEnabled()) {
295295
logger.debug("SqlCall for " + (isFunction() ? "function" : "procedure") +
296296
" [" + getProcedureName() + "] compiled");
@@ -335,7 +335,7 @@ protected void onCompileInternal() {
335335
* @return whether this operation is compiled and ready to use
336336
*/
337337
public boolean isCompiled() {
338-
return this.compiled;
338+
return this.compiled.get();
339339
}
340340

341341
/**

Diff for: spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929
import java.util.Locale;
3030
import java.util.Map;
31+
import java.util.concurrent.atomic.AtomicBoolean;
3132

3233
import javax.sql.DataSource;
3334

@@ -82,7 +83,7 @@ public abstract class AbstractJdbcInsert {
8283
* Has this operation been compiled? Compilation means at least checking
8384
* that a DataSource or JdbcTemplate has been provided.
8485
*/
85-
private volatile boolean compiled;
86+
private final AtomicBoolean compiled = new AtomicBoolean(false);
8687

8788
/** The generated string used for insert statement. */
8889
private String insertString = "";
@@ -268,23 +269,22 @@ public boolean isQuoteIdentifiers() {
268269
* @throws InvalidDataAccessApiUsageException if the object hasn't been correctly initialized,
269270
* for example if no DataSource has been provided
270271
*/
271-
public final synchronized void compile() throws InvalidDataAccessApiUsageException {
272-
if (!isCompiled()) {
273-
if (getTableName() == null) {
274-
throw new InvalidDataAccessApiUsageException("Table name is required");
275-
}
276-
if (isQuoteIdentifiers() && this.declaredColumns.isEmpty()) {
277-
throw new InvalidDataAccessApiUsageException(
278-
"Explicit column names must be provided when using quoted identifiers");
279-
}
272+
public final void compile() throws InvalidDataAccessApiUsageException {
273+
if (getTableName() == null) {
274+
throw new InvalidDataAccessApiUsageException("Table name is required");
275+
}
276+
if (isQuoteIdentifiers() && this.declaredColumns.isEmpty()) {
277+
throw new InvalidDataAccessApiUsageException(
278+
"Explicit column names must be provided when using quoted identifiers");
279+
}
280+
if (this.compiled.compareAndSet(false, true)) {
280281
try {
281282
this.jdbcTemplate.afterPropertiesSet();
282283
}
283284
catch (IllegalArgumentException ex) {
284285
throw new InvalidDataAccessApiUsageException(ex.getMessage());
285286
}
286287
compileInternal();
287-
this.compiled = true;
288288
if (logger.isDebugEnabled()) {
289289
logger.debug("JdbcInsert for table [" + getTableName() + "] compiled");
290290
}
@@ -320,7 +320,7 @@ protected void onCompileInternal() {
320320
* @return whether this operation is compiled and ready to use
321321
*/
322322
public boolean isCompiled() {
323-
return this.compiled;
323+
return this.compiled.get();
324324
}
325325

326326
/**

0 commit comments

Comments
 (0)