EJB : Singleton session bean or more like applicaton bean

205 views Asked by At

I'm running through again the Javaee7 oracle documentation and here's what's stated.

"A singleton session bean is instantiated once per application and exists for the lifecycle of the application. Singleton session beans are designed for circumstances in which a single enterprise bean instance is shared across and concurrently accessed by clients."

I do understand perfectly what it is saying. However come to think of it, the word session is very misleading here. Since it exists at application level, the term 'session' doesn't seem to apply here.

When i think of the word 'session', i think in terms of each individual user as a session. If that singleton session bean is for across application, shouldn't it not be called a session bean (instead probably a application bean would be better understood).

Any opinion?

2

There are 2 answers

0
Brett Kail On
  • Stateless - Session lasts for a single client operation
  • Stateful - Session lasts across operations for a client
  • Singleton - Session lasts across clients an application
5
Gabriel Aramburu On

When i think of the word 'session', i think in terms of each individual user as a session

The term "session" in this context means unit of work or business transaction.

In Stateless and Singleton beans, a new session/business transaction is opens when a new request arrives, and it lives until the response it sent back to the client. (session-per-request pattern)

For Stateful Session bean the business transaction could implies several client requests. From the Stateful bean's perspective, a client is a proxy that sends the requests to the same stateful instance.

Edit (too long for comment)

I think Application Bean could probably be a good name, in fact they are good for storing application settings, but the key point is why they are called Session Bean.

In this context "session" is the period of time the business transaction executes. For Stateless and Singleton this period matches with the request/response cycle.

...If that singleton session bean is for across application, shouldn't it not be called a session bean

The fact that a Singleton maintains its state between client invocations doesn't means that the session-per-request model doesn't applies. Every Singleton reference used in your application is a client, when a client makes a request a new session is created.