您的位置:首页 > 其它

使用@Async注解是hibernate中抛出LazyInitializationExceptions

2017-09-05 17:45 1396 查看
项目中有一个需求:批量产生一批数据,并存放在数据库中,并产生文件供下载。根据需求很容易想到@Async注解,但当注解加到service层的方法上时,却会抛出org.hibernate.LazyInitializationException。
项目使用spring3.1 和hibernate3.3.2GA,再经过各种谷歌之后终于找到原因,原因参考这里,具体是因为hibernate中session的范围问题。具体解决方法如下:
 @Service
public class AsyncServiceImpl implements AsyncServiceInt{

@Autowired private SlowProcess slowProcess;

@Override
@Async
public void startSlowProcess(Integer scenarioId) {
slowProcess.execute(param);
}

..

public class SlowProcess {

@Transactional
public double[] execute() { .. }

} 此解决方法来自:http://stackoverflow.com/questions/17278385/spring-async-generates-lazyinitializationexceptions
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