At the beginning of the year I launched a site that needed a "Popular Articles" section for their blog. I searched around and the only suggestion I saw for this was to add a field to the page for viewCount and increment that on each page load. It worked like a charm. Now however, I'm getting regular errors about deadlocks, and at least three times have had content disappear from the back end.
The first time it happened I was looked through the logs but not very hard, hoping this was more of a fluke. Obviously there is a larger problem here, so I dug through several days worth of logs to pinpoint when it seemed to be that this particular node lost its content. Here is what I found:
TraceLog:
2016-02-15 13:25:22,415 [P7592/D2/T43] INFO Umbraco.Core.Publishing.PublishingStrategy - Content 'Pros and Cons of Suing Nestle' with Id '7027' has been published.
2016-02-15 13:25:25,759 [P7592/D2/T14] INFO Umbraco.Core.Persistence.UmbracoDatabase - at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)
at System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)
at System.Data.SqlClient.SqlDataReader.Read()
at Umbraco.Core.Persistence.Database.<Query>d__7`1.MoveNext()
2016-02-15 13:25:25,759 [P7592/D2/T14] ERROR Umbraco.Core.UmbracoApplicationBase - An unhandled exception occurred
System.Data.SqlClient.SqlException (0x80131904): Transaction (Process ID 54) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)
at System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)
at System.Data.SqlClient.SqlDataReader.Read()
at Umbraco.Core.Persistence.Database.<Query>d__7`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Umbraco.Core.Persistence.Repositories.VersionableRepositoryBase`2.GetPropertyCollection(Sql docSql, IEnumerable`1 documentDefs)
at Umbraco.Core.Persistence.Repositories.ContentRepository.CreateContentFromDto(DocumentDto dto, Guid versionId, Sql docSql)
at Umbraco.Core.Persistence.Repositories.ContentRepository.PerformGet(Int32 id)
at Umbraco.Core.Persistence.Repositories.RepositoryBase`2.Get(TId id)
at Umbraco.Core.Services.ContentService.GetById(Int32 id)
at ASP._Page_Views_Article_cshtml.Execute() in c:\inetpub\wwwroot\endslaverynow.org\Views\Article.cshtml:line 16
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
ClientConnectionId:0a4e6558-b1e5-4eb3-b462-9aff173fd176
Error Number:1205,State:51,Class:13
2016-02-15 13:25:26,040 [P7592/D2/T16] INFO Umbraco.Core.Publishing.PublishingStrategy - Content 'Pros and Cons of Suing Nestle' with Id '7027' has been published.
2016-02-15 13:25:26,274 [P7592/D2/T49] INFO Umbraco.Core.Publishing.PublishingStrategy - Content 'Pros and Cons of Suing Nestle' with Id '7027' has been published.
2016-02-15 13:25:26,587 [P7592/D2/T44] INFO Umbraco.Core.Publishing.PublishingStrategy - Content 'Pros and Cons of Suing Nestle' with Id '7027' has been published.
2016-02-15 13:25:27,071 [P7592/D2/T14] INFO Umbraco.Core.Services.ContentService - Content 'Pros and Cons of Suing Nestle' with Id '7027' could not be published because of invalid properties.
2016-02-15 13:25:27,509 [P7592/D2/T11] INFO Umbraco.Core.Services.ContentService - Content 'Pros and Cons of Suing Nestle' with Id '7027' could not be published because of invalid properties.
And here is part of the code from Artcle.cshtml:
var cs = ApplicationContext.Current.Services.ContentService;
var node = cs.GetById(Model.Content.Id); //THIS IS THE LINE CALLED OUT IN TRACELOG
if (CurrentPage.HasValue("viewCount"))
{
var count = Model.Content.GetPropertyValue<int>("viewCount");
count = count + 1;
node.SetValue("viewCount", count.ToString());
cs.SaveAndPublish(node);
}
else
{
node.SetValue("viewCount", 0);
cs.SaveAndPublish(node);
}
My thought is that the deadlocks are occasionally causing the database to loose all the values. I'm mainly looking for confirmation that this is indeed what would cause the data to disappear, but if there is a better way to implement this functionality, I'm all ears.
Your code causes a content node to be re-published on every view. Publishing is expensive, and as of v7 our data layer can deadlock under pressure.
We do have plans to fix those deadlocks, but, think of this: anytime you publish a content node, you create a new row in one database table (cmsDocument) and a few more rows in other database tables (cmsPropertyData...).
Even if the data layer was fixed and robust enough to handle the load, and the database server was fast enough, you would flood the database rapidly.
There is no built-in mechanism in Umbraco to count views. You would need to create your own database table and write some code to update that table on each view.
Deadlocks and disappearing content
At the beginning of the year I launched a site that needed a "Popular Articles" section for their blog. I searched around and the only suggestion I saw for this was to add a field to the page for viewCount and increment that on each page load. It worked like a charm. Now however, I'm getting regular errors about deadlocks, and at least three times have had content disappear from the back end.
The first time it happened I was looked through the logs but not very hard, hoping this was more of a fluke. Obviously there is a larger problem here, so I dug through several days worth of logs to pinpoint when it seemed to be that this particular node lost its content. Here is what I found:
TraceLog:
And here is part of the code from Artcle.cshtml:
My thought is that the deadlocks are occasionally causing the database to loose all the values. I'm mainly looking for confirmation that this is indeed what would cause the data to disappear, but if there is a better way to implement this functionality, I'm all ears.
Thanks,
Owen
Your code causes a content node to be re-published on every view. Publishing is expensive, and as of v7 our data layer can deadlock under pressure.
We do have plans to fix those deadlocks, but, think of this: anytime you publish a content node, you create a new row in one database table (cmsDocument) and a few more rows in other database tables (cmsPropertyData...).
Even if the data layer was fixed and robust enough to handle the load, and the database server was fast enough, you would flood the database rapidly.
There is no built-in mechanism in Umbraco to count views. You would need to create your own database table and write some code to update that table on each view.
is working on a reply...