1

I have a situation where I need to load a list of providers only once . So I have used synchronized block . But it fails in multiclustered environment in weblogic . Is there any way to handle it . This snippet of code works fine in single cluster .

public class AdditionalInfoImplProvider
{
    private volatile boolean isLoaded = false;

    private void ensureProviderLoaded()
    {
        if (!isLoaded) {
            synchronized (this) {
                if (!isLoaded) {
                    // Load Provider
                    isLoaded = true;
                }
            }
        }
    }
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146

2 Answers2

0

Java synchronized keyword is a single jvm concept. In a cluster, there are multiple JVMs so all these will synchronize on different AdditionalInfoImplProvider instances and your logic will execute in all Weblogic instances.

As far as I know, this kind of synchronization is not provided in standard JDK.

Singleton design pattern will also not work here because traditionally Singleton means Singleton per JVM & not per cluster.

Primitive way to handle this scenario is to have something external to your application like a lock file or an entry in RDBMS & whosoever gets hold of it first - do the loading & rest simply exit.

You can also have a look if Weblogic provides some kind of listeners on application events - not specific to instances of cluster.

I haven't used Zookeeper but heard people using it for your use case.

Refer this question for broader discussion on the topic.

Summary is , you definitely need something that exists application wide and per JVM constructs wouldn't be sufficient.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
0

WebLogic has a specific interface you should implement - https://docs.oracle.com/cd/E68505_01/wls/WLAPI/weblogic/cluster/singleton/SingletonService.html - when you run the "activate" method this should throw an exception when another instance is already active in your WebLogic cluster.

b0tting
  • 577
  • 2
  • 7