SQLite Handler class as singleton?

筅森魡賤 提交于 2019-12-12 05:40:07

问题


I am using SQLite and JAVA.

I would like to know if its a good idea to make a singleton SQLite DB handler class. I have a normal SQLite DB handler class but I keep on running into NOT Closed DB or closed to early DB problems which I cant seem to figure out.

So I thought that I might have to make a singleton class and just open it on creation and close it on destruction. Also all DB Queries will go through this class that is always open. Why would this be a bad idea?

Side question...is it a great idea to keep a DB Connection open for as long as the program is running?

Also I have read that a SQLite DB can only have a connection from 1 thread. Why is that? Therefore this will not work : /

Thanks


回答1:


If you have a single database, using a singleton is perfectly fine.

A database connection does not interfere with other accesses (as long as no transaction is active), and does not use many resources. Closing the database would just increase the overhead when you need to re-open it.



来源:https://stackoverflow.com/questions/32561154/sqlite-handler-class-as-singleton

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