Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

jakarta ee - @Inject stateless EJB contains data from previous request

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...