Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ThreadAbortException when trying to close a SqlConnection in .NET?

Tags:

c#

.net

vb.net

I keep getting the following exception when I do:

Using cnn As SqlConnection = New SqlConnection(ConnectionStr)
    cnn.Open()  'I am fine up to here'
End Using       'Here I am getting the following exception'

Manually called cnn.Dispose() causes the same exception. It seems to be OK in most places in my code but just in this one function I can't close the connection that I opened because I keep getting the ThreadAbortException. I am stumped, any ideas? any hints? Here is the exception I get:

System.TypeInitializationException: The type initializer for 'System.Data.ProviderBase.DbConnectionClosedPreviouslyOpened' threw an exception. ---> System.Threading.ThreadAbortException: Exception of type 'System.Threading.ThreadAbortException' was thrown.
   --- End of inner exception stack trace ---
   at System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlInternalConnection.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Close()
   at System.Data.SqlClient.SqlConnection.Dispose(Boolean disposing)
like image 905
Denis Avatar asked Apr 26 '12 15:04

Denis


1 Answers

There was a dreaded "Finalize" method that someone put. It kept triggering and trying to close the connection. That was a couple of hours wasted!!! One day am going to get rid of all the Finalize methods in the code - until then - suck it up!

like image 134
Denis Avatar answered Nov 08 '22 20:11

Denis