[英]Should I make my database adaptor class a singleton?
I am using the database adaptor from devx.com 我正在使用devx.com的数据库适配器
I was wondering if I should make it a singleton so I can easily use it from a number of different classes without having to worry about multiple instances? 我想知道我是否应该把它变成单身,这样我就可以轻松地从许多不同的类中使用它而不必担心多个实例?
Though your logic is valid, I dont think you should. 虽然你的逻辑是有效的,但我认为你应该这样做。 Think of concurrencies, eg.
想想并发,例如。 class X is updating the database, and class Y is retreiving values from it at the same time .
class X正在更新数据库,而Y类正在同时从中检索值。 So you have 2 operations on the database at the same time;
因此,您同时对数据库进行了2次操作; having a singleton shall prevent this, as the db connection needs to be closed/reopened .
由于数据库连接需要关闭/重新打开,因此具有单例应防止这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.