SCM

[#1011220] System.ObjectDisposedException when accessing State

View Trackers | Bugs | Download .csv | Monitor

Date:
2012-08-27 08:43
Priority:
3
State:
Open
Submitted by:
Jochen Wezel (jwezel)
Assigned to:
Nobody (None)
Npgsql Version:
2.0.12beta
Category:
Group:
Resolution:
None
Summary:
System.ObjectDisposedException when accessing State

Detailed description
THE SITUATION

Please see following code snippet:

----SNIP----
Dim npgconn As New Npgsql.NpgsqlConnection
'Check for State properties works as expected while not disposed
Console.WriteLine("Status: " & npgconn.State)
Console.WriteLine("FullStatus: " & npgconn.FullState)
npgconn.Dispose()
'Check for State properties should work even if disposed to be able to check for a closed connection
'BUG: accessing the State properties throw a System.ObjectDisposedException
Console.WriteLine("Status: " & npgconn.State)
Console.WriteLine("FullStatus: " & npgconn.FullState)
----/SNIP----

WHY IT'S A BUG:
1. Since throwing/catching exceptions consumes lot of time, it's better to check for an object state if it's valid or not - in general.
But currently, there is NO possibility to check for the connection object state without immediately throwing an exception.
2. other .NET data providers (e.g. System.Data.SqlClient.SqlConnection) also never throw an exception when accessing the state property even if the object is disposed

HOW IT SHOULD BE RESOLVED:
At least the state property should return a valid state info System.Data.ConnectionState.Closed as the other .NET data providers (e.g. System.Data.SqlClient.SqlConnection) do.

Followup

Message
Date: 2014-02-07 14:22
Sender: Jochen Wezel

No, it's not for reusing the conntection after disposing.
It's e. for
- checking if the Connection is still open and ready for use for another SQL command
- checking if the conneciton is still open and has to be closed (closing may happen automatically on dispose, but there are several situations when it's required to force closing because the disposing by Garbage collector is on a later time - and for heavy load Environments it's sometimes critical to release ressources immediately without delay)
Date: 2012-09-21 11:11
Sender: Jochen Wezel

I Suggest a patch like following:

----SNIP----
Public Overrides ReadOnly Property State As ConnectionState
Get
If Me.isDisposed Then
Return ConnectionState.Closed
Else
...standard behaviour...
End If
End Get
End Property
----SNIP----

This will save lot of time since expensive try-catch for
ObjectDisposedException is not required any more (and makes
apps more robust) :-)
Date: 2012-08-27 13:54
Sender: Josh Cooley

Do you expect to open a connection again after it has been disposed? I'm not sure we should commit to supporting that. If you aren't planning to open a connection again after it has been disposed, why do you want to check it's state? Why even keep it around?

Attached Files:

Changes:

No Changes Have Been Made to This Item

Powered By FusionForge