Results 1 to 8 of 8

Thread: Problem using EJBs and ThreadLocal variables with spring

  1. #1
    Join Date
    Sep 2005
    Posts
    3

    Default Problem using EJBs and ThreadLocal variables with spring

    Hi Spring gurus,

    I'm developing a web application for a customer and I decided to use spring for the first time. All the things I've seen are really cool but I've got a big problem that I've to solve very very quickly because is stopping me and I've timing constraints with the customer that I've to respect.

    Basically my application uses org.springframework.ejb.access.LocalStatelessSessi onProxyFactoryBean to to let the front-end objects access a set of stateless session beans that contains my business methods. My stateless session beans does have only LOCAL interfaces. An excerpt of configuration of that kind of bean inside the spring xml configuration file follows below.

    <!-- AddressBookManagerBean -->
    <bean id="addressbookManagerBean" class="org.springframework.ejb.access.LocalStatele ssSessionProxyFactoryBean">
    <property name="jndiName">
    <value>AddressBookManagerBeanLocal</value>
    </property>
    <property name="resourceRef">
    <value>false</value>
    </property>
    <property name="businessInterface">
    <value>it.kausmedia.svctier.managers.addressbook.I AddressBookManager</value>
    </property>
    </bean>

    When the user requires a page from my application a sort of context variable is put in a ThreadLocal variable so that it can be seen from anywhere inside my application very easily . My problem is that the ThreadLocal variable I previously set (and I verified that is set) is correctly seen in every part of my application except inside my session beans. Infact when I access the variable inside my beans's business methods it returns null, but the variable has been set. To test that the variable is set I've tried to put the call to the ThreadLocal.get() for my context in the web tier, just before the call to my EJB's business method, and inside the business method I'm going to call in the session bean. The call in the web-tier gives me correctly the object but, following, the call inside the business tier gives me null :shock::shock::shock::shock:.

    Because of the web tier accesses the EJBs through local interfaces the mechanism of accessing ThreadLocal variables has to work. Maybe the Spring's LocalStatelessSessionProxyFactoryBean is introducing some cavets that I'm not able to see and that is scrambling all the things?? Can someone help me in clearing this issue? If so can you suggest me a different way of doing this same thing?

    Please help me :cry: it's really urgent.I'll appreciate a lot any help or suggestion given.

    Kind regards to everyone.

    Sergio

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Have you checked, if the problem does occur when you access your SessionBean by conventional means (i.e. without Spring)?

    For remote beans I know that threading is being controlled by the container. I'm not sure with local beans, but I suspect it might be like that as well. So you are in fact in another thread and cannot access your thread-local variable.

    So either you have to pass your parameter as argument or might try to skip usage of session beans in the first place.

    Regards,
    Andreas

  3. #3
    Join Date
    Sep 2005
    Posts
    3

    Default

    Quote Originally Posted by Andreas Senft
    Have you checked, if the problem does occur when you access your SessionBean by conventional means (i.e. without Spring)?
    Andreas, first of all thanks for you kind help. The things works if I access the bean by conventional ways.

    Quote Originally Posted by Andreas Senft
    For remote beans I know that threading is being controlled by the container. I'm not sure with local beans, but I suspect it might be like that as well. So you are in fact in another thread and cannot access your thread-local variable.
    Yes you're right and I know that. But I'm accessing the bean through local interfaces and not remote so everything has to be in the context of the same thread (this is what I've studied in my books). Effectively I've used org.springframework.ejb.access.LocalStatelessSessi onProxyFactoryBean because it's the way normally used to access the bean using local interfaces as states in the PRO Spring book (so without using RMI as for Remote interfaces)
    The real problem, in my opinion, is how the org.springframework.ejb.access.LocalStatelessSessi onProxyFactoryBean instantiates the bean in the business tier.

    Kind regards

    Sergio

  4. #4
    Join Date
    Sep 2005
    Posts
    3

    Default

    Pls guy no one has any idea about that? I'm blocked by this issue.

    PLS Help me.

    Thanks

    Sergio

  5. #5
    Join Date
    Dec 2004
    Location
    Bucuresti, Romania
    Posts
    72

    Default troubleshoting suggestion

    Maybe your thread local variables don't work because they are actually different threads. Try to log Thread.currentThread() inside and outside the session bean.

  6. #6
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Spring isn't doing anything strange with the thread, but your server might be. (But that would surprise me.) Check the thread is the same. I once did something similar that worked, in Orion and WebLogic 7/8.1.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  7. #7
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Also, local EJBs typically don't add much value in Spring applications. The most valuable service they provide is CMT, and Spring's declarative transaction management is more powerful and POJO-based. So you could consider replacing the EJB with a Spring-managed local object.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  8. #8
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    25

    Default

    It depends on the actual implementation of the appserver. Obviously EJB specs did not specify on this, although most appserver won't do a thread switch anyway.

    I have tested this on WSAD 5 and it works OK.

    However, I do not want to take the risk using it in production environment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •