"Could not synchronize database state with session" error

Topics: Troubleshooting
Apr 9, 2012 at 5:04 PM

I've got a 1.4 site that I'm trying to update, but starting last Friday, whenever I try to update any aspect of the site, the update fails silently (i.e., no error is shown to the user, but the update fails to take), and exceptions like this show up in the logs:

2012-04-09 12:53:14,746 [15] NHibernate.Event.Default.AbstractFlushingEventListener - Could not synchronize database state with sessionNHibernate.StaleStateException: Unexpected row count: -1; expected: 1   at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)   at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)   at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)   at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)   at NHibernate.Action.EntityUpdateAction.Execute()   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)   at NHibernate.Engine.ActionQueue.ExecuteActions()   at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)

2012-04-09 12:53:14,746 [15] NHibernate.Impl.AbstractSessionImpl - DTC transaction prepre phase failedSystem.Transactions.TransactionException: The operation is not valid for the state of the transaction. ---> System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()   at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)   at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)   at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()   at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()   at System.Data.SqlClient.TdsParserStateObject.ReadByte()   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)   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.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)   at System.Data.SqlClient.SqlDelegatedTransaction.SinglePhaseCommit(SinglePhaseEnlistment enlistment)   --- End of inner exception stack trace ---   at System.Transactions.TransactionState.Rollback(InternalTransaction tx, Exception e)   at System.Transactions.Transaction.Rollback()   at System.Transactions.TransactionScope.InternalDispose()   at System.Transactions.TransactionScope.Dispose()   at NHibernate.Transaction.AdoNetWithDistrubtedTransactionFactory.DistributedT

Any suggestions?

There are two slightly not-standard issues with this current site that could conceivably have something to do with this:

(1) I tried for a while to run the <1.x> branch, but reverted back to the <default>branch after running into this error: http://orchard.codeplex.com/discussions/351240

(2) In a custom module, I'm accessing data from an external database, using the ambient Orchard transaction: http://orchard.codeplex.com/discussions/350250

I haven't been able to reproduce this error on my dev box yet, but I'll keep plugging away.

Apr 9, 2012 at 6:28 PM

OK, well, I haven't figured it out, but I've determined that it has something to do with #2 above (e.g., http://orchard.codeplex.com/discussions/350250). If I go back and wrap my own database access code with a TransactionScope(TransactionScopeOption.Suppress), I don't get the error above. However, I do continue to get the original error that prompted that thread. Closing this one down for now; I'll go back and re-raise the issue on the previous thread.

May 28, 2012 at 5:13 PM

First results on Orchard, thought I'd chime in, the issue we had was had a Service using ISingletonDependency which had an IRepository so I'm pretty sure it losts its session so then threw the error, I think the rule is, don't mix Dependency types.

May 29, 2012 at 5:10 PM

In case you really need to get an IDependency from a ISingletonDependency (there are very specific cases) then you can inject a Work<T> which will be resolved contextually when used. It's like a Lazy<T> with WorkContext in mind.