Hi Laszio, 

Thank you for your answer. 

I think you pinpointed the problem, and I'm a step forward in resolving it. 

Here is the MyRunnable implementation, it already relies on two providers for 
EntityManager and UnitOfWork.
But now my question is : how to make sure that the Provider<EntityManager> in 
the MyRunnable worker returns another EntityManager than the one in the 
MyServletWorker ? 




// A time consuming job. 
class MyRunnable implements Runnable {

  @Inject Provider<EntityManager> entityManagerProvider;
  
  @Inject Provider<UnitOfWork> unitOfWorkProvider;

  @Override
  public void run()
  {
    UnitOfWork unitOfWork = null;
    EntityManager em = null;
    try {
      unitOfWork = unitOfWorkProvider.get(); 
      unitOfWork.begin();
      em  = entityManagerProvider.get();
      this.doTimeConsumingStuff(em);
    } finally {
      if (unitOfWork != null) unitOfWork.end();
      if (em != null && em.isOpen()) em.close(); // should it be explicitly 
closed ?
    }
  }

  @Transactional
  private void doTimeConsumingStuff(EntityManager em) {/* ... */}
}


// Executed within the http worker thread.
// It Creates and submits the jobs.   
class MyServletWorker {

  @Inject Provider<EntityManager> entityManagerProvider;

  @Inject ExecutorService executorService;

  public void createAndSubmitJobs(MyData data) {
    EntityManager em = entityManagerProvider.get();
    Set<MyRunnable> jobs = createJobs(em, data);
    for (MyRunnable job : jobs)
      executorService.submit(job);
    // ....
  }

  @Transactional
  public Set<MyRunnable> createJobs(EntityManager em, MyData data) {/* ... */}
}


Le 13 mai 2015 à 13:16:59, Laszlo Ferenczi ([email protected]) a écrit:

Hi,

I think the issue will be in the MyRunnable class.
You create the MyRunnable instance in the http worker thread, if you simply 
inject an EntityManager there it'll be the one bound to the current thread (the 
http worker). After submitting it to a different thread you essentially leak 
the transaction which can have many side effects (like when the same http 
worker is selected again, the PersistFilter will try to start a new transaction 
but it's already open for that thread - held by the long running background 
job).

The correct way to handle this is to inject a Provider<EntityManager> in 
MyRunnable and annotate the database using methods with @Transactional.

--
L


--
L

On Wed, May 13, 2015 at 11:58 AM, jbl <[email protected]> wrote:
Hello, 


I'm relatively new to guice, and I've got a problem that I quite don't 
understand.

I have a webapp based on guice, eclipse link, mysql that runs on a tomcat 7.  
The app uses a com.google.inject.persist.jpa.JpaPersistModule and a 
com.google.inject.persist.PersistFilter

For some requests the webapp creates a set of java.lang.Runnable, passes them 
to a java.util.concurrent.ExecutorService, and returns.  The runnables are 
numerous time-consuming jobs that must be executed in background to avoid 
blocking the request. Also, each job has to access the EntityManager. However, 
the current implementation is quite naive: 

    @Inject private Provider<MyRunnable> myRunnableProvider;
    @Inject private ExecutorService executorService;
   
    MyRunnable runnable = myRunnableProvider.get();
    executorService.submit(runnable);

This code runs fine on my development computer, but I get this exception when I 
run it in a production environment : 

java.lang.IllegalStateException: Work already begun on this thread. Looks like 
you have called UnitOfWork.begin() twice without a balancing call to end() in 
between.
    at com.google.common.base.Preconditions.checkState(Preconditions.java:150) 
~[guava-15.0.jar:na]
    at 
com.google.inject.persist.jpa.JpaPersistService.begin(JpaPersistService.java:73)
 ~[guice-persist-4.0.jar:na]
    at com.jbl.MyRunnable.run(MyRunnable.java:107)

I think that this has something to do with the Scope, but I don't quite 
understand how to correct this.  

Thank you for any insight on this !

--
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/141ea4f0-3f34-45c9-a8cf-c28779859a2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google 
Groups "google-guice" group.
To unsubscribe from this topic, visit 
https://groups.google.com/d/topic/google-guice/JKPs9sayXi4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to 
[email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/CAD-udUCC6FM2w9wGHNdUcMyZD6FQykHfGpUW%3DNveLu7UTawPkw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

-- 

Jean-Baptiste LÉZORAY
Ingénieur Logiciel
Portable: 06 64 86 32 92
[email protected]
Bâtiment Nucléole - 85 rue de Saint-Brieuc - 35 000 RENNES
Retrouvez-nous sur www.dolmen.bzh

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/etPan.5553451b.66334873.5aa0%40MBA-JB.local.
For more options, visit https://groups.google.com/d/optout.

Reply via email to