adrianp
adrianp

Reputation: 2551

Returning a Stateful Java Bean from a Stateless Bean?

I want to handle a login scenario as follows:

  1. Client connects to a Stateless Java Bean (SLJB) and tries to login;
  2. If login succeeds, the SLJB returns to the user a Stateful Java Bean (SFJB), so that the client can continue using the application.

I am currently doing the second step as:

return new StatefulBean(some params);

Is this the right way to do it? It does not seem to me as I get the exception:

Class org.eclipse.persistence.internal.jpa.EntityManagerImpl is not Serializable

when running my application, and I think it is related to the described method.

What would be the correct way to return a reference to the SFJB from the SLJB to the client?

Upvotes: 1

Views: 243

Answers (2)

Mike Braun
Mike Braun

Reputation: 3769

As Tomasz mentions, you probably need to rethink your flow.

That said, you can get a hold of a new stateful instance by doing a JNDI lookup, using the portable JNDI name that us assigned to each bean at startup.

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

First of all, this is completely wrong:

new StatefulBean(some params)

EJB container is responsible for creating and destroying instances of beans, you should never create them manually.

In your scenario I would reverse the flow: the client connects to the stateful bean which might stateless session bean as a helper. No need to pass beans around, client always uses the same bean.

Upvotes: 3

Related Questions