mardi 25 juillet 2017

How do I call a DAO method from Service layer in a J2EE web application

Generally a lot of applications these days use Spring which takes care of the life cycle of the pojo classes in their application. But what if my application cannot use Spring due to some other issues. How do I go from the Service layer of the application to the DAO layer.

Currently this is what I am doing.

public class MyServiceImpl{

    private static MyDAO daoInstance=new MyDAO();

    public void someMethod(){
        daoInstance.methodToCall();
    }

}

public class MyDAO{

   public void methodToCall(){


   }

}

Keeping the daoInstance in MyServiceImpl as static makes sure that there is only one instance across all ServiceImpl objects. But wouldn't this create concurrency issues if a lot of users access the same piece of code at the same time.

However if I don't keep it static, there would be one daoInstance with each MyServiceImpl object. Wouldn't this leave so many objects in the heap. How would the life cycle of these objects get managed.

I want to understand what is the correct way of navigating from one layer of the application to other layer keeping concurrency, performance and other such factors in mind.

Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire