Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guice-persist: Injection of EntityManager outside a UnitOfWork can lead to never closed EntityManagers #739

Closed
gissuebot opened this issue Jul 7, 2014 · 7 comments

Comments

@gissuebot
Copy link

From head@anthologique.net on February 02, 2013 05:40:00

Form the commit rb7a02b02d81c (Jul 7, 2011), in JpaPersistService.java which is a Provider<EntityManager>, in the get() method, we can see that if we don't already have an EntityManager stored in the thread local variable, we call the begin() method. This method will create a new EntityManager, and store it in the thread local variable.

So, if we follow the documentation in the wiki (page "Using JPA", section "Using the EntityManager inside transactions"), for each thread, for every other action needing an EntityManager, the same instance will always be returned, and this EntityManager will never be closed.

This can lead to broken threads, as if for any reason the EntityManager can no longer be used (connection closed, etc.), there is no way to replace it.

Note that this issue doesn't happen if the injection of EntityManager is done inside an opened UnitOfWork or inside a method annotated with @Transactional. In these cases, the EntityManager is already created, and will be destroyed at the end of the transaction, or on UnitOfWork close.

While taking a look at the get() method of JpaPersistService, we can also see that there is a strange precondition test. As said, if the EntityManager doesn't exist, it's created. Juste after that, we check if we have an EntityManager. If we don't have any, we throw this error message: "Requested EntityManager outside work unit.". Sounds like a bug, we should either remove the Precondition test, or remove the begin() call if the EntityManager doesn't exist.

So, my suggestion would be either to fix the get() method, or change the documentation to tell about this potential issue, and suggest the injection of Provider<EntityManager> in every cases.

Original issue: http://code.google.com/p/google-guice/issues/detail?id=739

@gissuebot
Copy link
Author

From sberlin on December 20, 2013 06:16:40

(No comment was entered for this change.)

Labels: Component-Persist

@mikehearn
Copy link

I ran into this - the silent creation of EntityManagers outside a UnitOfWork scope due to the implicit get turned out to be hiding a bug where I was doing db access outside a UnitOfWork. It showed up as a failure some time later when the thread got reused and I got the "maybe you're not matching with a call to end" message.

@singavarapu
Copy link

Can I go ahead and make a change like this.
public void begin() {

    if(null == entityManager.get()){
        entityManager.set(emFactory.createEntityManager());
    }
}

in the class
com.google.inject.persist.jpa.JpaPersistService

to resolve this problem. what other problems it might cause?

this is the exact error I am seeing in my application:
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. com.google.inject.internal.util.$Preconditions.checkState(Preconditions.java:142) com.google.inject.persist.jpa.JpaPersistService.begin(JpaPersistService.java:66) com.google.inject.persist.PersistFilter.doFilter(PersistFilter.java:87) com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163) com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58) com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118) com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs.


Apache Tomcat/7.0.52

@asharayev
Copy link

I have exactly the same concern as @mikehearn has.
The problem is that in the get() method of JpaPersistService there is actually the check we want:
see lines 53-56:

    EntityManager em = entityManager.get();
    Preconditions.checkState(null != em, "Requested EntityManager outside work unit. "
        + "Try calling UnitOfWork.begin() first, or use a PersistFilter if you "
        + "are inside a servlet environment.");

But, two lines up there is another code that makes the checkState() call ridiculous:

    if (!isWorking()) {
      begin();
    }

I see that this is definitely a bug. Could it be fixed?

krdlab added a commit to krdlab/guice that referenced this issue Jan 9, 2016
@krdlab
Copy link

krdlab commented Jan 9, 2016

I also have exactly the same concern. I suppose that the get() should not call the begin() implicitly.

@asharayev
Copy link

It seems that you did exactly what I was going to do? Maybe you can create a pull request for this issue, so that somebody from moderators will take a closer look at it?

jjantunen added a commit to jjantunen/guice that referenced this issue Jan 27, 2016
This modification continues work of Andreas Viedma
(andresviedma:unitofwork-annotation).

Current solution of the EntityManager doesn't robustly support use of
Transaction scoped EntityManager.If EntityManager is injected or
emProvider.get() is called outside of the Transactional annotation,
then this will lead to never closed EntityManager. (Current solution
would need boilerplate UnitOfWork.end() calls). This behaviour is also
different than in Guice 2.0 with warp-persist, where Transaction scoped
EntityManager works.

With this commit UnitOfWork and EntityManager are separated.
This means that getting of EntityManager doesn't start UnitOfWork.
If EntityManager scope needs to be different than transaction,
UnitOfWork has to be explicitly started.UnitOfWork can be started
with annotation or explicitly calling begin(). Annotation supports
nested UnitOfWorks. If begin is called directly from UnitOfWork then
it requires direct call to end() to end UnitOfWork.End call will always
end UnitOfWork.

Resolves issues: google#739, google#947
@stickfigure
Copy link

Just hit this and wasted several hours trying to hunt it down. Would love to see that begin() statement nuked!

copybara-service bot pushed a commit that referenced this issue Apr 21, 2023
…because it leads to leaks. Fixes #739, fixes #997, fixes #730, fixes #985, fixes #959, fixes #731. This also adds an optional Options to the JpaPersistModule constructor, if users wish to use the legacy behavior. This is a breaking change, but to a better default value. Users who want to keep the dangerous form can opt back in with the options.

PiperOrigin-RevId: 525852009
copybara-service bot pushed a commit that referenced this issue Apr 21, 2023
…because it leads to leaks. Fixes #739, fixes #997, fixes #730, fixes #985, fixes #959, fixes #731. This also adds an optional Options to the JpaPersistModule constructor, if users wish to use the legacy behavior. This is a breaking change, but to a better default value. Users who want to keep the dangerous form can opt back in with the options.

PiperOrigin-RevId: 525852009
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment