System.ObjectDisposedException: Safe handle has been closed while trying to begin a transaction

I am seeing this error randomly in one of our environments...Not sure how this can happen.  Looks like the connection is disposed after EF ensures that the connection is good(in SaveChanges).  Any thoughts? 

System.Data.EntityException: An error occurred while starting a transaction on the provider connection. See the inner exception for details. ---> System.ObjectDisposedException: Safe handle has been closed

 

   at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)

   at SNINativeMethodWrapper.SNIPacketReset(SafeHandle pConn, IOType ioType, SafeHandle packet)

   at System.Data.SqlClient.TdsParserStateObject.WriteSni()

   at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)

   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)

   at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName)

   at System.Data.SqlClient.SqlInternalConnection.BeginTransaction(IsolationLevel iso)

   at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)

   at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)

   --- End of inner exception stack trace ---

   at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)

   at System.Data.EntityClient.EntityConnection.BeginTransaction()

   at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)

 

Thanks,

Bek

December 1st, 2011 3:24am

Hi Hikmatbek,

Based on the error message you provided, which seems to be .NET issue. The exception ‘Safe handle has been closed’  might be available for various reasons. Please try to log more information from the UnhandledException Event in order to get an idea of what is causing them, and also how to deal with the exception.

If you want to get further help, please post your code in the related forums as below:
.NET Development Forums: http://social.msdn.microsoft.com/Forums/en-US/category/netdevelopment
ADO.NET Entity Framework and LINQ to Entities: http://social.msdn.microsoft.com/Forums/en-AU/adodotnetentityframework/threads

Free Windows Admin Tool Kit Click here and download it now
December 2nd, 2011 5:58am

You are right. .NET Dev Forum might be a better place for this. At the time, it seemed like SQL connections were getting dropped in this environment.  Thanks. 
December 3rd, 2011 1:20am

Hi Hikmatbek,

Thank you for your update.

According to your description this error happened randomly in one of your environments, which might be also related to the network issue or application time out issue due to the configuration disconnected.

Meanwhile the SQL Server does not drop any client connection at all. Hope you can get better help in .NET Dev Forum.
Free Windows Admin Tool Kit Click here and download it now
December 7th, 2011 9:40am

Hi,

We've got the same problem.

It happend on the different servers in the web farm aproximately in the same time.

So, I think it somehow caused bu the SQL server.

I've created bug on the Microsoft Connect.

https://connect.microsoft.com/VisualStudio/feedback/details/779588/safe-handle-objectdisposedexception-sqltransaction-dispose

February 20th, 2013 2:49pm

Hi All,

I have been struggling on a similar issue as well in a web application based on .net framework 2.0 and sql server 2008 R2. the exception happened about 3 times per month since 2011, and when it happens, no one could connect to the web site anymore. Here is the stack trace of the error.

at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)     
at SNINativeMethodWrapper.SNIPacketReset(SafeHandle pConn, IOType ioType, SafeHandle packet)     
at System.Data.SqlClient.TdsParserStateObject.WriteSni()     
at System.Data.SqlClient.TdsParserStateObject.ExecuteFlush()     
at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc)     
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)     
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)     
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)     
at System.Data.SqlClient.SqlCommand.ExecuteScalar()     
at System.Data.SqlClient.SqlCommand.ExecuteScalar()     
at TNSSmartNetwork.SPS.Data.SystemEventLog.EventLogEntry.Add(GenericPrincipal CurrentPrincipal)     
at TNSSmartNetwork.SPS.Web.Shell.Global.Session_Start(Object sender, EventArgs e)     
at System.Web.SessionState.SessionStateModule.RaiseOnStart(EventArgs e)     
at System.Web.SessionState.SessionStateModule.CompleteAcquireState()     
at System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData)     
at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()     
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

I noticed the bug filed on the Microsoft Connect had been fixed, but it does not mention the reason and which version of .net framework it is fixed.

https://connect.microsoft.com/VisualStudio/feedback/details/779588/safe-handle-objectdisposedexception-sqltransaction-dispose

Appreciated if you could provide more details.

Free Windows Admin Tool Kit Click here and download it now
January 30th, 2015 10:53pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics