By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Regarding Connection object behaviour

P: n/a
Hi ,

I am using OracleConnection object from Oracle ODP.net provider and
following is the behaviour which i am finding bit strange :

To start with my argument is based on followings facts :

1. Connection object is a reference type object .
2. All reference types are passed by reference even when done without
using modifier like ref / out .

Have a look at the following code :

Class A
{
OracleConnection conn ; // class variable

public static void Main()
{
A a = new A() ;
string connectionString = <StandardStuff> ; // Pooling false
a.c(connectionString);

connectionString = <StandardStuff> ; // Pooling true
a.c(connectionString);
}

public void c(string connectionString)
{
try
{
// Get a connection string

if(conn == null)
conn = new OracleConnection(connectionString)
else
conn.ConnectionString = connectionString ;

conn.Open() ;

CloseandDispose(conn,<Pooling Attrbute Value from connection
string>) ;
}
catch(OracleException ex)
{
// print exception message
}
catch(Exception ex)
{
// print exception message
}

}

public void CloseandDispose(OracleConnection conn,bool flag)
{
conn.Close() ;

if(!flag)
{
conn.dispose();
conn = null ;
}
}
}

Now in above mentioned code :

Case 1 : (OracleException)
========

When conn object is class variable and method c is called from second
time from main method after changing Connection String then it leads to
exception .

Reason is --> during first call when CloseandDispose method is called ,
it enters the loop to dispose and nullify the connection object but
second time when method c is called it doesn't validates the condition
conn = null and when it goes down and tries to open connection , then it
leads to exception :

"Can't open a connection object already disposed"

Now there are cases when exception doesn't happen i.e :

Case 2: --> I pass Oracleconnection objects between methods using ref
keyword and operation is successful .

Case 3 --> Don't use OracleConnection object in CloseAndDispose method
and it will nullify class object and valiadte conn = nul condition .

Case 4 --> Declare OracleConnection as a local variable in main method
and pass it in all methods and even then it success .

Now in present scenario it seems to be an issue with Connection object
as Case 1 also should have been successful .

Share your comments , in case you need some other specific details let
me know that .

thanks ,

Mrinal

Feb 20 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Mrinal,
It isn't very clear what exactly the objective of your exercise is, but it
might help to point out that connection pooling is based on the unique
connection string. In other words, with pooling on, if you use a different
connection string, a new connection will be created instead of a matching one
being returned from the pool.

The best pattern to use in almost all cases is to open a connection just
before use, do your work, and close or Dispose the connection immediately
afterward, and to leave pooling true (the default).

The code you present where you have separated out the different portiions
such as your "CloseAndDispose" method is somewhat non-standard.

--Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Mrinal Kamboj" wrote:
Hi ,

I am using OracleConnection object from Oracle ODP.net provider and
following is the behaviour which i am finding bit strange :

To start with my argument is based on followings facts :

1. Connection object is a reference type object .
2. All reference types are passed by reference even when done without
using modifier like ref / out .

Have a look at the following code :

Class A
{
OracleConnection conn ; // class variable

public static void Main()
{
A a = new A() ;
string connectionString = <StandardStuff> ; // Pooling false
a.c(connectionString);

connectionString = <StandardStuff> ; // Pooling true
a.c(connectionString);
}

public void c(string connectionString)
{
try
{
// Get a connection string

if(conn == null)
conn = new OracleConnection(connectionString)
else
conn.ConnectionString = connectionString ;

conn.Open() ;

CloseandDispose(conn,<Pooling Attrbute Value from connection
string>) ;
}
catch(OracleException ex)
{
// print exception message
}
catch(Exception ex)
{
// print exception message
}

}

public void CloseandDispose(OracleConnection conn,bool flag)
{
conn.Close() ;

if(!flag)
{
conn.dispose();
conn = null ;
}
}
}

Now in above mentioned code :

Case 1 : (OracleException)
========

When conn object is class variable and method c is called from second
time from main method after changing Connection String then it leads to
exception .

Reason is --> during first call when CloseandDispose method is called ,
it enters the loop to dispose and nullify the connection object but
second time when method c is called it doesn't validates the condition
conn = null and when it goes down and tries to open connection , then it
leads to exception :

"Can't open a connection object already disposed"

Now there are cases when exception doesn't happen i.e :

Case 2: --> I pass Oracleconnection objects between methods using ref
keyword and operation is successful .

Case 3 --> Don't use OracleConnection object in CloseAndDispose method
and it will nullify class object and valiadte conn = nul condition .

Case 4 --> Declare OracleConnection as a local variable in main method
and pass it in all methods and even then it success .

Now in present scenario it seems to be an issue with Connection object
as Case 1 also should have been successful .

Share your comments , in case you need some other specific details let
me know that .

thanks ,

Mrinal

Feb 20 '06 #2

P: n/a
Hi Mrinal,

Thanks for your post!

As Peter mentioned, the pattern for manipulation of connection is simple.
We can open a new connection and do some work. If we need use this
connection in other portion, we don't need to close and dispose the
connection. In opposite situation, we don't want to use the connection
again, please just call close method for the connection. Actually, based on
my experience, we can not call dispose method.

