You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Hitesh Fulchand Jaiswar <hi...@csinfocomm.com> on 2022/10/13 08:43:41 UTC

Can anyone help to run my first' code in struts2

Hii

Can anyone help to run my first' code in struts2
My code is perfect but it's not working properly i don't know why


HITESH jaiswar

On 13-Oct-2022 12:56 pm, Lukasz Lenart <lu...@apache.org> wrote:
You must only re-implement OpenSessionBackgroundProcess as follow:

public class OpenSessionBackgroundProcess extends StrutsBackgroundProcess {

    private final EntityManagerFactory entityManagerFactory;

    // used for synchronization
    private final Object lock = new Object();

    public OpenSessionBackgroundProcess(String name, ActionInvocation
invocation, int threadPriority, EntityManagerFactory
entityManagerFactory) {
        super(invocation, name, threadPriority);
        this.entityManagerFactory = entityManagerFactory;
    }

    protected void beforeInvocation() throws Exception {
        synchronized (lock) {
            EntityManager em = entityManagerFactory.createEntityManager();
            TransactionSynchronizationManager.bindResource(entityManagerFactory,
new EntityManagerHolder(em));
            super.beforeInvocation();
        }
    }

    protected void afterInvocation() throws Exception {
        synchronized (lock) {
            super.afterInvocation();
            EntityManagerHolder emHolder = (EntityManagerHolder)
TransactionSynchronizationManager.unbindResource(entityManagerFactory);
            EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
        }
    }

}

I would assume even the lock is not needed but I have no way to test
it. And if you could test 6.1.0-SNAPSHOT with this I would be very
thankful :)
I want to release 6.1.0 ASAP but I would like also resolve the problem
you have reported with includeParams="all'


Cheers
Łukasz


śr., 5 paź 2022 o 15:48 Burton Rhodes <bu...@gmail.com> napisał(a):
>
> Wow - very cool. When do you think this patch will be released? And
> would you be able to provide a brief code snippet of how to use/extend
> this new class? My current setup is below which uses an
> ExecuteAndWaitInterceptor to create a class that extends the
> BackgroundProcess class.
>
> Again thanks for all your work on this.
>
>
> @Component
> public class OpenSessionExecuteAndWaitInterceptor extends
> ExecuteAndWaitInterceptor {
>
>      @Autowired
>      private transient EntityManagerFactory entityManagerFactory;
>
>      @Override
>      protected BackgroundProcess getNewBackgroundProcess(String arg0,
> ActionInvocation arg1, int arg2) {
>          return new OpenSessionBackgroundProcess(arg0, arg1, arg2,
> entityManagerFactory);
>      }
> }
>
>
> public class OpenSessionBackgroundProcess extends BackgroundProcess
> implements Serializable {
>
>      private static final long serialVersionUID = 1L;
>
>      private final transient EntityManagerFactory entityManagerFactory;
>
>      // used for synchronization
>      protected boolean initializationComplete;
>      private transient Object lock = new Object();
>
>      public OpenSessionBackgroundProcess(String name, ActionInvocation
> invocation, int threadPriority, EntityManagerFactory
> entityManagerFactory) {
>          super(name, invocation, threadPriority);
>          this.entityManagerFactory = entityManagerFactory;
>          initializationComplete = true;
>          synchronized (lock) {
>              lock.notify();
>          }
>      }
>
>      protected void beforeInvocation() throws Exception {
>          while (!initializationComplete) {
>              try {
>                  synchronized (lock) {
>                      lock.wait(100);
>                  }
>              } catch (InterruptedException e) {
>                  // behavior ignores cause of re-awakening.
>              }
>          }
>          EntityManager em = entityManagerFactory.createEntityManager();
>
> TransactionSynchronizationManager.bindResource(entityManagerFactory, new
> EntityManagerHolder(em));
>          super.beforeInvocation();
>      }
>
>      protected void afterInvocation() throws Exception {
>          super.afterInvocation();
>          EntityManagerHolder emHolder = (EntityManagerHolder)
>
> TransactionSynchronizationManager.unbindResource(entityManagerFactory);
>
> EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
>      }
>
>      /**
>       * Override default readObject() method when deserializing
>       *
>       * @param serialized the serialized object
>       */
>      private void readObject(ObjectInputStream serialized) throws
> IOException, ClassNotFoundException {
>          serialized.defaultReadObject();
>          lock = new Object();
>      }
> }
>
> ------ Original Message ------
> From "Lukasz Lenart" <lu...@apache.org>
> To "Struts Users Mailing List" <us...@struts.apache.org>
> Date 10/5/2022 2:12:20 AM
> Subject Re: Race condition in recommended "OpenSessionBackProccess"
> class
>
> >I refactored the BackgroundProcess and it should solve your problem,
> >yet it will require some coding.
> >https://github.com/apache/struts/pull/609
> >
> >niedz., 2 paź 2022 o 13:09 Lukasz Lenart <lu...@apache.org> napisał(a):
> >>
> >>śr., 28 wrz 2022 o 16:15 Burton Rhodes <bu...@gmail.com> napisał(a):
> >> >
> >> > Lukasz -
> >> > I have queried you on this before, but I am trying to get to the bottom of
> >> > why the class Dale Newfield recommended (many years ago) to solve the Open
> >> > Session during ExecuteAndWait problem gives me issues from time to time
> >> > [1]. Recently (as our user base grows) I am getting NPEs more and more on
> >> > the synchronized "lock" variable. I posed the question to StackExchange
> >> > [2], and one contributor suggested there is a race condition by extending
> >> > the BackgroundProcess class.
> >> >
> >> > Full disclosure, this kind of problem starts to get over my head, so I am
> >> > wondering if you might have some guidance on how I can once and for all fix
> >> > this issue.
> >> >
> >> > As always, thanks in advance.
> >> >
> >> > [1]
> >> > https://cwiki.apache.org/confluence/display/WW/HibernateAndSpringEnabledExecuteAndWaitInterceptor
> >> >
> >> > [2]
> >> > https://stackoverflow.com/questions/73858251/how-to-properly-use-a-lock-variable-for-synchronization-in-a-serializable-clas?noredirect=1#comment130421653_73858251
> >>
> >>As far as I understand the answer on SO, we must change how the
> >>BackgroundProcess is initialised - instead of using a constructor
> >>based logic, move it into a method or so.
> >>Would you mind registering a ticket in JIRA with the above description?
> >>
> >>
> >>Regards
> >>--
> >>Łukasz
> >>+ 48 606 323 122 http://www.lenart.org.pl/
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >For additional commands, e-mail: user-help@struts.apache.org
> >

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org



Re: Can anyone help to run my first' code in struts2

Posted by Lukasz Lenart <lu...@apache.org>.
czw., 13 paź 2022 o 10:44 Hitesh Fulchand Jaiswar
<hi...@csinfocomm.com> napisał(a):
>
> Hii
>
> Can anyone help to run my first' code in struts2
> My code is perfect but it's not working properly i don't know why

Sure, but please start a new thread with additional info instead of
mixing it into a different topic.


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org