@Inject private Provider<MyRunnable> myRunnableProvider;
@Inject private ExecutorService executorService;
MyRunnable runnable = myRunnableProvider.get();
executorService.submit(runnable);
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)
--
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 google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
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.
// 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) {/* ... */}
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 google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
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.
--
// A time consuming job.
class MyRunnable implements Runnable {
@Inject Provider<EntityManager> entityManagerProvider;
@Inject UnitOfWork unitOfWork;
@Override
public void run()
{
try {
unitOfWork.begin();
EntityManager em = entityManagerProvider.get();
this.doTimeConsumingStuff(em);
} finally {
unitOfWork.end();
}
}
@Transactional
public void doTimeConsumingStuff(EntityManager em) {/* ... */}
}
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/845FA7E2-4B79-4FB5-9A13-DB4CC2AFA230%40gmx.ch.