Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ApplicationScoped must be Serializable? [duplicate]

I am now learning about CDI scope in Java EE 7 tutorial and finding that in the last paragraph it says

Beans that use session, application, or conversation scope must be serializable, but beans that use request scope do not have to be serializable.

But what confused me a lot is that in my IDE (Netbeans and IntelliJ Idea), when I use @SessionScoped or @ConversationScoped, it does give me an error if I am not implementing the Serializable just like what the Java EE 7 tutorial has said, and obviously, I can't build the project then run it. Things get strange when I use @ApplicationScoped but not implementing the Serializable, no errors come out and I can build then run the application normally.

So I'm very curious about that and really want to know why. Could you please explain what happened there? Thank you so much!

like image 661
MarcuX Avatar asked Sep 06 '25 22:09

MarcuX


2 Answers

The errors in your IDE are showed basically because your IDE has some plugin for this (which is not to be trusted 100% btw).

The reasons for serialization are as follows:

  • @SessionScoped beans

    • These are handled by more than just CDI spec and other specs have requirements on them
    • Namely, container can choose to store the passivate the session on order to preserve resources
    • Another story is replication between servers so your requests can be handled on several nodes (failover scenario etc.)
    • We don't know why but because of this we must make sure such beans are always serializable
  • @ConversationScoped beans

    • Pretty much the same story, not that other specs would have requirements on them but these bean 'live' within a scope of a session and can live as long as the session (if not ended sooner)
    • For these reasons, when a server passivates/replicates a session along with session scoped beans, it of course has to passivate/replicate conversation scoped beans as well
  • @ApplicationScoped beans

    • These really should be serializable but under certain circumstances, your app will work even when they are not
    • These circumstances being when you can avoid serialization, e.g. running on a single node application server
    • As soon as you need to replicate such bean into several nodes, you need it to be serializable too
    • Another case when serializability can be omitted is in SE
    • Your IDE is therefore smart and does not mandate the Serializable presence
like image 88
Siliarus Avatar answered Sep 08 '25 17:09

Siliarus


Serializable marker is one of the required attributes for beans, that are passivation capable(able to transform from active state to some kind of second inactive state). A bean must be passivation capable, if it has a passivating scope(scope with attribute passivating=true). According to CDI spec 1.1. only Session and Conversation scope are passivating scopes.

Chapter 6.6.4 Passivating scopes For example, the built-in session and conversation scopes defined in Section 6.7 are passivating scopes. No other built-in scopes are passivating scopes.

Therefore your session beans need to be passivation capable -ergo need to be Serializable, but your Application scoped beans needs not. Also, some CDI containers will not throw the error on missing Serializable on session scoped beans during deployment, but only at the time when they actually need to transfer the bean instance from active state to passive state(e.g. instance limit has been hit, memory usage, etc)

For more info just read the CDI 1.1 spec.
Happy hacking

like image 36
yntelectual Avatar answered Sep 08 '25 18:09

yntelectual