prototype - which scope should a DAO typically have - Stack Overflow

admin2022-05-22  57

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.

Answers:

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.

Answers:

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.

Answers:

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

Answers:

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://www.u19.cn/read-111905.html

New Post(0)