|
28 | 28 | import java.util.List;
|
29 | 29 | import java.util.Locale;
|
30 | 30 | import java.util.Map;
|
| 31 | +import java.util.concurrent.atomic.AtomicBoolean; |
31 | 32 |
|
32 | 33 | import javax.sql.DataSource;
|
33 | 34 |
|
@@ -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 = "";
|
@@ -268,23 +269,22 @@ public boolean isQuoteIdentifiers() {
|
268 | 269 | * @throws InvalidDataAccessApiUsageException if the object hasn't been correctly initialized,
|
269 | 270 | * for example if no DataSource has been provided
|
270 | 271 | */
|
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)) { |
280 | 281 | try {
|
281 | 282 | this.jdbcTemplate.afterPropertiesSet();
|
282 | 283 | }
|
283 | 284 | catch (IllegalArgumentException ex) {
|
284 | 285 | throw new InvalidDataAccessApiUsageException(ex.getMessage());
|
285 | 286 | }
|
286 | 287 | compileInternal();
|
287 |
| - this.compiled = true; |
288 | 288 | if (logger.isDebugEnabled()) {
|
289 | 289 | logger.debug("JdbcInsert for table [" + getTableName() + "] compiled");
|
290 | 290 | }
|
@@ -320,7 +320,7 @@ protected void onCompileInternal() {
|
320 | 320 | * @return whether this operation is compiled and ready to use
|
321 | 321 | */
|
322 | 322 | public boolean isCompiled() {
|
323 |
| - return this.compiled; |
| 323 | + return this.compiled.get(); |
324 | 324 | }
|
325 | 325 |
|
326 | 326 | /**
|
|
0 commit comments