Disposing a SqlConnection class resource

C

CEO Gargantua

I set up a class for managing SqlConnection's.

Then I added a method to open and return a SqlConnection (see below).


Question, if I instantiate this class in another class:

sql400con connection = new sql400con();

and use the method

So:

SqlConnection newConn = connection.makeSQLConnection();

Question:

What is the best way to dispose of this SqlConnection?

In a destructor?

From the calling class?

How do I reference a member of a member method from the class itself (
to implement in the destructor ) ?


using System;
using System.Data.SqlClient;
using IBM.Data.DB2.iSeries;
using System.Configuration;

namespace Trigger
{
/// <summary>
/// Summary description for sql400conn.
/// </summary>
public class sql400conn
{



public sql400conn()
{
//
// TODO: Add constructor logic here
//
}



public iDB2Connection makeDB2Connection()
{

iDB2Connection c_iDB2Connection = new iDB2Connection();

return c_iDB2Connection;

}

public SqlConnection makeSQLConnection()
{

SqlConnection c_SqlConnection = new SqlConnection(

c_SqlConnection.Open():
);

return c_SqlConnection;

}

}
}




--
incognito...updated almost daily
http://kentpsychedelic.blogspot.com

Texeme Textcasting Technology
http://texeme.com
 
G

Guest

Based on what you are doing, the consuming class can destroy it, as it has a
full object to work on. This relies on the developers consuming the
connection, of course.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
C

CEO Gargantua

Great, so destroying the object, closes the SqlConnection.

Based on what you are doing, the consuming class can destroy it, as it has a
full object to work on. This relies on the developers consuming the
connection, of course.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

:


--
incognito...updated almost daily
http://kentpsychedelic.blogspot.com

Texeme Textcasting Technology
http://texeme.com
 
S

Sahil Malik

A BIG BIG ABSOLUTELY NOT !!! That would qualify as the biggest ADO.NET
mistake you can make.

If a SqlConnection object that was open falls out of scope, eventually
finalize will get called on it - that is true .. but it might be 4-8 minutes
later.
Which means all that time it will hold an open instance of a physical
connection - that is not pool-able until it gets GC'ed. Even then it might
not clean it properly.

You should NEVER EVER rely on GC or Finalize to clean your open db
connections for u.
So whatdyu do? Implement Dispose on the class that holds an instance to
Connection - and make sure you call Connection.Dispose in this dispose and
make sure you call TheHolderClass.Dispose.
Or even better - implement a datalayer and pass only connection strings
rather than connection objects, so you don't have to deal with this mess.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top