Java 保存对象时出错:未绑定休眠会话
错误:没有绑定到线程的Hibernate会话,并且配置不允许在此创建非事务会话 我正在调用线程,它将迭代列表,并通过查找程序和通道将其映射到事件对象。但我不明白我犯了什么错误?我正在创建循环100-500中的线程 下面是实现Runnable接口的Thread类Java 保存对象时出错:未绑定休眠会话,java,multithreading,grails,Java,Multithreading,Grails,错误:没有绑定到线程的Hibernate会话,并且配置不允许在此创建非事务会话 我正在调用线程,它将迭代列表,并通过查找程序和通道将其映射到事件对象。但我不明白我犯了什么错误?我正在创建循环100-500中的线程 下面是实现Runnable接口的Thread类 class SaveScheduleListThread implements Runnable { private List<ChannelSchedule> scheduleList; public Sa
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条记录刷新一次会话:
int counter = 0
while(scheduleListIterator.hasNext())
{
counter++
event = scheduleListIterator.next()
// do stuff
event.save flush:counter % 500
}
您想表示ChannelSchedule.withTransaction{processScheduleList}吗,因为事件不是类名中的类?是的,但使用什么类并不重要。所有域类的静态withTransaction都是相同的。我只是看到一个名为event的变量,并认为该类也被称为soohh yes!它的工作很好的人。。。谢谢。。。实际上,我在这里使用多线程插入了大约700000到800000条记录。您能否告诉我如何优化此设置,因为完成此任务需要14小时左右,而且系统负载太大。您必须刷新会话,每个线程200-1000条记录:事件。保存刷新:计数器%200。这将提高性能,另一个策略是完全不使用hibernate进行批处理
int counter = 0
while(scheduleListIterator.hasNext())
{
counter++
event = scheduleListIterator.next()
// do stuff
event.save flush:counter % 500
}