From 807c3edfc9261570deb6ac4a638c17055079df7a Mon Sep 17 00:00:00 2001 From: lucky8987 Date: Mon, 30 Dec 2024 22:06:30 +0800 Subject: [PATCH] Using non-blocking optimization to compile function. --- .../jdbc/core/simple/AbstractJdbcCall.java | 16 ++++++------- .../jdbc/core/simple/AbstractJdbcInsert.java | 24 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java index d09c7e9e6c77..8b88a6430ca5 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcCall.java @@ -22,6 +22,7 @@ import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import javax.sql.DataSource; import org.apache.commons.logging.Log; @@ -70,7 +71,7 @@ public abstract class AbstractJdbcCall { * Has this operation been compiled? Compilation means at least checking * that a DataSource or JdbcTemplate has been provided. */ - private volatile boolean compiled; + private final AtomicBoolean compiled = new AtomicBoolean(false); /** The generated string used for call statement. */ @Nullable @@ -284,11 +285,11 @@ public void addDeclaredRowMapper(String parameterName, RowMapper rowMapper) { * @throws org.springframework.dao.InvalidDataAccessApiUsageException if the object hasn't * been correctly initialized, for example if no DataSource has been provided */ - public final synchronized void compile() throws InvalidDataAccessApiUsageException { - if (!isCompiled()) { - if (getProcedureName() == null) { - throw new InvalidDataAccessApiUsageException("Procedure or Function name is required"); - } + public final void compile() throws InvalidDataAccessApiUsageException { + if (getProcedureName() == null) { + throw new InvalidDataAccessApiUsageException("Procedure or Function name is required"); + } + if (compiled.compareAndSet(false, true)) { try { this.jdbcTemplate.afterPropertiesSet(); } @@ -296,7 +297,6 @@ public final synchronized void compile() throws InvalidDataAccessApiUsageExcepti throw new InvalidDataAccessApiUsageException(ex.getMessage()); } compileInternal(); - this.compiled = true; if (logger.isDebugEnabled()) { logger.debug("SqlCall for " + (isFunction() ? "function" : "procedure") + " [" + getProcedureName() + "] compiled"); @@ -341,7 +341,7 @@ protected void onCompileInternal() { * @return whether this operation is compiled and ready to use */ public boolean isCompiled() { - return this.compiled; + return this.compiled.get(); } /** diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java index 6b86dbb67390..2d214ad558aa 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java @@ -29,6 +29,7 @@ import java.util.Locale; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import javax.sql.DataSource; import org.apache.commons.logging.Log; @@ -82,7 +83,7 @@ public abstract class AbstractJdbcInsert { * Has this operation been compiled? Compilation means at least checking * that a DataSource or JdbcTemplate has been provided. */ - private volatile boolean compiled; + private final AtomicBoolean compiled = new AtomicBoolean(false); /** The generated string used for insert statement. */ private String insertString = ""; @@ -271,15 +272,15 @@ public boolean isQuoteIdentifiers() { * @throws InvalidDataAccessApiUsageException if the object hasn't been correctly initialized, * for example if no DataSource has been provided */ - public final synchronized void compile() throws InvalidDataAccessApiUsageException { - if (!isCompiled()) { - if (getTableName() == null) { - throw new InvalidDataAccessApiUsageException("Table name is required"); - } - if (isQuoteIdentifiers() && this.declaredColumns.isEmpty()) { - throw new InvalidDataAccessApiUsageException( - "Explicit column names must be provided when using quoted identifiers"); - } + public final void compile() throws InvalidDataAccessApiUsageException { + if (getTableName() == null) { + throw new InvalidDataAccessApiUsageException("Table name is required"); + } + if (isQuoteIdentifiers() && this.declaredColumns.isEmpty()) { + throw new InvalidDataAccessApiUsageException( + "Explicit column names must be provided when using quoted identifiers"); + } + if (this.compiled.compareAndSet(false, true)) { try { this.jdbcTemplate.afterPropertiesSet(); } @@ -287,7 +288,6 @@ public final synchronized void compile() throws InvalidDataAccessApiUsageExcepti throw new InvalidDataAccessApiUsageException(ex.getMessage()); } compileInternal(); - this.compiled = true; if (logger.isDebugEnabled()) { logger.debug("JdbcInsert for table [" + getTableName() + "] compiled"); } @@ -323,7 +323,7 @@ protected void onCompileInternal() { * @return whether this operation is compiled and ready to use */ public boolean isCompiled() { - return this.compiled; + return this.compiled.get(); } /**