简体   繁体   English

我应该使我的数据库适配器类成为单例吗?

[英]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.

相关问题 我应该将我的SQLite类变成单例吗? - Should I turn my SQLite class into a singleton? 我应该为我的数据库中的每个表创建一个继承SQLiteOpenHelper的类吗? - Should I create a class that inherits SQLiteOpenHelper for each table in my database? 如何使用数据库制作Android单例和自定义数据类? - How to make an Android singleton and custom data class with database? 我应该在Singleton类中处理一次性用品(Android RxJava 2) - Should I dispose disposables in a Singleton class ( Android RxJava 2 ) 我应该使用单身改装吗? - Should I use retrofit with a singleton? 如何使这个 class 成为 singleton? - How would to make this class as a singleton? ListView适配器崩溃类 - ListView adaptor crashing class 我应该同时创建可序列化和可打包的类吗? - Should I make a class both serializable and parcelable? 用户登录后,我应该使用类instance(序列化)还是Singleton存储userInfo? - Should I use class instance(serialized) or Singleton for storing userInfo once the user login 我应该使用单身人士来跟踪我的Google Analytic跟踪器实例吗? - Should I use a singleton to keep track of my Google Analytic tracker instances?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM