java - How do I call a DAO method from Service layer in a J2EE web application -
generally lot of applications these days use spring takes care of life cycle of pojo classes in application. if application cannot use spring due other issues. how go service layer of application dao layer.
currently doing.
public class myserviceimpl{ private static mydao daoinstance=new mydao(); public void somemethod(){ daoinstance.methodtocall(); } } public class mydao{ public void methodtocall(){ } }
keeping daoinstance in myserviceimpl static makes sure there 1 instance across serviceimpl objects. wouldn't create concurrency issues if lot of users access same piece of code @ same time.
however if don't keep static, there 1 daoinstance each myserviceimpl object. wouldn't leave many objects in heap. how life cycle of these objects managed.
i want understand correct way of navigating 1 layer of application other layer keeping concurrency, performance , other such factors in mind.
thanks help.
first of all, service class should not directly call dao instance.
interaction between service , dao should through interface in order make loosely coupled.
you can create dao instance singleton in service class , thread safe (i.e spring framework uses singleton default) make sure global variables not used in dao.
if make dao object singleton amount of objects created less in turn improves performance.
Comments
Post a Comment