简体   繁体   中英

Is the WebLogic JNDI tree thread safe?

Will objects in the JNDI tree be thread-safe?

Let's say for example I did something like this:

pseudo-code:

String value = null;
try {
  value = context.lookup("someValue")
} catch (Exception ignored) {}
if (value == null) {
  value = "My name is "+currentThread.getName()
  context.bind("someValue", value);
}

Now is it possible that the first thread checks on someValue, finds it empty and then goes to set a value to it but then right at this moment another thread comes in and checks and it too finds the value empty so it sets its own value so first thread binds the value to its name and the second one then rebinds to its own name overwriting the first one?

Or is there some way to make this thread-safe?

When you bind a custom object (a non-RMI object) into a JNDI tree in a WebLogic Server cluster, the object is replicated across all the servers in the cluster. However, if the host server goes down, the custom object is removed from the cluster's JNDI tree. Custom objects are not replicated unless the custom object is bound again. You need to unbind and rebind a custom object every time you want to propagate changes made to the custom object.

Unbinding and rebinding are expensive (slow) operations.

See more info here: http://docs.oracle.com/cd/E13222_01/wls/docs81/jndi/jndi.html#475689

There is also a ton of info there about thread safety with contexts. You will want to look into the exactly-once-per-cluster design pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM