Skip to content

Commit 807c3ed

Browse files
committed
Using non-blocking optimization to compile function.
1 parent adb64c7 commit 807c3ed

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Map;
2323
import java.util.Set;
2424

25+
import java.util.concurrent.atomic.AtomicBoolean;
2526
import javax.sql.DataSource;
2627

2728
import org.apache.commons.logging.Log;
@@ -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
@Nullable
@@ -284,19 +285,18 @@ public void addDeclaredRowMapper(String parameterName, RowMapper<?> rowMapper) {
284285
* @throws org.springframework.dao.InvalidDataAccessApiUsageException if the object hasn't
285286
* been correctly initialized, for example if no DataSource has been provided
286287
*/
287-
public final synchronized void compile() throws InvalidDataAccessApiUsageException {
288-
if (!isCompiled()) {
289-
if (getProcedureName() == null) {
290-
throw new InvalidDataAccessApiUsageException("Procedure or Function name is required");
291-
}
288+
public final void compile() throws InvalidDataAccessApiUsageException {
289+
if (getProcedureName() == null) {
290+
throw new InvalidDataAccessApiUsageException("Procedure or Function name is required");
291+
}
292+
if (compiled.compareAndSet(false, true)) {
292293
try {
293294
this.jdbcTemplate.afterPropertiesSet();
294295
}
295296
catch (IllegalArgumentException ex) {
296297
throw new InvalidDataAccessApiUsageException(ex.getMessage());
297298
}
298299
compileInternal();
299-
this.compiled = true;
300300
if (logger.isDebugEnabled()) {
301301
logger.debug("SqlCall for " + (isFunction() ? "function" : "procedure") +
302302
" [" + getProcedureName() + "] compiled");
@@ -341,7 +341,7 @@ protected void onCompileInternal() {
341341
* @return whether this operation is compiled and ready to use
342342
*/
343343
public boolean isCompiled() {
344-
return this.compiled;
344+
return this.compiled.get();
345345
}
346346

347347
/**

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Locale;
3030
import java.util.Map;
3131

32+
import java.util.concurrent.atomic.AtomicBoolean;
3233
import javax.sql.DataSource;
3334

3435
import org.apache.commons.logging.Log;
@@ -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 = "";
@@ -271,23 +272,22 @@ public boolean isQuoteIdentifiers() {
271272
* @throws InvalidDataAccessApiUsageException if the object hasn't been correctly initialized,
272273
* for example if no DataSource has been provided
273274
*/
274-
public final synchronized void compile() throws InvalidDataAccessApiUsageException {
275-
if (!isCompiled()) {
276-
if (getTableName() == null) {
277-
throw new InvalidDataAccessApiUsageException("Table name is required");
278-
}
279-
if (isQuoteIdentifiers() && this.declaredColumns.isEmpty()) {
280-
throw new InvalidDataAccessApiUsageException(
281-
"Explicit column names must be provided when using quoted identifiers");
282-
}
275+
public final void compile() throws InvalidDataAccessApiUsageException {
276+
if (getTableName() == null) {
277+
throw new InvalidDataAccessApiUsageException("Table name is required");
278+
}
279+
if (isQuoteIdentifiers() && this.declaredColumns.isEmpty()) {
280+
throw new InvalidDataAccessApiUsageException(
281+
"Explicit column names must be provided when using quoted identifiers");
282+
}
283+
if (this.compiled.compareAndSet(false, true)) {
283284
try {
284285
this.jdbcTemplate.afterPropertiesSet();
285286
}
286287
catch (IllegalArgumentException ex) {
287288
throw new InvalidDataAccessApiUsageException(ex.getMessage());
288289
}
289290
compileInternal();
290-
this.compiled = true;
291291
if (logger.isDebugEnabled()) {
292292
logger.debug("JdbcInsert for table [" + getTableName() + "] compiled");
293293
}
@@ -323,7 +323,7 @@ protected void onCompileInternal() {
323323
* @return whether this operation is compiled and ready to use
324324
*/
325325
public boolean isCompiled() {
326-
return this.compiled;
326+
return this.compiled.get();
327327
}
328328

329329
/**

0 commit comments

Comments
 (0)