简体   繁体   中英

Error in saving an object: No Hibernate Session bound

Error : No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here


I'm calling thread which will iterate the list and By finding program and channel map it to event object. But I'm not getting that where I'm making mistake? I'm creating thread in loop 100-500


ExecutorService executor = Executors.newFixedThreadPool(10);//creating a pool of 5 threads
Runnable SaveSchedule = new SaveScheduleListThread(scheduleList.clone());
executor.execute(SaveSchedule);

Following is the Thread Class which implements the Runnable Interface

class SaveScheduleListThread implements Runnable
{
    private List<ChannelSchedule> scheduleList;

    public SaveScheduleListThread(List<ChannelSchedule> scheduleList)
    {
//      this.scheduleList = new ArrayList<ChannelSchedule>();
        this.scheduleList=scheduleList;
    }


    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run()
    {
        // TODO Auto-generated method stub

        log.info "Thread:" +Thread.currentThread().getName() +" Started Time:"+System.currentTimeMillis();
        processScheduleList();
        log.info "Thread:" +Thread.currentThread().getName() +" Ended Time:"+System.currentTimeMillis();

    }

    private void processScheduleList()
    {
        try
        {
            Iterator<ChannelSchedule> scheduleListIterator = scheduleList.iterator();

            ScheduleProgram programDetails = new ScheduleProgram();
            IpgChannel channelDetails = new IpgChannel();
            ChannelSchedule event = new ChannelSchedule(); 

            while(scheduleListIterator.hasNext())
            {
                event = scheduleListIterator.next();

                programDetails = ScheduleProgram.findByTmsId(event.getEventTMSId());
                channelDetails = IpgChannel.findByPrgSvcId(event.getEventPrgSvcId());

                event.setProgram(programDetails);
                event.setChannel(channelDetails);

                event.save();
            }
        }
        catch(Exception e)
        {
            session.open()
            log.error "Exception while Processing ScheduleList in Thread"
            log.error e.getStackTrace();
            log.error e.getMessage();

        }
    }

}

put processScheduleList() inside a transaction:

Event.withTransaction{
  processScheduleList()
}

UPDATE:

flush the session each 500 records:

int counter = 0
while(scheduleListIterator.hasNext())
        {
            counter++
            event = scheduleListIterator.next()
            // do stuff
            event.save flush:counter % 500
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM