From 66b9eab2c02a6720116cf23a0f7f7babf164c825 Mon Sep 17 00:00:00 2001 From: Henning Rohlfs Date: Fri, 25 Sep 2020 16:58:55 +0200 Subject: [PATCH] DATACMNS-1806 - Prevent returning null from Lazy due to concurrency problem. When accessed concurrently from multiple threads, Lazy.getNullable() could return null unexpectedly. Original pull request: #468. --- src/main/java/org/springframework/data/util/Lazy.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/data/util/Lazy.java b/src/main/java/org/springframework/data/util/Lazy.java index 75dda3485..2edcd8784 100644 --- a/src/main/java/org/springframework/data/util/Lazy.java +++ b/src/main/java/org/springframework/data/util/Lazy.java @@ -34,6 +34,7 @@ import org.springframework.util.Assert; * * @author Oliver Gierke * @author Mark Paluch + * @author Henning Rohlfs * @since 2.0 */ @RequiredArgsConstructor @@ -45,7 +46,7 @@ public class Lazy implements Supplier { private final Supplier supplier; private @Nullable T value = null; - private boolean resolved = false; + private volatile boolean resolved = false; /** * Creates a new {@link Lazy} to produce an object lazily. @@ -203,15 +204,11 @@ public class Lazy implements Supplier { @Nullable public T getNullable() { - T value = this.value; - - if (this.resolved) { + if (resolved) { return value; } - value = supplier.get(); - - this.value = value; + this.value = supplier.get(); this.resolved = true; return value;