First of all: guice-persist has some known bugs and they haven't been fixed for
a long time. It looks like the extension is no longer maintained. Have a look
into
http://onami.apache.org/persist/
It is concepionally build on guice persist and migrating existing code should
be very easy.
Regarding your problem:
The persist filter follows the j2ee recomendation which states you should not
start a new thread in a web request. Therefore it asumes that only the http
thread is used.
If you start your own worker threads then there are two possible scenarios.
1. each worker has a task which is independant of the others. It is ok if some
worker commit their work and others roll back.
2. all or some tasks are related and either everything is comitted or rolled
back.
Scenario 1. is the simple one. All you need to wrap your worker code in the
following snipped:
try {
unitOfWork.begin();
doActualJob();
} finally {
unitOfWork.end();
}
This is required since every thread which uses the entity manager must be in an
active unitOfWork. The persist filter does exactly the above for the http
thread.
If you happen to have scenario 2. it gets way more complicated. Because guice
persist and onami persist both expect only one thread to partisipate in a unit
of work. Therefore I will not go into details on how to implement this.
Ask on this mailing list and I can help you.
Am 13. Mai 2015 11:58:10 MESZ, schrieb jbl <[email protected]>:
>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 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/2C06949E-F2F3-44CE-831D-B3A2D10CE356%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.