javax.jcr.InvalidItemStateException: Item cannot be saved - aem

I am getting following exception in a single box cq5 author environment.
javax.jcr.InvalidItemStateException: Item cannot be saved
because node property has been modified externally
more exception details:
Caused by: javax.jcr.InvalidItemStateException: Unable to update a stale item: item.save()
at org.apache.jackrabbit.core.ItemSaveOperation.perform(ItemSaveOperation.java:262)
at org.apache.jackrabbit.core.session.SessionState.perform(SessionState.java:216)
at org.apache.jackrabbit.core.ItemImpl.perform(ItemImpl.java:91)
at org.apache.jackrabbit.core.ItemImpl.save(ItemImpl.java:329)
at org.apache.jackrabbit.core.session.SessionSaveOperation.perform(SessionSaveOperation.java:65)
at org.apache.jackrabbit.core.session.SessionState.perform(SessionState.java:216)
at org.apache.jackrabbit.core.SessionImpl.perform(SessionImpl.java:361)
at org.apache.jackrabbit.core.SessionImpl.save(SessionImpl.java:812)
at com.day.crx.core.CRXSessionImpl.save(CRXSessionImpl.java:142)
at org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProvider.commit(JcrResourceProvider.java:511)
... 215 more
Caused by: org.apache.jackrabbit.core.state.StaleItemStateException: 3bec1cb7-9276-4bed-a24e-0f41bb3cf5b7/{}ssn has been modified externally
at org.apache.jackrabbit.core.state.SharedItemStateManager$Update.begin(SharedItemStateManager.java:679)
at org.apache.jackrabbit.core.state.SharedItemStateManager.beginUpdate(SharedItemStateManager.java:1507)
at org.apache.jackrabbit.core.state.SharedItemStateManager.update(SharedItemStateManager.java:1537)
at org.apache.jackrabbit.core.state.LocalItemStateManager.update(LocalItemStateManager.java:400)
at org.apache.jackrabbit.core.state.XAItemStateManager.update(XAItemStateManager.java:354)
at org.apache.jackrabbit.core.state.LocalItemStateManager.update(LocalItemStateManager.java:375)
at org.apache.jackrabbit.core.state.SessionItemStateManager.update(SessionItemStateManager.java:275)
at org.apache.jackrabbit.core.ItemSaveOperation.perform(ItemSaveOperation.java:258)
Here is the code sample:
adminResourceResolver = resourceResolverFactory
.getAdministrativeResourceResolver(null);
Resource fundPageResource = adminResourceResolver.getResource(page
.getPath() + "/jcr:content");
ModifiableValueMap homePageResourceProperties = fundPageResource
.adaptTo(ModifiableValueMap.class);
homePageResourceProperties.put("ssn",(person.getSsn());
adminResourceResolver.commit();
Any ideas ? It could be possible multiple threads accessing this code, as multiple authors on multiple pages calling this code from a authored component.
Thank you,
Sri

This is an error your see often in CQ5.5 (and lessens with each version upwards). The root cause of this issue is that multiple processes/services are modifying the same resource in roughly the same timespan (usually using different sessions, sometimes even with different users).
A small example to demonstrate perhaps. Session A and B both have a reference to Resource X. Session A modifies some properties on X, saves and commits, and is destroyed. This all goes smoothly. Session B still has a snapshot of the situation before A made modifications, session B makes modifications and all seems well UNTIL it tries to save. At this point, session B detects that it can't commit its changes because it doesn't have the latest node state. It has detected some other sessions made changes to the same node. In essence the current node state conflicts with modifications that session A has done and throws an ItemStale exception. The reason for this exception is the notion that the API doesn't know wether you want to keep the changes made by A, keep the changes made by the current session and discard the changes made by A, or merge them.
This error happens often with long running sessions and with workflow/listener combinations. Therefore the recommendation is to keep sessions as short as possible to prevent this kind of conflicts as much as possible.
One way to deal with this is to call session.refresh(keepChangesBoolean) before calling .save(). This instructs the current session to check for updates made by other sessions and deal with it according to the boolean flag you submit. This however is not a guarantee as it's still possible that between your refresh and your save call, yet another session has done the same. It only lowers the odds of this exception occurring.
Another way to deal with this is to retry again from scratch.

Related

how to run bpy callback on workspace tools change

How to add a pre-draw hook to current context workspace.tools change?
I attempted to get there using bpy.types.SpaceView3D.draw_handler_add(...) which as it runs on every draw, checks if workspace.tools changed, and if it changed, run my callback, but my callback wants to add its own SpaceView3D.draw_handler_add and doing it this way adds it a frame-too-late, leaving the view port undrawn until a user event repaints the screen.
I found this post online
https://devtalk.blender.org/t/update-property-when-active-tool-changes/11467/12
summary: maybe there is a mainline callback new
https://developer.blender.org/D10635
AFWS Jan '20
#kaio
This seem like a better solution. It’s kind of a mystery code, cause I
couldn’t figure out where you got that code info ,but then started
looking at the space_toolsystem_common.py file. kaio AFWS Jan '20
Just realized there might be a cleaner way of getting callbacks for
active tools using the msgbus. Since workspace tools aren’t rna
properties themselves, figured it’s possible to monitor the
bpy_prop_collection which changes with the tool.
The handle is the workspace itself, so shouldn’t have to worry about
keeping a reference. The subscription lasts until a new file is
loaded, so add a load_post callback which reapplies it.
Note this doesn’t proactively subscribe to workspaces added
afterwards. Might need a separate callback for that :joy:
import bpy
def rna_callback(workspace):
idname = workspace.tools[-1].idname
print(idname)
def subscribe(workspace):
bpy.msgbus.subscribe_rna(
key=ws.path_resolve("tools", False),
owner=workspace,
args=(workspace,),
notify=rna_callback)
if __name__ == "__main__":
ws = bpy.context.workspace
subscribe(bpy.context.workspace)
# Subscribe to all workspaces: if 0:
for ws in bpy.data.workspaces:
subscribe(bpy.context.workspace)
# Clear all workspace subscriptions if 0:
for ws in bpy.data.workspaces:
bpy.msgbus.clear_by_owner(ws)

Unexpected behavior from Drools' collectSet and difficulty effecting undo moves

What I actually wanted to do is to have a Drools rule as so:
rule "globalRequiredPredecessorAfterMe"
when
$rpAll: Set(size>1) from accumulate (
Customer(vehicle!= null, vehicle.vehicleTyp != VehicleTyp.DUMMY, $rpAfterMe: requiredPredecessorsAfterMe);
collectSet($rpAfterMe)
)
then
scoreHolder.addMediumConstraintMatch(kcontext, - $rpAll.size()-1);
end
Unfortunately, on the second move in CH drools awards me with a
Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
at org.drools.core.rule.SingleAccumulate.reverse(SingleAccumulate.java:124)
...
Is this a feature?
I swallowed my pride and to wrote a fantastically complex Listener which is supposed to to what the Drools rule above did. With an assertionScoreDirectorFactory (an EasyScoreCalculator) I get score corruption. On a closer look, I get a corruption in one of the two cases:
My planning entity's previousXXX is set exactly to the same value
An Undo move is done
To investigate, I set the solution to the state just before the score corruption occurs. I use the CreateChangeMoves from Optaplanner's 7.4.1.Final SolutionBusiness class. (On a side note, it would be really helpful if one could also set a MoveCountLimit in the SolverConfig.)
So I create the scoreDirector as follows
SolverFactory<MySolution> solverFactory = SolverFactory.createFromXmlResource(
SolverConfigXML);
solver = solverFactory.buildSolver();
ScoreDirectorFactory<MySolution> scoreDirectorFactory = solver.getScoreDirectorFactory();
scoreDirector = scoreDirectorFactory.buildScoreDirector();
scoreDirector.setWorkingSolution(unsolvedSolution);
within a JUnit 5 test and then perform several ChangeMoves to mimick the solution just before the offending move occurs. However, when I perform the offending move as in bullet 1 above,
// customer SUK0002030's previousVehicleOrCustomer is DUMMY_3
cm = createChangeMove(nameToCustomerMap.get("SUK0002030"), "previousVehicleOrCustomer", nameToVehicleMap.get("DUMMY_3"));
cm.doMove(scoreDirector);
I get a
java.lang.IllegalStateException: The entity (SUK0002030) has a variable (previousVehicleOrCustomer) with value (DUMMY_3) which has a sourceVariableName variable (nextCustomer) with a value (null) which is not that entity.
Verify the consistency of your input problem for that sourceVariableName variable.
at org.optaplanner.core.impl.domain.variable.inverserelation.SingletonInverseVariableListener.retract(SingletonInverseVariableListener.java:87)
...
When I set the previousVehicleOrCustomer to null, and then back to DUMMY_3, everything is fine and no score corruption occurs.
Similarly, when attempting to create an (offending) UndoMove of the previous Move cm, bullet 2 above, as so
cm.createUndoMove(scoreDirector).doMove(scoreDirector)
I get the same message:
java.lang.IllegalStateException: The entity (SUK0002014) has a variable (previousVehicleOrCustomer) with value (Vehicle_0) which has a sourceVariableName variable (nextCustomer) with a value (null) which is not that entity.
Verify the consistency of your input problem for that sourceVariableName variable.
at org.optaplanner.core.impl.domain.variable.inverserelation.SingletonInverseVariableListener.retract(SingletonInverseVariableListener.java:87)
...
Of course, if I manually create the UndoMove (by simply setting the previousVehicleOrCustomer back to null), everything is fine.
These moves really should be possible and I'm really curious to find out what's wrong.

Moving from file-based tracing session to real time session

I need to log trace events during boot so I configure an AutoLogger with all the required providers. But when my service/process starts I want to switch to real-time mode so that the file doesn't explode.
I'm using TraceEvent and I can't figure out how to do this move correctly and atomically.
The first thing I tried:
const int timeToWait = 5000;
using (var tes = new TraceEventSession("TEMPSESSIONNAME", #"c:\temp\TEMPSESSIONNAME.etl") { StopOnDispose = false })
{
tes.EnableProvider(ProviderExtensions.ProviderName<MicrosoftWindowsKernelProcess>());
Thread.Sleep(timeToWait);
}
using (var tes = new TraceEventSession("TEMPSESSIONNAME", TraceEventSessionOptions.Attach))
{
Thread.Sleep(timeToWait);
tes.SetFileName(null);
Thread.Sleep(timeToWait);
Console.WriteLine("Done");
}
Here I wanted to make that I can transfer the session to real-time mode. But instead, the file I got contained events from a 15s period instead of just 10s.
The same happens if I use new TraceEventSession("TEMPSESSIONNAME", #"c:\temp\TEMPSESSIONNAME.etl", TraceEventSessionOptions.Create) instead.
It seems that the following will cause the file to stop being written to:
using (var tes = new TraceEventSession("TEMPSESSIONNAME"))
{
tes.EnableProvider(ProviderExtensions.ProviderName<MicrosoftWindowsKernelProcess>());
Thread.Sleep(timeToWait);
}
But here I must reenable all the providers and according to the documentation "if the session already existed it is closed and reopened (thus orphans are cleaned up on next use)". I don't understand the last part about orphans. Obviously some events might occur in the time between closing, opening and subscribing on the events. Does this mean I will lose these events or will I get the later?
I also found the following in the documentation of the library:
In real time mode, events are buffered and there is at least a second or so delay (typically 3 sec) between the firing of the event and the reception by the session (to allow events to be delivered in efficient clumps of many events)
Does this make the above code alright (well, unless the improbable happens and for some reason my thread is delayed for more than a second between creating the real-time session and starting processing the events)?
I could close the session and create a new different one but then I think I'd miss some events. Or I could open a new session and then close the file-based one but then I might get duplicate events.
I couldn't find online any examples of moving from a file-based trace to a real-time trace.
I managed to contact the author of TraceEvent and this is the answer I got:
Re the exception of the 'auto-closing and restarting' feature, it is really questions about the OS (TraceEvent simply calls the underlying OS API). Just FYI, the deal about orphans is that it is EASY for your process to exit but leave a session going. This MAY be what you want, but often it is not, and so to make the common case 'just work' if you do Create (which is the default), it will close a session if it already existed (since you asked for a new one).
Experimentation of course is the touchstone of 'truth' but I would frankly expecting unusual combinations to just work is generally NOT true.
My recommendation is to keep it simple. You need to open a new session and close the original one. Yes, you will end up with duplicates, but you CAN filter them out (after all they are IDENTICAL timestamps).
The other possibility is use SetFileName in its intended way (from one file to another). This certainly solves your problem of file size growth, and often is a good way to deal with other scenarios (after all you can start up you processing and start deleting files even as new files are being generated).

GWTP Invalid attempt to reveal errorplace, but then works normally

I have a couple of places set up, and they work correctly, except with a delay caused by this issue. They're using nested presenters.
For one place, it appears that any repeat attempt to load it causes an infinite loop of reveal error / unauthorized place (no idea why, no gatekeeper set), but then loads the page correctly. The issue I have with it is the delay and unnecessary log spam it causes - it loads the page correctly, why can't it do it without going through the loop first? Anyone have any ideas?
-- UPDATE --
I am using GWTP 1.4 with GWT 2.7.0, but the project was first created using GWTP 0.6 or maybe earlier. We've updated deprecation etc as we've upgraded, but I know there are anachronisms left.
I tried switching out our ClientPlaceManager with the default, bound the ErrorPlace and UnauthorizedPlace to our home page, and removed its gatekeeper, but it still tries to go to the error place (overrode the revealErrorPlace method and noticed it's throwing the error for a valid token that had been loaded at least once already that session. One page in particular, none of the presenter lifecycle phases are firing, though the presenter is visible (only breaking in firefox I think). I really don't understand it.
-- UPDATE 2 --
I've removed gatekeepers (even specifying #NoGatekeeper), have ensured that the error / unauthorized place have #NoGatekeeper and exists, and overrode revealPlace(request, updateUrl) to output results, and added a try/catch - and it does the exact same thing. An infinite loop, but everything is accessible. my debug output even shows it attempting to reveal the error place, but it never does, just errors out.
This is frustrating to no end.
Stacktrace:
SEVERE: Exception caught: Encountered repeated errors resulting in an infinite
loop. Make sure all users have access to the pages revealed by revealErrorPlace
and revealUnauthorizedPlace. (Note that the default implementations call
revealDefaultPlace)
com.google.gwt.event.shared.UmbrellaException: Exception caught:
Encountered repeated errors resulting in an infinite loop. Make sure all users
have access to the pages revealed by revealErrorPlace and
revealUnauthorizedPlace. (Note that the default implementations call
revealDefaultPlace)
at Unknown.fillInStackTrace_0_g$(student-0.js#36:10580)
at Unknown.Throwable_3_g$(student-0.js#8:10535)
at Unknown.Exception_3_g$(student-0.js#18:10678)
at Unknown.RuntimeException_3_g$(student-0.js#18:61481)
at Unknown.UmbrellaException_3_g$(student-0.js#25:133542)
at Unknown.UmbrellaException_5_g$(student-0.js#26:133603)
at Unknown.fireEvent_7_g$(student-0.js#13:133134)
at Unknown.fireEvent_12_g$(student-0.js#22:154354)
at Unknown.fire_8_g$(student-0.js#17:132936)
at Unknown.fireValueChangedEvent_0_g$(student-0.js#3:154358)
at Unknown.onHashChanged_0_g$(student-0.js#29:154297)
at Unknown.apply_0_g$(student-0.js#28:109006)
at Unknown.entry0_0_g$(student-0.js#16:109062)
at Unknown.anonymous(student-0.js#14:109042)
Caused by: java.lang.RuntimeException: Encountered repeated errors resulting in
an infinite loop. Make sure all users have access to the pages revealed by
revealErrorPlace and revealUnauthorizedPlace. (Note that the default
implementations call revealDefaultPlace)
at Unknown.fillInStackTrace_0_g$(student-0.js#36:10580)
at Unknown.Throwable_2_g$(student-0.js#8:10526)
at Unknown.Exception_2_g$(student-0.js#18:10672)
at Unknown.RuntimeException_2_g$(student-0.js#18:61475)
at Unknown.startError_0_g$(student-0.js#11:92009)
at Unknown.error_2_g$(student-0.js#8:91772)
at Unknown.doRevealPlace_0_g$(student-0.js#10:91762)
at Unknown.revealPlace_1_g$(student-0.js#8:91921)
at Unknown.revealPlace_0_g$(student-0.js#8:91908)
at Unknown.revealErrorPlace_1_g$(student-0.js#8:92109)
at Unknown.error_2_g$(student-0.js#8:91773)
at Unknown.doRevealPlace_0_g$(student-0.js#10:91762)
at Unknown.handleTokenChange_0_g$(student-0.js#12:91848)
at Unknown.onValueChange_4_g$(student-0.js#8:91888)
at Unknown.dispatch_87_g$(student-0.js#16:132968)
at Unknown.dispatch_88_g$(student-0.js#8:132972)
at Unknown.dispatch_0_g$(student-0.js#8:49973)
at Unknown.dispatchEvent_2_g$(student-0.js#14:133006)
at Unknown.doFire_0_g$(student-0.js#9:133250)
at Unknown.fireEvent_8_g$(student-0.js#8:133323)
at Unknown.fireEvent_7_g$(student-0.js#25:133128)
at Unknown.fireEvent_12_g$(student-0.js#22:154354)
at Unknown.fire_8_g$(student-0.js#17:132936)
at Unknown.fireValueChangedEvent_0_g$(student-0.js#3:154358)
at Unknown.onHashChanged_0_g$(student-0.js#29:154297)
at Unknown.apply_0_g$(student-0.js#28:109006)
at Unknown.entry0_0_g$(student-0.js#16:109062)
at Unknown.anonymous(student-0.js#14:109042)
If you're using the DefaultPlaceManager, make sure you have bound DefaultPlace, ErrorPlace and UnauthorizedPlace to Presenter name tokens in your Gin module.
From DefaultPlaceManager's javadoc (http://arcbees.github.io/GWTP/javadoc/apidocs/com/gwtplatform/mvp/client/proxy/DefaultPlaceManager.html):
Important! If you use this class, don't forget to bind DefaultPlace,
ErrorPlace and UnauthorizedPlace to Presenter name tokens in your Gin
module.
Note: The default, error and unauthorized places are revealed without
updating the browser's URL (hence the false value passed in
revealPlace). This will avoid stepping into an infinite navigation
loop if the user navigates back (using the browser's back button).
Here's an example of infinite navigation loop that we want to avoid:
An unauthenticated hits #admin (a place reserved to authenticated
admins) The #unauthorized place is revealed, and the browser's URL is
updated to #unauthorized The user clicks the back button in his
browser, lands in #admin, then #unauthorized, then #admin, and so on.
Also, from https://github.com/ArcBees/GWTP/issues/296:
Verify that the Interface of the Proxy in your Presenter inherit from
ProxyPlace.

WF4 InstancePersistenceCommand interrupted

I have a windows service, running workflows. The workflows are XAMLs loaded from database (users can define their own workflows using a rehosted designer). It is configured with one instance of the SQLWorkflowInstanceStore, to persist workflows when becoming idle. (It's basically derived from the example code in \ControllingWorkflowApplications from Microsoft's WCF/WF samples).
But sometimes I get an error like below:
System.Runtime.DurableInstancing.InstanceOwnerException: The execution of an InstancePersistenceCommand was interrupted because the instance owner registration for owner ID 'a426269a-be53-44e1-8580-4d0c396842e8' has become invalid. This error indicates that the in-memory copy of all instances locked by this owner have become stale and should be discarded, along with the InstanceHandles. Typically, this error is best handled by restarting the host.
I've been trying to find the cause, but it is hard to reproduce in development, on production servers however, I get it once in a while. One hint I found : when I look at the LockOwnersTable, I find the LockOnwersTable lockexpiration is set to 01/01/2000 0:0:0 and it's not getting updated anymore, while under normal circumstances the should be updated every x seconds according to the Host Lock Renewal period...
So , why whould SQLWorkflowInstanceStore stop renewing this LockExpiration and how can I detect the cause of it?
This happens because there are procedures running in the background and trying to extend the lock of the instance store every 30 seconds, and it seems that once the connection fail connecting to the SQL service it will mark this instance store as invalid.
you can see the same behaviour if you delete the instance store record from [LockOwnersTable] table.
The proposed solution is when this exception fires, you need to free the old instance store and initialize a new one
public class WorkflowInstanceStore : IWorkflowInstanceStore, IDisposable
{
public WorkflowInstanceStore(string connectionString)
{
_instanceStore = new SqlWorkflowInstanceStore(connectionString);
InstanceHandle handle = _instanceStore.CreateInstanceHandle();
InstanceView view = _instanceStore.Execute(handle,
new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
handle.Free();
_instanceStore.DefaultInstanceOwner = view.InstanceOwner;
}
public InstanceStore Store
{
get { return _instanceStore; }
}
public void Dispose()
{
if (null != _instanceStore)
{
var deleteOwner = new DeleteWorkflowOwnerCommand();
InstanceHandle handle = _instanceStore.CreateInstanceHandle();
_instanceStore.Execute(handle, deleteOwner, TimeSpan.FromSeconds(10));
handle.Free();
}
}
private InstanceStore _instanceStore;
}
you can find the best practices to create instance store handle in this link
Workflow Instance Store Best practices
This is an old thread but I just stumbled on the same issue.
Damir's Corner suggests to check if the instance handle is still valid before calling the instance store. I hereby quote the whole post:
Certain aspects of Workflow Foundation are still poorly documented; the persistence framework being one of them. The following snippet is typically used for setting up the instance store:
var instanceStore = new SqlWorkflowInstanceStore(connectionString);
instanceStore.HostLockRenewalPeriod = TimeSpan.FromSeconds(30);
var instanceHandle = instanceStore.CreateInstanceHandle();
var view = instanceStore.Execute(instanceHandle,
new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(10));
instanceStore.DefaultInstanceOwner = view.InstanceOwner;
It's difficult to find a detailed explanation of what all of this
does; and to be honest, usually it's not necessary. At least not,
until you start encountering problems, such as InstanceOwnerException:
The execution of an InstancePersistenceCommand was interrupted because
the instance owner registration for owner ID
'9938cd6d-a9cb-49ad-a492-7c087dcc93af' has become invalid. This error
indicates that the in-memory copy of all instances locked by this
owner have become stale and should be discarded, along with the
InstanceHandles. Typically, this error is best handled by restarting
the host.
The error is closely related to the HostLockRenewalPeriod property
which defines how long obtained instance handle is valid without being
renewed. If you try monitoring the database while an instance store
with a valid instance handle is instantiated, you will notice
[System.Activities.DurableInstancing].[ExtendLock] being called
periodically. This stored procedure is responsible for renewing the
handle. If for some reason it fails to be called within the specified
HostLockRenewalPeriod, the above mentioned exception will be thrown
when attempting to persist a workflow. A typical reason for this would
be temporarily inaccessible database due to maintenance or networking
problems. It's not something that happens often, but it's bound to
happen if you have a long living instance store, e.g. in a constantly
running workflow host, such as a Windows service.
Fortunately it's not all that difficult to fix the problem, once you
know the cause of it. Before using the instance store you should
always check, if the handle is still valid; and renew it, if it's not:
if (!instanceHandle.IsValid)
{
instanceHandle = instanceStore.CreateInstanceHandle();
var view = instanceStore.Execute(instanceHandle,
new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(10));
instanceStore.DefaultInstanceOwner = view.InstanceOwner;
}
It's definitely less invasive than the restart of the host, suggested
by the error message.
you have to be sure about expiration of owner user
here how I am used to handle this issue
public SqlWorkflowInstanceStore SetupSqlpersistenceStore()
{
SqlWorkflowInstanceStore sqlWFInstanceStore = new SqlWorkflowInstanceStore(ConfigurationManager.ConnectionStrings["DB_WWFConnectionString"].ConnectionString);
sqlWFInstanceStore.InstanceCompletionAction = InstanceCompletionAction.DeleteAll;
InstanceHandle handle = sqlWFInstanceStore.CreateInstanceHandle();
InstanceView view = sqlWFInstanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
handle.Free();
sqlWFInstanceStore.DefaultInstanceOwner = view.InstanceOwner;
return sqlWFInstanceStore;
}
and here how you can use this method
wfApp.InstanceStore = SetupSqlpersistenceStore();
wish this help