I appreciate your understanding! If you have anything unclear, please feel
you free to let me know.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
================================================== ====
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD were
updated on February 14, 2006. Please complete a re-registration process
by entering the secure code mmpng06 when prompted. Once you have
entered the secure code mmpng06, you will be able to update your profile
and access the partner newsgroups.
================================================== ====
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====

Feb 21 '06 #3

P: n/a
Hi Peter ,

Idea is like this :

Code that , i have pasted is part of a test suite , which tests
OracleConnectionStringBuilder introduced as a part of ADO.net 2.0 .

Now , logic behind calling method C twice from the main method is that
connection string builder class is used to change connection string at
runtime , where it changes the pooling attribute and reconnect .

Now initially when Connection is created in Pooling = false mode then i
make sure that after closing connection object is disposed and nullified
, however in case of Pooling = true it's just closed , so that it can
return to connection pool .

Reason for implementing a separate CloseAndDispose method is that in
original test suite , conn object and the CloseAndDispose method are
part of a base class , as multiple derived classes uses same object and
finally any of them can close and dispose , so essentially a method is
implemented in base class to complete the task .

Now as the question goes , point of confusion for me remains that in
case 1 , when i am passing the conn object without any ref / out it
gives me the exception as mentioned earlier but in other cases it
doesn't , which is bit strange and could be a possible issue with
Connection object .

Any comments regarding the behaviour .

If you need some more explanation , let me know that .

thanks ,

Mrinal

Peter Bromberg [C# MVP] wrote:
Mrinal,
It isn't very clear what exactly the objective of your exercise is, but it
might help to point out that connection pooling is based on the unique
connection string. In other words, with pooling on, if you use a different
connection string, a new connection will be created instead of a matching one
being returned from the pool.

The best pattern to use in almost all cases is to open a connection just
before use, do your work, and close or Dispose the connection immediately
afterward, and to leave pooling true (the default).

The code you present where you have separated out the different portiions
such as your "CloseAndDispose" method is somewhat non-standard.

--Peter

Feb 21 '06 #4

P: n/a
Hi Yuan ,

Are you saying that , in any case and scenario , there's no need to call
dispose on connection object , just close will do the job , whether
it's pooling true / false mode .

Even when Classes like OracleConnection has lot of unmanaged code
underlying it , since it's implemented using Oracle's native call
interfaces .

What , i knew was if dispose method is available for a class and there's
no need to use it further , then it should be used as it releases the
unmanaged references which are beyond the purview of GC .

Let me know your views regarding same ,

regards ,

Mrinal

Yuan Ren[MSFT] wrote:
Hi Mrinal,

Thanks for your post!

As Peter mentioned, the pattern for manipulation of connection is simple.
We can open a new connection and do some work. If we need use this
connection in other portion, we don't need to close and dispose the
connection. In opposite situation, we don't want to use the connection
again, please just call close method for the connection. Actually, based on
my experience, we can not call dispose method.

I appreciate your understanding! If you have anything unclear, please feel
you free to let me know.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
================================================== ====
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD were
updated on February 14, 2006. Please complete a re-registration process
by entering the secure code mmpng06 when prompted. Once you have
entered the secure code mmpng06, you will be able to update your profile
and access the partner newsgroups.
================================================== ====
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====

Feb 21 '06 #5

P: n/a
Hi Mrinal,

Thanks for your reply!

As far as I know, the dipose method just marks a flag for the GC. Whether
the connection needs to be released is determined the GC system or
connection pool, even for the oracle client.

The best practice is using the "using" statement in the code as below:
using (Connection c = new Connection())
{
//do some work
}

Hope this will be helpful!

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
================================================== ====
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD were
updated on February 14, 2006. Please complete a re-registration process
by entering the secure code mmpng06 when prompted. Once you have
entered the secure code mmpng06, you will be able to update your profile
and access the partner newsgroups.
================================================== ====
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====

Feb 23 '06 #6

P: n/a
Hi yuan ,

I think marking a flag for GC is the job of the finalizer , which
implements a separate queue for this than dispose , which is meant for
relasing underlying unmanaged references explicitly .

Best practice , i do understand , but my motive was to ensure that what
i was doing is correct and there could be a possible issue with ODP.net
implementation , as i had found through another thread in ADO.net group .

thanks ,

Mrinal

Yuan Ren[MSFT] wrote:
Hi Mrinal,

Thanks for your reply!

As far as I know, the dipose method just marks a flag for the GC. Whether
the connection needs to be released is determined the GC system or
connection pool, even for the oracle client.

The best practice is using the "using" statement in the code as below:
using (Connection c = new Connection())
{
//do some work
}

Hope this will be helpful!

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
================================================== ====
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD were
updated on February 14, 2006. Please complete a re-registration process
by entering the secure code mmpng06 when prompted. Once you have
entered the secure code mmpng06, you will be able to update your profile
and access the partner newsgroups.
================================================== ====
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====

Feb 23 '06 #7

P: n/a
Hi Mrinal,

Thanks for your understanding!

Regards,

Yuan Ren [MSFT]
Microsoft Online Support

Feb 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.