|
29 | 29 | import java.util.Locale;
|
30 | 30 | import java.util.Map;
|
31 | 31 |
|
| 32 | +import java.util.concurrent.atomic.AtomicBoolean; |
32 | 33 | import javax.sql.DataSource;
|
33 | 34 |
|
34 | 35 | import org.apache.commons.logging.Log;
|
@@ -82,7 +83,7 @@ public abstract class AbstractJdbcInsert {
|
82 | 83 | * Has this operation been compiled? Compilation means at least checking
|
83 | 84 | * that a DataSource or JdbcTemplate has been provided.
|
84 | 85 | */
|
85 |
| - private volatile boolean compiled; |
| 86 | + private final AtomicBoolean compiled = new AtomicBoolean(false); |
86 | 87 |
|
87 | 88 | /** The generated string used for insert statement. */
|
88 | 89 | private String insertString = "";
|
@@ -271,23 +272,22 @@ public boolean isQuoteIdentifiers() {
|
271 | 272 | * @throws InvalidDataAccessApiUsageException if the object hasn't been correctly initialized,
|
272 | 273 | * for example if no DataSource has been provided
|
273 | 274 | */
|
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)) { |
283 | 284 | try {
|
284 | 285 | this.jdbcTemplate.afterPropertiesSet();
|
285 | 286 | }
|
286 | 287 | catch (IllegalArgumentException ex) {
|
287 | 288 | throw new InvalidDataAccessApiUsageException(ex.getMessage());
|
288 | 289 | }
|
289 | 290 | compileInternal();
|
290 |
| - this.compiled = true; |
291 | 291 | if (logger.isDebugEnabled()) {
|
292 | 292 | logger.debug("JdbcInsert for table [" + getTableName() + "] compiled");
|
293 | 293 | }
|
@@ -323,7 +323,7 @@ protected void onCompileInternal() {
|
323 | 323 | * @return whether this operation is compiled and ready to use
|
324 | 324 | */
|
325 | 325 | public boolean isCompiled() {
|
326 |
| - return this.compiled; |
| 326 | + return this.compiled.get(); |
327 | 327 | }
|
328 | 328 |
|
329 | 329 | /**
|
|
0 commit comments