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

  • Question

  • 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

    Thursday, December 1, 2011 12:24 AM

Answers

  • 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


    Regards, Amber zhang
    • Proposed as answer by amber zhang Friday, December 9, 2011 3:35 AM
    • Marked as answer by Stephanie Lv Monday, December 12, 2011 7:52 AM
    Friday, December 2, 2011 2:58 AM
  • 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.

    Regards, Amber zhang
    • Proposed as answer by amber zhang Friday, December 9, 2011 3:35 AM
    • Marked as answer by Stephanie Lv Monday, December 12, 2011 7:52 AM
    Wednesday, December 7, 2011 6:40 AM

All replies

  • 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


    Regards, Amber zhang
    • Proposed as answer by amber zhang Friday, December 9, 2011 3:35 AM
    • Marked as answer by Stephanie Lv Monday, December 12, 2011 7:52 AM
    Friday, December 2, 2011 2:58 AM
  • 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. 
    Friday, December 2, 2011 10:20 PM
  • 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.

    Regards, Amber zhang
    • Proposed as answer by amber zhang Friday, December 9, 2011 3:35 AM
    • Marked as answer by Stephanie Lv Monday, December 12, 2011 7:52 AM
    Wednesday, December 7, 2011 6:40 AM
  • 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


    Best Regards -- Yahor Sinkevich

    Wednesday, February 20, 2013 11:49 AM
  • 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.

    Friday, January 30, 2015 7:53 PM