java - 保存对象时出错 : No Hibernate Session bound

标签 java multithreading grails

错误:没有 Hibernate Session 绑定(bind)到线程,并且配置不允许在此处创建非事务性 session

我正在调用线程,它将迭代列表并通过查找程序和 channel 将其映射到事件对象。但我没有明白我犯错的地方吗?我正在循环 100-500 中创建线程

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

以下是实现可运行接口(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();

        }
    }

}

最佳答案

processScheduleList()在交易中:

Event.withTransaction{
  processScheduleList()
}

更新:

每 500 条记录刷新 session :
int counter = 0
while(scheduleListIterator.hasNext())
        {
            counter++
            event = scheduleListIterator.next()
            // do stuff
            event.save flush:counter % 500
        }

关于java - 保存对象时出错 : No Hibernate Session bound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330611/

相关文章:

java - Magnolia 6.0 至少执行 MyModule 的 UpdateTasks

java - 如何在 java swing 中以线程安全的方式初始化 gui 对象?

java - 多线程 Java 仅使用一个线程

grails - 新手-在GGTS中安装webtest

java - 正则表达式:如何处理空引号

java - 了解 JPA 序列生成器

java - 建议非常快(微秒)的 java 日志记录?

validation - Grails 验证不起作用

grails - 重构Grails模板以期望使用 map 而不是列表

java - SpringBoot,如何在不使用ldif的情况下使用LDAP进行身份验证?