'Container-Managed Entity Manager hanging and cause "This web container has not yet been started"

I am using EJB3 with Payara server for my WebApp. Naturally, I am doing some code mistakes and got some exceptions while trying to save an Entity. My issue is: after fixing my code and deplying my WebApp, sometime I got this Exception:

"This web container has not yet been started".

I found that this is because the EntityManager hanging and can not be reinitialized because of a runtime error that happened before. So, the only solution that I found is to restart the Application Server (Payara) and sometime I need to redeploy my WebApp.

My question is: how to avoid that. Is there a way for closing or clearing the EntityManager in case of it is a Container-Managed after handling the exception ? or there is a callback methode that should I call in such case? Or the reason of that is something else that I should to fix.

Appreciate your help and feedback.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source