views:

94

answers:

2

Which one is better while developing win-based application which uses a Database as its data store ? What about web-based applications?

1) when user loads he first form of an application, the global connection opens and by closing the last form of the application, the connection closes and disposes.

2) for every form within the application, there is a local connection (form scope) and when user wants to performs an operation like insert, update, delete, search, ... the application uses the connection and by unloading the form, the connection also closes and disposes.

3) for every operation within a form of an application, there is a local connection (procedure scope) and when user wants to performs an operation like insert, update, delete, search, ... the application uses procedure connection and at the end of every procedure within the form, the connection also closes and disposes.

A: 

Go with #3

You should try to only ever keep connections open for just as long as is required.

Also have a look at

Connecting to a database server typically consists of several time-consuming steps. A physical channel such as a socket or a named pipe must be established, the initial handshake with the server must occur, the connection string information must be parsed, the connection must be authenticated by the server, checks must be run for enlisting in the current transaction, and so on.

In practice, most applications use only one or a few different configurations for connections. This means that during application execution, many identical connections will be repeatedly opened and closed. To minimize the cost of opening connections, ADO.NET uses an optimization technique called connection pooling.

Connection pooling reduces the number of times that new connections must be opened. The pooler maintains ownership of the physical connection. It manages connections by keeping alive a set of active connections for each given connection configuration. Whenever a user calls Open on a connection, the pooler looks for an available connection in the pool. If a pooled connection is available, it returns it to the caller instead of opening a new connection. When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of closing it. Once the connection is returned to the pool, it is ready to be reused on the next Open call.

astander
It seems this is a Windows-based (GUI?) application. If so, there's probably only going to be one user-interaction process going on at once, and I don't see how connection pooling would be at all useful.Moreover, it sounds like you're proposing to maximize the overhead of the application in the name of keeping connections open only for a very short amount of time. This tradeoff does not sound worthwhile to me, without any further knowledge of particular constraints.
fennec
I do recall this line from the question : *What about web-based applications?*
astander
@Astander: Oops! I missed that totally! (Sorry.) And aside from that, even for the GUI apps, I realize I'm being a bit too user-form-centric in my thinking. If you want different parts of the application to access the database at once (perhaps to update status indicators while the user is using a form) then by all means DO use connection pooling. Aside from that, though, my gut instinct is that opening and closing connections is more overhead than the alternative, so avoid it when possible.
fennec
it depends if the database is local or if it shared on a server between many different clients. it's bad for the user (and adds load to the server) if connections are being opened and closed all the time. OTOH, if many different clients are hanging on to connections but not doing anything with them, then server resources are being wasted. If a local connection pool is used, then it should be configured to release connections after an idle period.
Ken Liu
+1  A: 

This is quite a broad question. But usually, for any database server and application environment, opening and keeping a new connection is an expensive operation. That's why you definitely don't want to open multiple connections from a single client, and should stick to process-scope for connections.

In a desktop application using a database server, strategy for handling it's single connection depends a lot on the DB usage pattern. Say, if the app reads or writes something a lot within 5 minutes, and then just does nothing with the DB for hours, it makes no sense to keep the connection open all the time (assuming there are many other clients). You may introduce some kind of time-out for closing a connection.

The Web server situation depends a lot on the used technology. Say, in PHP every request is a "fresh start" WRT database connection. You open and close a connection for each mouse click. While popular Java application servers have DB connections pool, reusing the same connection instances for many HTTP request handling threads.

Ivan Krechetov