0

JNDI 树中的对象是线程安全的吗?

例如,假设我做了这样的事情:

伪代码:

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

现在有可能第一个线程检查 someValue,发现它是空的,然后去为它设置一个值,但是此时另一个线程进来并检查它也发现值是空的,所以它设置自己的值所以第一个线程将值绑定到它的名称,然后第二个线程重新绑定到它自己的名称覆盖第一个?

或者有什么方法可以使这个线程安全?

4

1 回答 1

0

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.

于 2013-08-14T21:37:13.517 回答