-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
From sberlin on December 20, 2013 06:16:40 (No comment was entered for this change.) Labels: Component-Persist |
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. |
Can I go ahead and make a change like this.
in the class to resolve this problem. what other problems it might cause? this is the exact error I am seeing in my application: note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs. Apache Tomcat/7.0.52 |
I have exactly the same concern as @mikehearn has. 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? |
I also have exactly the same concern. I suppose that the |
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? |
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
Just hit this and wasted several hours trying to hunt it down. Would love to see that begin() statement nuked! |
…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
…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
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
The text was updated successfully, but these errors were encountered: