Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6992211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:36:18+00:00 2026-05-27T19:36:18+00:00

I am planning to use EJBContext to pass some properties around from the application

  • 0

I am planning to use EJBContext to pass some properties around from the application tier (specifically, a message-driven bean) to a persistence lifecycle callback that cannot directly be injected or passed parameters (session listener in EclipseLink, entity lifecycle callback, etc.), and that callback is getting the EJBContext via JNDI.

This appears to work but are there any hidden gotchas, like thread safety or object lifespan that I’m missing? (Assume the property value being passed is immutable like String or Long.)

Sample bean code

@MessageDriven
public class MDB implements MessageListener {
   private @Resource MessageDrivenContext context;

   public void onMessage(Message m) { 
      context.getContextData().put("property", "value");
   }
}

Then the callback that consumes the EJBContext

public void callback() { 
   InitialContext ic = new InitialContext();
   EJBContext context = (EJBContext) ic.lookup("java:comp/EJBContext");
   String value = (String) context.getContextData().get("property");
} 

What I’m wondering is, can I be sure that the contextData map contents are only visible to the current invocation/thread? In other words, if two threads are running the callback method concurrently, and both look up an EJBContext from JNDI, they’re actually getting different contextData map contents?

And, how does that actually work – is the EJBContext returned from the JNDI lookup really a wrapper object around a ThreadLocal-like structure ultimately?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:36:18+00:00Added an answer on May 27, 2026 at 7:36 pm

    I think in general the contract of the method is to enable the communication between interceptors + webservice contexts and beans. So the context should be available to all code, as long as no new invocation context is created. As such it should be absolutely thread-safe.

    Section 12.6 of the EJB 3.1 spec says the following:

    The InvocationContext object provides metadata that enables
    interceptor methods to control the behavior of the invocation chain.
    The contextual data is not sharable across separate business method
    invocations or lifecycle callback events. If interceptors are invoked
    as a result of the invocation on a web service endpoint, the map
    returned by getContextData will be the JAX-WS MessageContext

    Furthermore, the getContextData method is described in 4.3.3:

    The getContextData method enables a business method, lifecycle callback method, or timeout method to retrieve any interceptor/webservices context associated with its invocation.

    In terms of actual implementation, JBoss AS does the following:

    public Map<String, Object> getContextData() {
        return CurrentInvocationContext.get().getContextData();
    }
    

    Where the CurrentInvocationContext uses a stack based on a thread-local linked list to pop and push the current invocation context.

    See org.jboss.ejb3.context.CurrentInvocationContext. The invocation context just lazily creates a simple HashMap, as is done in org.jboss.ejb3.interceptor.InvocationContextImpl

    Glassfish does something similar. It also gets an invocation, and does this from an invocation manager, which also uses a stack based on a thread-local array list to pop and push these invocation contexts again.

    The JavaDoc for the GlassFish implementation is especially interesting here:

    This TLS variable stores an ArrayList. The ArrayList contains
    ComponentInvocation objects which represent the stack of invocations
    on this thread. Accesses to the ArrayList dont need to be synchronized
    because each thread has its own ArrayList.

    Just as in JBoss AS, GlassFish too lazily creates a simple HashMap, in this case in com.sun.ejb.EjbInvocation. Interesting in the GlassFish case is that the webservice connection is easier to spot in the source.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was planning to use url routing for a Web Forms application. But, after
We're planning to use standard ASP.NET user authentication for our application. However, by default
I am planning to use SCTP for a embedded Linux project. Anybody has some
im planning to use java/jsp for web application installed on virtual web hosting space
I am planning to use facebook authentication for my application. I thought of using
I am planning to use coverflow control in my Silverlight application, but I am
I am planning to use Snowball stemming solution in one of my commercial application.There
I am planning to use Ext JS for a large application. The application's features
I am planning to use ShellExecute for the updater of an application written in
Was planning to use Service Routing (on WCF/REST) to do some common tasks before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.