org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.dspace.content.DSpaceObject.handles, could not initialize proxy - no Session

Sakai JIRA | Terry Brady | 4 months ago
  1. 0

    h2. Context See the discussion in https://github.com/DSpace/DSpace/pull/1469 The following code can produce an exception while walking the community/collection hierarchy. My worry is that this is evidence a potential bug that could be lurking in other parts of the system. For that reason, I marked the issue as Major. h2. Original Code {code:java} private static long indexAll(final IndexingService indexingService, final ItemService itemService, Context context, DSpaceObject dso) throws IOException, SearchServiceException, SQLException { long count = 0; indexingService.indexContent(context, dso, true, true); count++; if (dso.getType() == Constants.COMMUNITY) { Community community = (Community) dso; //final String communityHandle = community.getHandle(); for (Community subcommunity : community.getSubcommunities()) { count += indexAll(indexingService, itemService, context, subcommunity); } //final Community reloadedCommunity = (Community) HandleServiceFactory.getInstance().getHandleService().resolveToObject(context, communityHandle); for (Collection collection : community.getCollections()) { count++; indexingService.indexContent(context, collection, true, true); count += indexItems(indexingService, itemService, context, collection); } } else if (dso.getType() == Constants.COLLECTION) { count += indexItems(indexingService, itemService, context, (Collection) dso); } return count; } {code} h2. Exception Text {quote}Exception: failed to lazily initialize a collection of role: org.dspace.content.DSpaceObject.handles, could not initialize proxy - no Session org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.dspace.content.DSpaceObject.handles, could not initialize proxy - no Session at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:569) at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:188) at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:139) at org.hibernate.collection.internal.PersistentBag.isEmpty(PersistentBag.java:249) at org.apache.commons.collections.CollectionUtils.isEmpty(CollectionUtils.java:979) at org.apache.commons.collections.CollectionUtils.isNotEmpty(CollectionUtils.java:992) at org.dspace.content.DSpaceObject.getHandle(DSpaceObject.java:133) at org.dspace.discovery.SolrServiceImpl.indexContent(SolrServiceImpl.java:198) at org.dspace.discovery.SolrServiceImpl.indexContent(SolrServiceImpl.java:2305) at org.dspace.discovery.IndexClient.indexAll(IndexClient.java:192) at org.dspace.discovery.IndexClient.main(IndexClient.java:152) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.dspace.app.launcher.ScriptLauncher.runOneCommand(ScriptLauncher.java:229) at org.dspace.app.launcher.ScriptLauncher.main(ScriptLauncher.java:81) {quote} h2. The issue is resolved by explicitly reloading an object by ID. {code} private static long indexAll(final IndexingService indexingService, final ItemService itemService, Context context, DSpaceObject dso) throws IOException, SearchServiceException, SQLException { long count = 0; indexingService.indexContent(context, dso, true, true); count++; if (dso.getType() == Constants.COMMUNITY) { Community community = (Community) dso; //final String communityHandle = community.getHandle(); for (Community subcommunity : community.getSubcommunities()) { subcommunity = ContentServiceFactory.getInstance().getCommunityService().find(context, subcommunity.getID()); count += indexAll(indexingService, itemService, context, subcommunity); } //final Community reloadedCommunity = (Community) HandleServiceFactory.getInstance().getHandleService().resolveToObject(context, communityHandle); for (Collection collection : community.getCollections()) { collection = ContentServiceFactory.getInstance().getCollectionService().find(context, collection.getID()); count++; indexingService.indexContent(context, collection, true, true); count += indexItems(indexingService, itemService, context, collection); } } else if (dso.getType() == Constants.COLLECTION) { count += indexItems(indexingService, itemService, context, (Collection) dso); } return count; }{code}

    Sakai JIRA | 4 months ago | Terry Brady
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.dspace.content.DSpaceObject.handles, could not initialize proxy - no Session
  2. 0

    Indexing of ProtocolTable fails when started from 'index datasets' panel of the 'admin' menu

    GitHub | 3 years ago | erwinwinder
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.molgenis.omx.observ.Protocol.subprotocols, could not initialize proxy - no Session
  3. 0

    Fetch data from table using hibernate with fetch=FetchType.LAZY

    Stack Overflow | 4 years ago | blackhard
    org.hibernate.LazyInitializationException: could not initialize proxy - no Session
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    LazyInitializationException: failed to lazily initialize a collection of role: Usuario.autorizacoes, could not initialize proxy - no Session

    Stack Overflow | 3 years ago | Kleber Mota
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.horariolivre.entity.Usuario.autorizacoes, could not initialize proxy - no Session
  6. 0

    Failed to lazily initialize a collection, no session or session was closed (despite eagerly fetching)

    Stack Overflow | 4 years ago | user2090747
    javax.faces.FacesException: #{commissionController.saveCommission}: javax.persistence.PersistenceException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. org.hibernate.LazyInitializationException

    failed to lazily initialize a collection of role: org.dspace.content.DSpaceObject.handles, could not initialize proxy - no Session

    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException()
  2. Hibernate
    PersistentBag.isEmpty
    1. org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:569)
    2. org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:188)
    3. org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:139)
    4. org.hibernate.collection.internal.PersistentBag.isEmpty(PersistentBag.java:249)
    4 frames
  3. Commons Collections
    CollectionUtils.isNotEmpty
    1. org.apache.commons.collections.CollectionUtils.isEmpty(CollectionUtils.java:979)
    2. org.apache.commons.collections.CollectionUtils.isNotEmpty(CollectionUtils.java:992)
    2 frames
  4. DSpace Kernel :: API and Implementation
    IndexClient.main
    1. org.dspace.content.DSpaceObject.getHandle(DSpaceObject.java:133)
    2. org.dspace.discovery.SolrServiceImpl.indexContent(SolrServiceImpl.java:198)
    3. org.dspace.discovery.SolrServiceImpl.indexContent(SolrServiceImpl.java:2305)
    4. org.dspace.discovery.IndexClient.indexAll(IndexClient.java:192)
    5. org.dspace.discovery.IndexClient.main(IndexClient.java:152)
    5 frames
  5. Java RT
    Method.invoke
    1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    4. java.lang.reflect.Method.invoke(Method.java:606)
    4 frames
  6. DSpace Kernel :: API and Implementation
    ScriptLauncher.main
    1. org.dspace.app.launcher.ScriptLauncher.runOneCommand(ScriptLauncher.java:229)
    2. org.dspace.app.launcher.ScriptLauncher.main(ScriptLauncher.java:81)
    2 frames