Zuoshu Lu
Zuoshu Lu

Reputation: 67

Application connection to database

I have an application interacts with Access database using DAO class, recently I converted the database to a sqlite database.

I do not know which connection method is better for the design as following:

  1. Create only one database connection using a public variable when open the application, any queries use the only connection object for interaction during the run time, the connection is then closed when close the application

  2. Create database connection every time before running a query, then close the database connection instantly after loading the resultset to the memory.

Upvotes: 1

Views: 203

Answers (2)

StuartLC
StuartLC

Reputation: 107247

I recommend that you encapsulate your db access, so that the decision on whether to keep a persistent connection or not open can be changed at a later point.

Since you are using SqlLite I am assuming that it is a single user DB, so concurrency , connection contention, locking etc. are not likely to be issues.

Typically the main reasons to reuse short running connections is usually on a multi user web or service oriented system, where scalability and licensing considerations are important. This doesn't seem to be applicable in your case. . In short, there doesn't seem any reason not to keep a connection open for the entire duration of your app / user's login session based on the above assumptions.

If you use transactions however, I would suggest that you commit these after each successful atomic activity

Upvotes: 1

HS Developer
HS Developer

Reputation: 26

You know your two options have + and -. For your special case I think to create database connection every time is not so bad idea, because creating connection to sqlite is very fast and no time consuming. Also this way you may create/close more than one connection at once, which is a good benefit, maybe you don't do it now, but in the future maybe you will have to.

Upvotes: 0

Related Questions