views:

250

answers:

2

Hello All, I'm struggling futher in my adventure with Jboss and EJB 3.0. I joined Spring 3.0 with EJB. I have 3 modules - web module with Spring controllers, ejb module with my EJB beans and spring mogule with other classes like dao or some helpers. All in the ear.

So, in my Spring Controller I have the following code:

@Controller
public class IndexController {

    @EJB
    PaymentRemote paymentRemote;

    @RequestMapping(value = "/index")
    public ModelAndView index() throws Exception {
        ModelAndView mav = new ModelAndView("index/index");     

        paymentRemote.savePayment(123, "bla222");
        paymentRemote.sayGoodbye();
           return mav;
}

In my EJB module I have the following stateful bean:

@Stateful
@Interceptors( SpringBeanAutowiringInterceptor.class)
public class PaymentRemoteImpl implements PaymentRemote{

    @Autowired
    ExampleService exampleService;

    public void savePayment(long payment, String name) throws Exception {
        exampleService.savePayment(123, "kk");

    }

    @Remove
    public void sayGoodbye() {
        System.out.println("I want to finish my task here!");

    }
}

Every dependency is injected properly. When I test this code with stateless beans it works just fine. When it comes to stateful beans when I call my method sayGoodbye() I can't call again this bean. I get the exception:

javax.ejb.NoSuchEJBException: Could not find stateful bean: a74a2l-n1u5tx-gcwd0e6a-1-gcwd18fo-9h

I don't understand this situation :/ I asked the container to remove the bean but later on I would like to use it again but it wants to find me again the same bean. I thought that although I asked to remove it it will create on my request again. Could anyone kindly help me with my problem? I'm stucked.

+2  A: 

You can't use injection of Stateful Session Beans (SFSB) into a multi-threaded component that may be shared by multiple concurrent clients like a Servlet or a Controller. Perform a JNDI lookup instead and put the bean into the HttpSession.

See also

Pascal Thivent
You can't have concurrent access on the SFSB no matter what, even if it's stored in the session. Either synchronize the servlets or wrap the bean to synchronize the methods: http://stackoverflow.com/questions/1935178/correct-usage-of-stateful-beans-with-servlets/1935476#1935476
ewernli
This goes way beyond the issue I wanted to address in this particular question (which is that you can't use injection with SFSB).
Pascal Thivent
A: 

Hello, Good point! I'm new in Jboss and ejb world and sometimes I find different obstacles. With your answer it seems to be so obvious. I will try your solution!

Agata