You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Martin Vysny <mv...@whitestein.com> on 2008/11/20 18:17:07 UTC

Get current SessionContext

Hi guys,
  perhaps a strange question: is there an OpenEJB-specific way to detect
if my code is currently being executed in a stateless session bean
context (and get its SessionContext)? In pure J2EE this is not possible
(unless you remember the SessionContext and store it into let's say
ThreadLocal), however, I cannot do that.
Thanks!
Martin


Re: Get current SessionContext

Posted by Dain Sundstrom <da...@iq80.com>.
On Nov 21, 2008, at 7:27 AM, Martin Vysny wrote:

> On Thu, 2008-11-20 at 18:17 +0100, Martin Vysny wrote:
>> Hi guys,
>>  perhaps a strange question: is there an OpenEJB-specific way to  
>> detect
>> if my code is currently being executed in a stateless session bean
>> context (and get its SessionContext)? In pure J2EE this is not  
>> possible
>> (unless you remember the SessionContext and store it into let's say
>> ThreadLocal), however, I cannot do that.
>> Thanks!
>> Martin
>
> Okay, seems that the following snippet worked:
>
> final Method m =  
> Class.forName("org.apache.openejb.core.ThreadContext",
> true,
> Thread 
> .currentThread 
> ().getContextClassLoader()).getMethod("getThreadContext", (Class<? 
> >[]) null);
> final Object result = m.invoke(null, (Object[]) null);
> final boolean isInBean = result != null;

Um, just because you are using reflection instead of a direct thread  
local, doesn't make it legal.  I'd just use a thread local directly,  
because there are so may libraries out there that use them it is  
likely the code will work in any container.  Just be sure to clear the  
thread local in a finally block.

-dain


Re: Get current SessionContext

Posted by Martin Vysny <mv...@whitestein.com>.
On Thu, 2008-11-20 at 18:17 +0100, Martin Vysny wrote:
> Hi guys,
>   perhaps a strange question: is there an OpenEJB-specific way to detect
> if my code is currently being executed in a stateless session bean
> context (and get its SessionContext)? In pure J2EE this is not possible
> (unless you remember the SessionContext and store it into let's say
> ThreadLocal), however, I cannot do that.
> Thanks!
> Martin

Okay, seems that the following snippet worked:

final Method m = Class.forName("org.apache.openejb.core.ThreadContext",
true,
Thread.currentThread().getContextClassLoader()).getMethod("getThreadContext", (Class<?>[]) null);
final Object result = m.invoke(null, (Object[]) null);
final boolean isInBean = result != null;