Should I cache an instance of InitialContext?

I am using InitialContext in my application to search for remote EJBs. There are some external systems that notify me of some events, and when that happens, I will delegate this notification to the correct remote EJB.

I always thought that I should create a new InitialContext for each group of searches (and maybe even close the initial context after them). In the above case, this is one instance of InitialContext for each search. Although some members of my team are not sure.

So the question is: Should I cache an instance of InitialContext? If so, how long should I keep this instance alive and why?

+4
source share
2 answers

Keep in mind that the InitialContext instance is not synchronized, so caching can be dangerous if one instance is accessed by parallel threads.

You probably already know this, but just in case, the usual practice that effectively improves the performance of the process of calling remote methods is to cache the ejb link caused by the search operation.

+3
source

Since your IntialContext is nothing more than a reference to the starting point in the namespace, it does not matter whether you will do a new one each time or somewhere to store this link.

, , , , , , .

: JNDI- Java

+2

Source: https://habr.com/ru/post/1546962/


All Articles