Expand my Community achievements bar.

SOLVED

Handling InvalidItemStateException

Avatar

Level 3

According to the doc the sesion's save() method can throw InvalidItemStateException exception.

It means that some item that was modified in current session also was modified from other thread and already saved. The transient space of the session cannot be saved

The quesion is what is a best practice in such case? Some code would be really appriciated. I cannot find any examples

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

Whenever you are performing some critical task, lets say scheduler will update few things, same time there can be some common java methods running, I would suggest you can implement design patterns example Singleton design pattern can give good results with session as it will never allow multiple objects to execute and modify.

Somewhat it also depends upon what exactly is the requirement. When ever you makes some changes you can use session.save() in case when you know that another thread is roaming around.

View solution in original post

3 Replies

Avatar

Employee

Hi,

my advice would be to find out why two threads are trying to modify the same resource. There may be an issue with the way your code base has been written, only then should you decide how to implement a solution in code. 

Is this for AEM5.x?

Regards,

Opkar

Avatar

Correct answer by
Level 10

Hi,

Whenever you are performing some critical task, lets say scheduler will update few things, same time there can be some common java methods running, I would suggest you can implement design patterns example Singleton design pattern can give good results with session as it will never allow multiple objects to execute and modify.

Somewhat it also depends upon what exactly is the requirement. When ever you makes some changes you can use session.save() in case when you know that another thread is roaming around.

Avatar

Employee Advisor

Hi,

if you get that exception, you can handle it programatically like this:

  • call session.refresh(true) and retry the session.save() again
  • in case it doesn't work, you can call session.refresh(false) to discard your changes, try to execute your operations and then call session.save().

kind regards,
Jörg