losing conection to EJB after restart

169 views Asked by At

I have an app connecting to an EJB to retrieve some data. The EJB is in a separate EAR. The app is using a delegate-singleton pattern to get a connection to the EJB. The problem is, the EJB gets redeployed quite often because it's still being developed and every time this happens my app ends up disconnecting and unable to reconnect when the EJB comes back online because the singleton's getInstance method keeps returning an empty remote object reference and the connection cannot be reestablished, so I have to redeploy my client app to be able to reconnect. Any way to keep this from happening, that doesn't involve doing lookups on every request?

public class Delegate 
{

...

private DataStoreService getDataStoreServiceInterface {

        ServiceLocator serviceLocator = ServiceLocator.getInstance();            
        return serviceLocator.getDataStoreService();
    }

   public Data getData(){
   DataStoreService dataStoreService = getDataStoreServiceInterface();
   return dataStoreService.getData();
    }

}



public class ServiceLocator{

private static ServiceLocator instance = null;

private DataStoreService dataStoreService = null;

protected Context context;

private ServiceLocator(){

context = new InitialContext();
dataStoreService = (DataStoreService)context.lookup(DataStoreService.class.getName());
}

 static public ServiceLocator getInstance(){

    if (instance == null) {
        instance = new ServiceLocator();
    }
    return instance;
  }
}
2

There are 2 answers

1
bez On BEST ANSWER

Apparently this is a container bug in Weblogic 10.3.6, I had to remove singleton logic to avoid this issue

1
erv-Z On

Don't use the singleton pattern whereas the project is in development mode, so every time that you call getInstance return a new instance. Also, in the way that you are coding the Class that get the EJB's instance, you will have a problem because only one thread in your app could call that EJB because whereas a EJB is in use, no one can use that instance and in your all application use only one and the same ejb's instance.

Another way, its use a Singleton session bean (EJB) instead of using the a Singleton Pattern.