Ticket (Solved)

Unable to modify ContentPart Programmtically?

Hi, I am trying to modify a contentpart programmatically. Part doesn't has any driver. I can see in the debugger that values are modified but I see they are not persisted in database. What could be the reason?

var conv = xyz;
var query = _contentManager.Query<MessagePart, MessagePartRecord>().ForVersion(VersionOptions.Latest);
var MessagePart = query.Where(x => x.ConversationId == conv.conversationId).FirstOrDefault();
                        if (MessagePart != null) {
                            MessagePart.ChatMode = conv.chatMode;
                            MessagePart.IsGroup = true;
                            _contentManager.Publish(MessagePart.ContentItem);
                        }

Re: Unable to modify ContentPart Programmtically?

Where are you doing that from? Do you see any trace of an exception that could be rolling back the transaction in the logs? Can you share the code for the part?

Wednesday, October 19, 2016 8:42:10 AM bybleroy
  • bleroy
  • Lv. 08 Rookie
  • Total EXP: 527

Re: Unable to modify ContentPart Programmtically?

Now I checked I am getting following exception. Is it necessary to call publish on contentitem after modifying the part or part is automatically saved if we assign any values?

2016-10-19 15:24:02,080 [22] NHibernate.Event.Default.AbstractFlushingEventListener - (null) - Could not synchronize database state with session [(null)] NHibernate.Exceptions.GenericADOException: could not update: [Orchard.Core.Common.Models.CommonPartRecord#860][SQL: UPDATE CommonCommonPartRecord SET OwnerId = ?, CreatedUtc = ?, PublishedUtc = ?, ModifiedUtc = ?, Containerid = ? WHERE Id = ?] ---> System.Data.SqlServerCe.SqlCeLockTimeoutException: SQL Server Compact timed out waiting for a lock. The default lock time is 2000ms for devices and 5000ms for desktops. The default lock timeout can be increased in the connection string using the ssce: default lock timeout property. [ Session id = 4,Thread id = 15580,Process id = 15624,Table name = OrchardFrameworkContentItemRecord,Conflict type = s lock (x blocks),Resource = RID: 1886:10 ] at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommandText(IntPtr& pCursor, Boolean& isBaseTableCursor) at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) 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) --- End of inner exception stack trace --- 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)

Wednesday, October 19, 2016 12:03:32 PM byPraggie
  • Praggie
  • Lv. 01 Rookie
  • Total EXP: 3

Re: Unable to modify ContentPart Programmtically?

Well, there's your issue. You probably have something leaking connections somewhere. You'll have to track that down, which is no fun.

Otherwise, no, you usually don't need to explicitly publish, except when you want to actually publish the item (from draft state, etc.): the changes should get persisted when the transaction is committed. In your case, it's rolled back because of that error.

Wednesday, October 19, 2016 5:44:53 PM bybleroy
  • bleroy
  • Lv. 08 Rookie
  • Total EXP: 527

Re: Unable to modify ContentPart Programmtically?

i am getting this exception only when i call _contentManager.Publish(MessagePart.ContentItem);

If i simply modify the contenpart, changes are not persisted in database. I am doing this modification in a service that is ISingletonDependency. I suspect this is causing the issue somehow.

How do I create a singleton service in Orchard instance of that is initialized after the orchard startup is complete?

Thursday, October 20, 2016 10:20:52 AM byPraggie
  • Praggie
  • Lv. 01 Rookie
  • Total EXP: 3

Re: Unable to modify ContentPart Programmtically?

Oh wow, yes, you should have mentioned that. Why do you need a singleton? Very few things need to be singletons in Orchard, and most things shouldn't.

The issue may be that a singleton will by definition not have its life bounded by the request. That means that it won't be enrolled in an ambient transaction, and instead you need to spin up and commit your own.

But chances are, it's a mistake to have a singleton in the first place.

Thursday, October 20, 2016 5:45:30 PM bybleroy
  • bleroy
  • Lv. 08 Rookie
  • Total EXP: 527

Post a reply

You need to be signed in to post a reply.

Sign In