What happens when I inject a Stateless Bean into a Stateful Bean?

265 views Asked by At

I work on a large monolithic Java web system, built with JSF 1.1 and Seam Framework, running on a jBoss Server.

I have a JSF Backing Bean (Stateful by default in JSF) that injects a Stateless Bean used as a service (and annoted with @Stateless). The Stateless Service has a instance member variable declared as a List and increased by a method.

Issue: After updating jBoss version from 5.1 EAP to 7.0, the mentioned variable (list) is taking values from other requests. This creates a bug in the system. But, the variable is inside a stateless service. Why is this happening? What should be happening in this situation?

ps: I have so many things that can be used to solve the issue, but I need to understand what is happening.

Edit: Pproblem solved, i was working with jBoss 5 and Seam Framework by several years. I was confused by a wrong jBoss 5 comportament. After read the definition to a Steteless Bean, i understand very well what are hepenning with my code: [Java EE 1.4 Tutorial1

After update to jBoss 7, the pool of stateless beans work as expected and the system code gives a bug because a bad implementation of my team (stateless beans with so many global variables). When tho jBoss pool started works, the values started to be shared with others requests.

1

There are 1 answers

7
Julien Aubin On

This is because your stateless instance is shared among several stateful instances.

Member variables of stateless beans almost behave as global variables at runtime. So : - if your list depends on a state, put it in a stateful bean - otherwise keep it as is but with a purge mechanism - the third way to be "stateful in stateless" but only in web servers consists in using ThreadLocal variables