Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To close or not to close connection in database

I work with Windows-Mobile and Windows-CE using SqlCE and I dont know what better to do.

To open connection when the program open, run any query's... update...delete database and close the connection after the program close?

Or open connection run any query's..update...delete database and close the connection immediately?

like image 536
Gali Avatar asked Feb 10 '11 20:02

Gali


People also ask

Should you close database connections?

Keep your customers happy by making sure that there are always enough connections available to service requests. And by keeping the number of open connections to a minimum, you also help improve your database's performance. So, remember to always close those open database connections!

What happens if you don't close database connection?

If we don't close the connection, it will lead to connection memory leakage. Until application server/web server is shut down, connection will remain active, even if the user logs out.

Should I keep a database connection open?

For fast response time and high throughput, it's actually best to keep database connections open and reuse them for subsequent requests.

Why should I close SQL connection?

An application can call Close more than one time. No exception is generated. If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose .


1 Answers

Nice. The answers are all over the place. Here's what I know from experience and interacting with the SQL Compact team:

  1. Closing the connection flushes the changes you've made, otherwise the engine waits for the flush period before doing it. It's a good idea to close the connection when you're done using it to ensure that your changes actually go to the store. A power loss after a write and before a flush will lose data.
  2. There is no official connection pool, but opening the first connection is expensive (i.e. slow), all others are quick. The recommendation I got from the team is to actually create a connection when the app starts up and just leave it open. You don't actually need to use it, but keeping it open keeps a lot of connection info cached so that subsequent connections to the same store are quick.

So the answer, actually, is both.

Edit

For those interested, a good example of how this works can be seen in the OpenNETCF ORM library. The library, by default, creates a "maintenance" connection that remains open and is used for doing things like schema queries. All other data operations use their own connection. You also have to option to configure the library to reuse a single connection for the life of the Store, or to use a new connection every time it touches the store. Perfomance and behavior has always been best in all of my projects using the default (which is why I made it the default).

like image 179
ctacke Avatar answered Dec 07 '22 23:12

ctacke