Forum Moderators: open

Message Too Old, No Replies

Should a Connection be left open in a windows app.

Should a Connection be left open in a windows app.

         

mavrick

2:04 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



Hi guys

I was asked to do some research on weather...

I have an windows application which connects to the a SQL db, code being used is .net, SHOULD this connection be opened once(to the SQL DB)and run as long as the app. is running or should it be opened and closed as the app. needs it.

WHY?

I would personally open and close it as the app. needs it.
I would just like to know what is been done out there.

Thanks

peterdaly

2:20 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



For me, it would depend on the exact nature of the application. There is overhead in opening a connection, but there is also (mostly server) overhead in leaving it open.

It would depend a lot on how often and how the connection would be used. How much time normally would there be between needing the connection?

One possible middle ground would be to build in a single connection pool, where an open connection is kept open for 5 minutes, and then closed if it has not been reused.

It also depends on how "polished" the application is expected to be.

CaseyRyan

5:40 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



I would open and close it.

The overhead with connection is usually only experienced the first time a connection is made. If connections are frequent, the connections after the first will actually be grabbed from the pool on the local machine (these happen rather quickly). If conenctions aren't frequent, chances are the connection object is still in memory and will load quickly.

Another reason to open and close the connection is that at the opening you can fail gracefully if the connection doesn't happen. When you leave a connection open, the status of the connection can change without the connection object knowing. Meaning that if you have an open connection and the network goes down or server resets, the connection object still thinks that it's open and active. You just get a funky error when you go to execute the sql.

-=casey=-

TheNige

8:04 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



Open late, close early. That is the rule you should live by.

CaseyRyan

8:17 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



Open late, close early. That is the rule you should live by.

never heard this rule phrased that way. it's awesome and my new mantra.

-=casey=-