javajakarta-eesession-scopestateful-session-bean

@Stateful and @SessionScoped


I'm having a problem about the bean lifecycle I don't understand. I have a @SessionScoped bean in my war:

@Named
@SessionScoped
public class UserBean implements Serializable {
  @Inject
  UserServicesLocal userServices;
  [...]
}

then a @Staseful session bean in the ejb part:

@Stateful
@LocalBean
@SessionScoped
@ExcludeDefaultInterceptors
public class UserServices implements UserServicesLocal, Serializable {
  [...]
}

they play nicely but when an unchecked exception is generated in the ejb part the SFSB "die". I understand it's the expected behavior but I don't understand how to manage this situation. For example: the user go into a page where he can upload an xls file, upload a file and then the processing of the file fail for some weird reason. The "WeirdReasonException" is not caught, the SFSB disappear, and every subsequent call generate a "javax.ejb.NoSuchObjectLocalException: The EJB does not exist". I know I should avoid leave an exception uncaught, but if something weird pass I think the user should be able to continue his work. Is there a method to "force" the recreation of the SFSB?

Thank you


Solution

  • I assume by "SFSB" you mean the UserServices bean. If that's correct, then I wouldn't let the bean die--I'd wrap the part that might throw an unchecked exception you could recover from in a try/catch block, and catch RuntimeException (not Exception, by the way). Log the exception and return something the UI can interpret as "processing your input failed".

    It's never a problem to catch "Exception" if you can actually handle it. Most of my code catches exception at the UI handler level, so the app doesn't die if something unexpected happens down in the bowels of the code.