Multithreading in EJB

I study EJB, so far I have read that multithreading is not allowed in EJB'S, because it is the container that should take care of thread safety and allow the developer to focus only on business logic, so basically this means that EJB guarantees that there is only one thread has access to a method at the same time in a bean session.

What happens when many users have access to the same method in EJB? Is the container serializing acceses or creating different instances of a bean, one per thread?

Can someone explain to me what politics is? I'm also a bit confused why, if multithreading is not allowed, so we cannot create our own threads, why do we have this @Asynchronous annotation?

+5
source share
1 answer

Yes, it creates several instances and combines them. See Oracle's official documentation :

Since a session without a bean is never passivated, its life cycle has only two stages: nonexistent and ready to call business methods. Figure 22-4 illustrates the steps of a session without a bean.

An EJB container typically creates and maintains a beans-free session pool, starting the life cycle of a stand-alone bean session. The container does any dependency injection, and then calls the annotated @PostConstruct method, if one exists. bean is now ready to use its business methods by the client.

At the end of the life cycle, the EJB container calls the annotated @PreDestroy method, if one exists. The bean instance is then ready for garbage collection.

+7
source

All Articles