M
Mrinal Kamboj
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
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