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

LazyInitializationException on REST services #67

Closed
jmiddleton opened this issue Dec 16, 2013 · 5 comments
Closed

LazyInitializationException on REST services #67

jmiddleton opened this issue Dec 16, 2013 · 5 comments

Comments

@jmiddleton
Copy link

Hi All,

Checking the code, I realised that REST services can throw LazyInitializationException when the code is trying to access a relationship. The transaction has been completed but the code is accessing a lazy relationship which requires a transaction.

To replicate the issue, in web.rest.AccountResource.getAccount(HttpServletResponse) I added the following line:

log.debug("User authorities size: " + user.getAuthorities().size());

Then, I logged in the app and in the log I saw the following exception:

[ERROR] ar.com.tunuyan.conf.DispatcherServletConfiguration - An error has occured: failed to lazily initialize a collection of role: ar.com.tunuyan.domain.User.authorities, could not initialize proxy - no Session
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ar.com.tunuyan.domain.User.authorities, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:137)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:156)
at ar.com.tunuyan.web.rest.AccountResource.getAccount(AccountResource.java:53)

@jdubois
Copy link
Member

jdubois commented Dec 16, 2013

This is "normal" with JPA, if you access a lazy relationship outside of a transaction.

Normally, you should use a service layer, with a transaction. For instance, the UserService class is perfect for doing this job.

Now, I understand this is a pitfall with JPA, so we should work on two possible solutions (outside of documenting the issue):

  • Using the Open Session In View pattern. This is not my favorite solution, as it is mostly a hack, and it's bad for scalability
  • Have a look at the extended persistence context pattern. I'm not sure we can make it work correctly with JHipster, but this would be my favorite solution. See the "PersistentContext.EXTENDED" attribute on the persistence context.

@jdubois
Copy link
Member

jdubois commented Dec 16, 2013

As I feared we can't use an extended persistence context with our stack.

So the only two options we have left is:

  • Using the open session in view pattern. I don't like it, as it hides the problem from the developer: he should use JPA correctly.
  • Letting people use JPA correctly: that's why I won't correct this issue. You have many ways to go around this problem, either by using eager relations or by using a transaction and a Spring service. That's how JPA is supposed to be used, and Spring controllers, in my opinion.

@jdubois jdubois closed this as completed Dec 16, 2013
@jmiddleton
Copy link
Author

Agree with you, I don't like open session view pattern coz some exceptions (db exceptions) are only detected when the transaction is committed which happens outside of the controller.

What about to provide an option during the generation of the app where the developer can automatically include or not a open session in view filter?

Thanks,
Jorge

@jdubois
Copy link
Member

jdubois commented Dec 17, 2013

This could be a solution: I don't want to have too many options when generating the app, but some options come and go depending on how we evolve. For me this is not a very high priority option, but I'll see that in a couple of versions (I think we will lose some options soon)

@jdubois jdubois modified the milestone: 0.0.1 Sep 10, 2014
@phstudy
Copy link
Contributor

phstudy commented Oct 19, 2014

In fact, this is a pitfall with Hibernate, not JPA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants