which scope should a DAO typically have

爱⌒轻易说出口 提交于 2019-12-08 07:44:53

问题


its out of question that a dao will not hold any state.

however, for easiest access to the class, is it better to use prototype( = new every time) or singleton?

simple object creation is cheap for dao's.. it typically only holds a sessionfactory, accessing the object from a list of singletons may be equally expensive.

clarfication: the focus of this question is, if there is a common convention to the scoping of daos.


回答1:


If your question is about architecture, I'd go with scoping DAOs to the Unit of Work or Transaction you are doing. This reduces the potential for cross-transaction pollution and threading-issues.

If your question is about performance, then the answer lies within a profiler, which gives you accurate numbers for your particular workload.




回答2:


Or use prototype in combination with a pool as described here:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop-api.html#aop-ts-pool

OR here:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/classic-aop-spring.html#classic-aop-ts-pool

EDIT: obviously, I assume that spring is used here. Otherwise I apologize




回答3:


i have come to the conclusion that there is no "perfect" way of deciding this. singleton scope is most likely the wrong way of doing it in a web app, since you will have different sessions - one per request. so in a webapp - request scope may be the correct answer, but only if you are using it exclusively in requests and not in background tasks. prototype scope is viable - but only if you are not holding complex data there.



来源:https://stackoverflow.com/questions/201022/which-scope-should-a-dao-typically-have

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!