简体   繁体   English

保存对象时出错:没有休眠会话绑定

[英]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 错误:没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话


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 我在循环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 以下是实现Runnable接口的线程类

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: processScheduleList()放入事务中:

Event.withTransaction{
  processScheduleList()
}

UPDATE: 更新:

flush the session each 500 records: 每500条记录刷新一次会话:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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