473,738 Members | 3,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regarding Connection object behaviour

Hi ,

I am using OracleConnectio n 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
{
OracleConnectio n conn ; // class variable

public static void Main()
{
A a = new A() ;
string connectionStrin g = <StandardStuf f> ; // Pooling false
a.c(connectionS tring);

connectionStrin g = <StandardStuf f> ; // Pooling true
a.c(connectionS tring);
}

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

if(conn == null)
conn = new OracleConnectio n(connectionStr ing)
else
conn.Connection String = connectionStrin g ;

conn.Open() ;

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

}

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

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

Now in above mentioned code :

Case 1 : (OracleExceptio n)
========

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 Oracleconnectio n objects between methods using ref
keyword and operation is successful .

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

Case 4 --> Declare OracleConnectio n 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
7 2206
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 "CloseAndDispos e" 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 OracleConnectio n 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
{
OracleConnectio n conn ; // class variable

public static void Main()
{
A a = new A() ;
string connectionStrin g = <StandardStuf f> ; // Pooling false
a.c(connectionS tring);

connectionStrin g = <StandardStuf f> ; // Pooling true
a.c(connectionS tring);
}

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

if(conn == null)
conn = new OracleConnectio n(connectionStr ing)
else
conn.Connection String = connectionStrin g ;

conn.Open() ;

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

}

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

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

Now in above mentioned code :

Case 1 : (OracleExceptio n)
========

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 Oracleconnectio n objects between methods using ref
keyword and operation is successful .

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

Case 4 --> Declare OracleConnectio n 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
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
Hi Peter ,

Idea is like this :

Code that , i have pasted is part of a test suite , which tests
OracleConnectio nStringBuilder 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 "CloseAndDispos e" method is somewhat non-standard.

--Peter

Feb 21 '06 #4
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 OracleConnectio n 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
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
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
Hi Mrinal,

Thanks for your understanding!

Regards,

Yuan Ren [MSFT]
Microsoft Online Support

Feb 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3328
by: gg | last post by:
I am confused regarding what the line in the following function does. It seems to work ok. It seems to be creating a new T object using the pointer to an existing T object. But which function is it calling to create the new T object? void f ( auto_ptr < T > & aPT ) { auto_ptr < T > newPT ( new T ( aPT.get ( ) ) ); }
8
5260
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
9
2427
by: niclane | last post by:
Hi, I was reading section 5.5 of Ritchie and Kernighan and saw the following: " ..... char amessage = "now is the time"; char *pmessage = "now is the time";
4
2419
by: Mike Moore | last post by:
We are developing an asp.net web application. When we drag and drop the sql connection object to the form we set the in the dynamic properties connection string to use the connection string in the web.config file. Works Great except everytime we need drag and drop a new data adapter on the form visual studio automatically creates a new connection sql string object. Why? and how to fix?
35
11415
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing Then CType(cn, IDisposable).Dispose() End If I have to admit, I'm not sure what happens here. Will someone explain this line of code (the middle one, not the if statement LOL) to me please?
6
2451
by: Mikus Sleiners | last post by:
Is there any way to enable exception throws in VS 2005, that occur during binding operations? I am upset that i can't see exceptions that are thrown during binding operations. It's very hard to track down erroneous behaviour of your app if you can't see where the problem is... i mean it's realy hard to debug binding issues and it would be realy of help if i could somehow enable this during debug session.
3
1556
by: rkausch | last post by:
Hello, I'm performing some research to determine the feasibility of developing a Windows Service (see http://en.wikipedia.org/wiki/Windows_Service for the specific definition of "service" to which I'm referring) to perform a particular task. I'm having some trouble determining if a service's functionality can be accessed in a non-I/O manner (such as avoiding Sockets, reading and writing a temp file, etc). Basically, I'd like to call a...
5
1587
by: Philip Potter | last post by:
I have a somewhat flippant question regarding undefined behaviour. Does an operation which invokes undefined behaviour affect the whole program, or are earlier statements guaranteed to execute correctly? For example: #include <stdio.h> int main(void) { int i;
167
8316
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an object should run in its own thread, it should implement this mix-in class. Does this sound like plausible design decision? I'm surprised that C++ doesn't have such functionality, say in its STL. This absence of a thread/object relationship in...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.