From 130c1667a8c6738ca2ae2d01bcec69939417f752 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sun, 31 Mar 2024 18:17:49 -0700 Subject: [PATCH] Merge #184 manually (fails clean merge due to refactor b/w 2.x and 3.0) (#185) --- .../jaxrs/cfg/MapperConfiguratorBase.java | 25 ++++++++++++++----- release-notes/CREDITS-2.x | 2 ++ release-notes/VERSION-2.x | 3 ++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/base/src/main/java/tools/jackson/jaxrs/cfg/MapperConfiguratorBase.java b/base/src/main/java/tools/jackson/jaxrs/cfg/MapperConfiguratorBase.java index e36c7498..26318223 100644 --- a/base/src/main/java/tools/jackson/jaxrs/cfg/MapperConfiguratorBase.java +++ b/base/src/main/java/tools/jackson/jaxrs/cfg/MapperConfiguratorBase.java @@ -2,6 +2,7 @@ import java.util.EnumMap; import java.util.Map; +import java.util.concurrent.locks.ReentrantLock; import tools.jackson.databind.*; import tools.jackson.databind.cfg.MapperBuilder; @@ -17,6 +18,8 @@ public abstract class MapperConfiguratorBase { + private final ReentrantLock _lock = new ReentrantLock(); + /* /********************************************************************** /* Configuration, simple features @@ -87,9 +90,14 @@ public MapperConfiguratorBase(MAPPER mapper, _instropectorOverride = instropectorOverride; } - public synchronized MAPPER getDefaultMapper() { + public MAPPER getDefaultMapper() { if (_defaultMapper == null) { - _defaultMapper = _mapperWithConfiguration(mapperBuilder()); + _lock.lock(); + try { + _defaultMapper = _mapperWithConfiguration(mapperBuilder()); + } finally { + _lock.unlock(); + } } return _defaultMapper; } @@ -102,7 +110,12 @@ public synchronized MAPPER getDefaultMapper() { protected MAPPER mapper() { if (_mapper == null) { - _mapper = _mapperWithConfiguration(mapperBuilder()); + _lock.lock(); + try { + _mapper = _mapperWithConfiguration(mapperBuilder()); + } finally { + _lock.unlock(); + } } return _mapper; } @@ -127,17 +140,17 @@ protected MAPPER mapper() /** * Method that locates, configures and returns {@link ObjectMapper} to use */ - public synchronized MAPPER getConfiguredMapper() { + public MAPPER getConfiguredMapper() { // important: should NOT call mapper(); needs to return null // if no instance has been passed or constructed return _mapper; } - public synchronized final void setMapper(MAPPER m) { + public final void setMapper(MAPPER m) { _mapper = m; } - public synchronized final void setAnnotationIntrospector(AnnotationIntrospector aiOverride) { + public final void setAnnotationIntrospector(AnnotationIntrospector aiOverride) { _instropectorOverride = aiOverride; } diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index e697345d..b0168b67 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -91,6 +91,8 @@ PJ Fanning (@pjfanning) * Contributed #166: `ProviderBase` class shows contention on synchronized block using `LRUMap` _writers instance (2.14.2) +* Contributed #184: Use `ReentrantLock`s instead of synchronized blocks + (2.18.0) Steven Schlansker (@stevenschlansker) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 985282c9..2c06ff18 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -12,7 +12,8 @@ Sub-modules: 2.18.0 (not yet released) -No changes since 2.17 +#184: Use `ReentrantLock`s instead of synchronized blocks + (contributed by @pjfanning) 2.17.0 (12-Mar-2024)