user3074874
user3074874

Reputation: 95

(failed to lazily initialize a collection, no session or session was closed) error in quartz-0.4.2 in grails 1.3.7

Job

 class PicSchedulerJob {
        def myDataService
        def springSecurityService
        static triggers = {
            simple name:'picsJob', startDelay:1000,repeatInterval:30*60*1000 
        }
        def group = "icsJobGroup"

        def execute() {

            def userList=User.list()
            userList?.each{User user->  
                def fullList= Album.findAllByUser(user) 
                springSecurityService.reauthenticate(user.username)
                fullList?.each{Album a->
                    myDataService.removePicsFilter( a)          
                }

            }
        }
    }

Method:

def removePicsFilter(Album a){
        def tempList=a.photo
        int siz=tempList.size()?:0
        for(int i=0;i<siz;i++)  {
            Photo photodb=tempList[i]
            PhotoConnection photoConnection=PhotoConnection.findByPhoto(photodb)
                photoConnection?.tags.each{
                    Tag t=Tag.get(it.id)
                    t.delete()
                    }


        }
        a.save(validate:true,flush:true)
    }

Error:2013-12-06 20:20:59,618 [quartzScheduler_Worker-5] ERROR hibernate.LazyInitializationException - failed to lazily initialize a collection, no session or session was closed org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)

Job is quartz job code, method code called by job and its error is the error i am facing..i also try the code with Album.withTransaction and also Album.withSession but not able to fix it. Pls guide me on this

Upvotes: 3

Views: 6857

Answers (2)

Amit Kumar user3037405
Amit Kumar user3037405

Reputation: 390

The problem is that either the Hibernate session is not open or it was closed. Please go through the link http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-fetching-lazy

Also it may be the case that the object got detached from the hibernate session. You can try this:

attach the object back to the hibernate session like this: object.attach() (http://grails.org/doc/2.2.1/ref/Domain%20Classes/attach.html) make the association between the two domain classes non-lazy (http://grails.org/doc/2.2.x/ref/Database%20Mapping/lazy.html)

Upvotes: 3

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

Hope it will help.

Use Hibernate.initialize() within Transactional to initialize lazy objects.

 start Transaction 
      Hibernate.initialize(album.photo());
 end Transaction 

 out of Transaction
   tempList=album.photo

Upvotes: 1

Related Questions