@Inject stateless EJB contains data from previous request

余生长醉 提交于 2019-11-28 12:08:46

问题


I have a JAX-RS webservice with a resource for generating testdata. During tests I found out, that the injected EJB is not reinitialized and still contains data from the last request.

I have a jar file server.jar containing my business logic with EJBs. To show my problem I have created a stateless bean:

@Stateless
public class TestService
{
    @EJB
    SubsequentTestService state2Service;

    private String value;

    public void testIt()
    {
        System.out.println("####### VALUE: " + value);
        value = "TestValue";

        state2Service.testIt();
    }
}

I am using the subsequent call to SubsequentTestService to show the odd behaviour also exists for call of another stateless EJB:

@Stateless
public class SubsequentTestService
{
    private String value;

    public void testIt()
    {
        System.out.println("####### VALUE2: " + value);
        value = "TestValue2";
    }
}

Changing the annotation form @EJB to @Inject does not change anything.

In my web.war I have simple JAX-RS beans. The one which is called to show the strange behaviour is defined as follows:

@Path("/test")
public class TestResource
{
    @Inject
    TestService testService;

    @GET
    @Path("/state")
    public void testState()
    {
        testService.testIt();
    }
}

The JAX-RS application configuration looks as follows:

@ApplicationPath("/api")
public class JaxRsConfiguration extends Application
{
}

The war file contains the beans.xml, but no other configuration file. Everything is packaged into an ear file and is deployed in wildfly 10.0.0.Final. If I call the webservice as GET request via http://localhost:8080/api/test/state I get the expected output:

INFO [stdout] (default task-7) ####### VALUE: null
INFO [stdout] (default task-7) ####### VALUE2: null

But on the second request I get following unexpected output:

INFO [stdout] (default task-8) ####### VALUE: TestValue
INFO [stdout] (default task-8) ####### VALUE2: TestValue2

What is my problem here? Might be anything misconfigured in the wildfly? But I have only changed the logging and the datasource definition.


回答1:


You have the meaning of @Stateless backwards.

This does not mean like so:

Hey container, here's an arbitrary class, please make it a stateless bean.

This actually means like so:

Hey container, here's a stateless class, you can safely use it as a stateless bean.

You have a stateful class. You should mark it as a @Stateful bean. Otherwise, get rid of all the state (unmanaged instance variables) so you can safely use it as a @Stateless bean.

See also:

  • Why Stateless session beans?
  • JSF request scoped bean keeps recreating new Stateful session beans on every request?
  • When using @EJB, does each managed bean get its own @EJB instance?


来源:https://stackoverflow.com/questions/36112510/inject-stateless-ejb-contains-data-from-previous-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!