PC Review


Reply
Thread Tools Rate Thread

Disposing a SqlConnection class resource

 
 
CEO Gargantua
Guest
Posts: n/a
 
      19th Jan 2005

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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
Guest
Posts: n/a
 
      19th Jan 2005
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!
***************************

"CEO Gargantua" wrote:

>
> 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
>
>

 
Reply With Quote
 
CEO Gargantua
Guest
Posts: n/a
 
      19th Jan 2005

Great, so destroying the object, closes the SqlConnection.


Cowboy (Gregory A. Beamer) - MVP wrote:
> 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!
> ***************************
>
> "CEO Gargantua" wrote:
>
>
>>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
>>
>>



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

Texeme Textcasting Technology
http://texeme.com

 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      20th Jan 2005
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




"CEO Gargantua" <(E-Mail Removed)> wrote in message
news:flCHd.444$(E-Mail Removed)...
>
> Great, so destroying the object, closes the SqlConnection.
>
>
> Cowboy (Gregory A. Beamer) - MVP wrote:
>> 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!
>> ***************************
>>
>> "CEO Gargantua" wrote:
>>
>>
>>>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
>>>
>>>

>
>
> --
> incognito...updated almost daily
> http://kentpsychedelic.blogspot.com
>
> Texeme Textcasting Technology
> http://texeme.com
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
errors when closing and disposing SqlConnection in DLL Eli Silverman Microsoft C# .NET 1 3rd Jun 2010 07:44 PM
disposing SqlCommand and SqlConnection =?Utf-8?B?TWlrZSBLcmFsZXk=?= Microsoft Dot NET Framework 13 1st Aug 2007 08:38 AM
Disposing strings created from a resource file using resource mana =?Utf-8?B?TXVydGh5?= Microsoft Dot NET Compact Framework 3 25th Jun 2007 03:30 PM
Disposing SqlConnection MuZZy Microsoft C# .NET 13 1st Mar 2005 08:26 AM
Brush Resource - Disposing =?Utf-8?B?RGVubmlz?= Microsoft VB .NET 5 23rd Dec 2004 04:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:57 PM.