0

I've looked at a lot of other questions about LazyInitializationException and @Transactional, and haven't seen anything that seems to help our case.

I apologise for being less than methodical investigating it. It's not my code but it's fallen into my hands for the time being...

We have code that looks like this:

@Entity
class TaskSchedule {
    // fields that make up a task schedule
    Date nextRunTime;
}

@Entity
class Task {
    @OneToMany(fetch = FetchType.LAZY)
    List<TaskSchedule> schedules;

    @Transactional
    public void doTask() {
        // run the task

        for (TaskSchedule schedule : schedules) {  /* CRASH HAPPENS HERE */
            // set the schedule's next run time
        }
    }
}

@Component
public class TaskDao {
    @Transactional(readOnly = true)
    public List<Task> getScheduledTasks() {

        Calendar calendar = Calendar.getInstance();

        Criteria criteria = getSession().createCriteria(Task.class);
        criteria.add(Restrictions.le("runTime", calendar.getTime()));
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        List<Task> results = criteria.list();
        return results;
    }
}

public class MonitorJob {
    TaskDao taskDao;

    public void runScheduledTasks() {
        // start a read only transaction
        List<Task> tasks = taskDao.getScheduledTasks(); // query database
        // finish the transaction

        for (Task task : tasks) {
            // start a transaction
            task.doTask(); // populate lazy list
            // finish transaction
        }
    }
}

We get a LazyInitializationException exception when the lazy list schedules is iterated over:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Task.schedules, no session or session was closed
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
        at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
        at Task.doTask(Task.java:366)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:197)
        at Task_$$_javassist_43.doTask(Task_$$_javassist_43.java)
        at MonitorJob.runScheduledTasks(MonitorJob.java:106)
        at sun.reflect.GeneratedMethodAccessor68.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
        at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:299)
        at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:111)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)

As far as I can tell, after the @Transactional method doTask is called for the first item in tasks, a whole lot of objects, including all the other lazy schedules lists for the remaining items in tasks, are removed from the session by having unsetSession called on them. This means that subsequent iterations over tasks crash because their lazy lists no longer have an associated session.

I haven't actually been working on this bit of code, but it has started failing recently (after our DB was reloaded). I can't see why it would have worked in the first place, if the objects get removed from the session.

1
  • 1
    Why not runScheduledTasks() with @Transactional annotated? And I haven't seen before @Transactional in entity level method. Jul 25, 2014 at 4:29

2 Answers 2

1

Ok, now you got self invocation. Please read all answers at the link I have posted earlier (note that it's also depends on whether you are using AOP proxies or not - but I'm assuming you are). In your current code snippet you need to annotate runScheduledTasks() with the @Transactional and remove @Transactional from getScheduledTasks() cause it's redundant and you don't need transaction for reading database. Also as A Nyar Thar mentioned you don't need to annotate your entity method with @Transactional.

Also from my experience @Transactional annotations are commonly used in service layer. If you wish to update each Task in new transaction when you need to create service method that always opens new transaction.

public class TaskService {

    @Transactional(propagation = REQUIRES_NEW)
    public void updateTask(Task task) {
        task.doTask();      
    }

}

then in MonitorJob

//in this case you don't need @Transactional here
public void runScheduledTasks() {
        ....
        List<Task> tasks = getScheduledTasks();
        for (Task task : tasks) {
            taskService.updateTask(task);
        }
    }
1
  • Gosh darn it! Again, getScheduledTasks is only in the same class because I edited the code to make clearer for StackOverflow. Now that I know how private and self-invocation affects @Transactional, I'll know not to do that in future.
    – Edmund
    Jul 27, 2014 at 21:57
0

@Transactional annotation only works with public methods. Either add @Transactional to runScheduledTasks() method and remove one from getScheduledTasks(), or make getScheduledTasks() public.

Read this for explanation: Does Spring @Transactional attribute work on a private method?

1
  • Changing the method visibility was in error I introduced when editing the code for SO. The original uses a public method. Thanks for spotting it! Any other suggestions?
    – Edmund
    Jul 27, 2014 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.