I need the connection last til I say otherwise

sirius

New member
Joined
Sep 25, 2006
Messages
4
Location
Estonia
Programming Experience
Beginner
What I mean is that I iniate a connection under one class but when I want to use that connection under other class... well if you know what I'm talking about then.. it's impossible...
So... I read somewhere that I should use either Session Cookies (if web-based) or MDI... Since I'm not making a webapplication... please tell more about the other option ;)

In short: I want a connection to stay open til I tell the program to close it :)
 
Why Bother

Why is it necessary to keep the connection open, would it not be better to open the connection, do your stuff, then close the connection .
 
no

I want to do it my way :)
By the way... I think there's a class version too... but it was easier to do classes in PHP then in .NET :(

I'd like to keep the connection open til I use my program and when I click Close Connection or I close program then the connection will be terminated.
---
So If I wanna alter database content for longer then a minute then I think it would be a waste of code to iniate and close it all the time.
 
I want to do it my way :)
By the way... I think there's a class version too... but it was easier to do classes in PHP then in .NET :(

I'd like to keep the connection open til I use my program and when I click Close Connection or I close program then the connection will be terminated.
---
So If I wanna alter database content for longer then a minute then I think it would be a waste of code to iniate and close it all the time.

You really don't need to worry about this - connection pooling will deal with this far more efficiently that you will be able to manually.

Just make sure you properly dispose of any database objects you have created.
 
I want to do it my way :)
By the way... I think there's a class version too... but it was easier to do classes in PHP then in .NET :(

I'd like to keep the connection open til I use my program and when I click Close Connection or I close program then the connection will be terminated.
---
So If I wanna alter database content for longer then a minute then I think it would be a waste of code to iniate and close it all the time.

without meaning to offend - there's a good reason you arent allowed to do it your way and that is because you dont understand enough of what is going on underneath the hood to see that your way is one of the worst ways possible.

When you open and close a connection, it doesnt disconnect and reconnect the database unless Pooling=false. Pooling=true by default and as its a setting that you arent fully understanding, you ought to leave it as default.

If you seek clarification, google for "connection pooling in .net"
If you still have questions after reading some articles, by all means return and we will attempt to answer them

Just make sure you properly dispose of any database objects you have created.

I'm not certain whether dispose is overriden to do nothing on a database connection but it ought to be. I dont dispose my database connections because I am not sure whether it destroys the connection in such a way that it becomes unusable to the pool..

i.e. if it DOES destroy the conenction, then there is no point using pooling because if you manually cause the connection to be disrupted and hence force the pool to connect to the database again, then there's no point using pooling.

Did you read somewhere that pooled conenctions can/should be .Dispose()d without ill effect? I never saw any microsoft examples that Disposed the connection,only Close()d it
 
Perhaps not relevant to the MySql implementation of Connector/Net, but SqlConnection.Close method documentation states that .Close and .Dispose methods are functionally equivalent. (This is valid for this class only.)
 
. . .

I'm not certain whether dispose is overriden to do nothing on a database connection but it ought to be. I dont dispose my database connections because I am not sure whether it destroys the connection in such a way that it becomes unusable to the pool..

i.e. if it DOES destroy the conenction, then there is no point using pooling because if you manually cause the connection to be disrupted and hence force the pool to connect to the database again, then there's no point using pooling.

Did you read somewhere that pooled conenctions can/should be .Dispose()d without ill effect? I never saw any microsoft examples that Disposed the connection,only Close()d it

Sorry - my mistake I was using the word "dispose" generically rather than referring to a method.

My understanding of the issue is:

Command and datareader objects should be destroyed by calling the object.dispose method.

Connections should be closed using the object.close method but should NOT be destroyed after closing using "object = nothing"

If the connection object is destroyed, either explicitly or by the GC, the connection cannot be recycled by the pool. In addition the connection strings must be absolutely identical otherwise there will be a separate pool for each version.

However the above originates from Microsoft's guidelines for classic ADO in SQL Server/ASP so it may not still be applicable to ADO.NET - but it certainly works efficiently and pools correctly in all the testing I have done.
 
Last edited:
Perhaps not relevant to the MySql implementation of Connector/Net, but SqlConnection.Close method documentation states that .Close and .Dispose methods are functionally equivalent. (This is valid for this class only.)


I did some looking up:

The IDbConnection interface which all ADO.NET connection classes implement - inherits from the IDisposable (similarly IDataReader and IDBCommand) so it is inconsistent that a dispose method is not exposed.

However, I guess, this can be explained as the close method is far more familiar to a whole generation of ADO programmers.

The close method itself works according to the pooling setting on the connection.

Quote:

The Close method rolls back any pending transactions. It then releases the connection to the connection pool, or closes the connection if connection pooling is disabled.

from:

http://msdn.microsoft.com/library/d...lrfsystemdataidbconnectionclassclosetopic.asp


So I guess, contrary to what I posted above, you can do anything you like to the connection object once you have closed it without having any adverse effects.
 
Back
Top