错误:没有 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/