Monday, 9 September 2013

C# - connection leaking returning from class method

C# - connection leaking returning from class method

I has returning a connection from a class method in Connection class. In
my another class, I instantiate the connection class to open the
connection. I has close the connection as well but it seem like has
connection leak. Any idea how I can fix it. My code as below
public class Connection
{
private SqlConnection _oConn;
public SqlConnection GetConnection
{
get
{
if (_oConn == null)
{
string sConnString =
ConfigurationManager.ConnectionStrings["bplocator_database_connection"].ConnectionString;
_oConn = new SqlConnection(sConnString);
}
return _oConn;
}
}
}
In another class file, i call this connection class
private BPAdmin.data.Connection oConn
{
get
{
if (_oConn == null)
{
_oConn = new BPAdmin.data.Connection();
}
return _oConn;
}
}
public void getData
{
try
{
oConn.GetConnection.Open();
//Do something
}
catch
{
oConn.GetConnection.Close();
}
finally
{
oConn.GetConnection.Close();
}
}
I found that this cause a connection leaking and it cause application pool
reached max. Any idea whats wrong and how I can fix it. Please help!.

No comments:

Post a Comment