使用@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
项目使用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
相关文章推荐
- 在Hibernate 4.3中如果使用了@Table注解,在获取session的时候会抛出如下异常解决办法
- Hibernate 注解实体关系(异常的org.hibernate.LazyInitializationException: could not initialize proxy )
- 关于使用hibernate出现org.hibernate.LazyInitializationException: could not initialize proxy - no Session错误
- org.hibernate.LazyInitializationException: could not initialize proxy - the owning
- org.hibernate.LazyInitializationException(懒加载异常)原因
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- Hibernate - 使用注解完成映射
- Hibernate使用注解设定关联关系
- Hibernate注解使用
- Hibernate注解方法使用总结
- Hibernate注解使用方法总结
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
- Hibernate学习(4)- 注解的使用
- 如何在DAO层既使用注解又使用HibernateDaoSupport
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(一):消除hibernate中<mapping resouce 的xxxx. hbm.xml文件
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
- Hibernate注解使用以及Spring整合
- Spring使用OpenSessionInViewFilter解决Hibernate的lazy延时加载问题
- J2EE进阶(九)org.hibernate.LazyInitializationException: could not initialize proxy - no Session