6

It's impossible to use static variables on a session bean code. Is this restriction arbitrary or fundamented? And why?

Best regards

thathashd
  • 1,022
  • 4
  • 17
  • 49

3 Answers3

16

As stated in the FAQ on EJB restrictions, one of the restrictions for using EJBs is:

enterprise beans should not read or write nonfinal static fields

Further expanded in the discussion on static fields:

Nonfinal static class fields are disallowed in EJBs because such fields make an enterprise bean difficult or impossible to distribute. Static class fields are shared among all instances of a particular class, but only within a single Java Virtual Machine (JVM). Updating a static class field implies an intent to share the field's value among all instances of the class. But if a class is running in several JVMs simultaneously, only those instances running in the same JVM as the updating instance will have access to the new value. In other words, a nonfinal static class field will behave differently if running in a single JVM, than it will running in multiple JVMs. The EJB container reserves the option of distributing enterprise beans across multiple JVMs (running on the same server, or on any of a cluster of servers). Nonfinal static class fields are disallowed because enterprise bean instances will behave differently depending on whether or not they are distributed.

It is acceptable practice to use static class fields if those fields are marked as final. Since final fields cannot be updated, instances of the enterprise bean can be distributed by the container without concern for those fields' values becoming unsynchronized.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Nice answer. Unfortunately the links are broken. – Algiz Dec 23 '14 at 15:29
  • And what about static final Collection? Are they also allowed? – botchniaque Apr 27 '15 at 13:06
  • @botchniaque that's worse, unless you make it unmodifiable, a collection can be changed by all threads leading to further problems – Óscar López Apr 27 '15 at 14:41
  • 2
    That's what I thought, why then EJB specs mentions restrictions for `nonfinal static fields`, rather than for `nonfinal static primitives` **AND** `any static complex types`? It's a bit misleading. – botchniaque Apr 28 '15 at 13:46
5

It is fundamental. As per this sun documenation,

Nonfinal static class fields are disallowed in EJBs because such fields make an enterprise bean difficult or impossible to distribute. Static class fields are shared among all instances of a particular class, but only within a single Java Virtual Machine (JVM). *

kosa
  • 65,990
  • 13
  • 130
  • 167
  • In a stateless session bean, if my code calls Resources.getSingleton(), and that returns a static Resources object, is that also a no-no? – djb Jun 20 '13 at 11:40
1

static means unique for a class OR for all it's objects.

Now, javabeans are supposed to have user-specific data, static fields don't make any sense for these.

One user edits a variable, ant it'll be updated for all other users too. (at free of cost :-)).

However if you want static behaviour for these (i.e. using same data for all users), you have application for that purpose.

Azodious
  • 13,752
  • 1
  • 36
  • 71