问题
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